Friday, November 28, 2008

Powerbuilder byte datatype

One of the more frustrating problems with programming with windows api in powerbuilder is the lack of the byte datatype. Although as of powerbuilder 10.5 and up it is now included, those of us stuck with a version below that have to work a little harder to get the same results. This is how I was able to work around this..

char is the smallest datatype that powerbuilder handles and it is two bytes long. So, when ever the need for defining a byte array rises you would have to cut the size in half when defining the char array. For example..if funtion calls for a byte array byte[6], you would declare: char lc_char[3].

Now the hard part..say for this byte array the function is expexting the following:
byte[1] = 192
byte[2] = 00

byte[3] = 00
byte[4] = 70

byte[5] = 192
byte[6] = 70

Now, lets take the first two bytes to powerbuilder:
lc_char[1] = char(192)

This works as expected..lets set the next two bytes
lc_char[2] = char(70)

This is where things get tricky..how does powerbuilder decide which byte is filled with 00 and which byte is filled with 70? Short answer Endianness. Powerbuilder uses Little-endian store bytes in memory. We got lucky with the first one, it stored the number we wanted in the right spot. But if we leave it how we have it right now this is the result we would get:
byte[1] = 192
byte[2] = 00
byte[3] = 70
byte[4] = 00

Which is out of order. The easiest way to fix this is with a calculator. Since we now know that the bytes are stored backwards all we have to do is find the equivalent of 70 backwards. To do this we convert 70 to the hex equivalent(easily done with windows calc. just change the view to scientific then type the number you want to convert click the hex option): h46. So, we have h00 h46. Flip to get h46 h00. back to calc starting in hex mode type "4600", then hit dec. h4600 = 17920.

So, back to our char array. if we want to set the third and fourth byte to 00 70, we do this:
lc_char[2] = char(17920)

Now were in business. A quick example of the last two bytes in the array:
what we want: 192 70
Convert to hex: C0 46
Reverse: 46 C0
Convert to dec: 18112

lc_char[3] = char(18112)

Fun stuff.