Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Convert String value to DWORD?

Author
Message
Omen
18
Years of Service
User Offline
Joined: 7th Nov 2006
Location: Maple Grove, MN US
Posted: 14th Dec 2008 02:18
I'm trying to convert a numeric value stored in a text string into a dword. Seems simple enough, but I haven't been able to find a way of doing this...

val() function returns the value as a float, so that won't cut it

I did try taking the float returned from val() and converting it again with

CAST FLOAT TO DWORD ( Float Value )

...from matrixutil4, but it gives me odd results.

Anyone know how to do this?

zenassem
22
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 14th Dec 2008 02:19
what size are INTs??

~Zenassem
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 14th Dec 2008 02:43 Edited at: 14th Dec 2008 02:43
What are these 'odd' results you are seeing?

DWORD is an unsigned integer (4 bytes) by the way.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 14th Dec 2008 02:57
Don't use VAL. Because it returns a float, there are some integers that won't be returned correctly. Don't use the CAST functions, because they simply return the binary value.

Use INTVAL - that has the correct range to allow you to convert to an integer.

Omen
18
Years of Service
User Offline
Joined: 7th Nov 2006
Location: Maple Grove, MN US
Posted: 14th Dec 2008 03:04 Edited at: 14th Dec 2008 03:09
@IanM,

It would seem then that converting the value I get out of INTVAL into a DWORD would be as simple as...



...something along those line? This seems to work, but I know you've had a lot of experience with dbpro's auto-casting, so I'm wondering if you've had any problems with the int->dword cast.

@Benjamin

I banged my head into the VAL() wierdness because I wasn't inspecting the intermediate value, and I should have been

@zenassem
4 bytes

zenassem
22
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 14th Dec 2008 03:24
Well IanM beat me too it, but I was going to suggest trying intval!

~Zenassem
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 14th Dec 2008 04:05
@Omen,
It's completely safe to put the value directly into a dword without using an integer intermediary. In fact, the INTVAL function returns a double integer, which is easily capable of handling the full range of values for integers and dwords.

As for integer <-> dword conversion, as long as the number is between 0 and 2147483647, you'll get the same value from each. Negative integers convert to positive dwords above 2147483647 using standard 2's-complement and vice versa - the conversion is loss-less, ie no matter what value you use, integer -> dword -> integer will give you the same number as you passed in originally, as will dword -> integer -> dword

Omen
18
Years of Service
User Offline
Joined: 7th Nov 2006
Location: Maple Grove, MN US
Posted: 14th Dec 2008 04:37
That's the info I was looking for - thanks a ton, as always, Ian!

Login to post a reply

Server time is: 2025-05-27 20:50:16
Your offset time is: 2025-05-27 20:50:16