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 / Function MakeDWORD(byte1,byte2,byte3,byte4)

Author
Message
Zep
15
Years of Service
User Offline
Joined: 31st Aug 2002
Location: From PA, USA. Currently reside in Hanoi, Vietnam
Posted: 11th Mar 2018 06:43 Edited at: 26th Mar 2018 13:24
I needed this for my DBO texturer.

will take 4 bytes (say, from an array of bytes as I use) and return the DWORD Value. (can also do/return floats from bytes with the appropriate changes)

Rudolpho
12
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 11th Mar 2018 09:23
Some tips:
1) Keep a single memory allocation for this kind of conversion; DBPro programs are single threaded anyway so there won't be any race conditions and recreating the memory allocation each time will cost a lot of time.
2) For combining bytes into a larger integer data type you can just bitshift and OR them together, like so:
Zep
15
Years of Service
User Offline
Joined: 31st Aug 2002
Location: From PA, USA. Currently reside in Hanoi, Vietnam
Posted: 11th Mar 2018 09:35
Quote: "2) For combining bytes into a larger integer data type you can just bitshift and OR them together, like so: "


Nice, got one for floats?

I originally did the float one first, but thought the DWORD example would be a little easier to understand.


Zep
15
Years of Service
User Offline
Joined: 31st Aug 2002
Location: From PA, USA. Currently reside in Hanoi, Vietnam
Posted: 11th Mar 2018 09:41
Good tip about Make Memory , I'll throw that in the global variables file and just create it once. First time I've used that command.
Zep
15
Years of Service
User Offline
Joined: 31st Aug 2002
Location: From PA, USA. Currently reside in Hanoi, Vietnam
Posted: 11th Mar 2018 10:59
@Rudolpho,

dwValue = (byte1 << 24) || (byte2 << 16) || (byte3 << 8) || byte4

I plugged this in to test...it doesn't work.

My bytes from 1 to 4 --- 150,1,0,0

dwValue = (150 << 24) || (1 << 16) || (0<< 8) || 0
Should be dword val 406 (and it shows that in my function), but the bit shifting shows dword val 65536 in DBP.

I think you would need to rearrange the byte order (or something) which I don't have to do. I'd rather just feed mine the 4 sequential bytes and be done with it.
Zep
15
Years of Service
User Offline
Joined: 31st Aug 2002
Location: From PA, USA. Currently reside in Hanoi, Vietnam
Posted: 11th Mar 2018 11:18 Edited at: 11th Mar 2018 11:19
Tried again, as I had made a small mistake (copy/pasta).

It actually comes up with some huge number: 2516647936 instead of 406 as it should.

But I do believe it is in the byte ordering. It probably works if you put the bytes in the right places for shifting.
Zep
15
Years of Service
User Offline
Joined: 31st Aug 2002
Location: From PA, USA. Currently reside in Hanoi, Vietnam
Posted: 11th Mar 2018 11:22
Ok, if I reverse the order of bytes

Bytes 4 to 1

The shifting works.
Zep
15
Years of Service
User Offline
Joined: 31st Aug 2002
Location: From PA, USA. Currently reside in Hanoi, Vietnam
Posted: 11th Mar 2018 13:42
Oh, reversing the shift order also works, so'it is more in line with the way DBP stores dwords.

val3 = (150) || (1 << 8) || (0 << 16) || (0 << 24)

or

val3 = (byte1) || (byte2 << 8) || (byte3 << 16) || (byte4 << 24)
Zep
15
Years of Service
User Offline
Joined: 31st Aug 2002
Location: From PA, USA. Currently reside in Hanoi, Vietnam
Posted: 12th Mar 2018 03:49
Cleaned this up a bit using Rudolpho's bit-shifting and I switched to mem-blocks to make my float out of the 4 bytes (shorter codes compared to using pointers).



(and yes, I moved the "make memblock" command to my init routine, it is only here for the example)
I'm sure it can still be streamlined even more.
Mage
11
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 12th Mar 2018 10:29
Zep wrote: "(and yes, I moved the "make memblock" command to my init routine, it is only here for the example)"

Good job figuring that out, and also for mentioning it.

Login to post a reply

Server time is: 2018-06-24 08:10:32
Your offset time is: 2018-06-24 08:10:32