Page 1 of 1
Convert binary display to binary
Posted: Fri Nov 09, 2012 9:34 am
by rolly
Hi All,
I have an external program that returns a 4 or 32 byte binary display string in the format "0010101101001010" and "0010101101001010001010110100101000101011010010100010101101001010"
Is there a simple 'type cast' way to convert this string into a Jade 4 Byte Binary variable or do I need to build a method do this conversion, and if so, any ideas for how to do this efficiently?
Thanks
Roland
Re: Convert binary display to binary
Posted: Fri Nov 09, 2012 9:51 am
by allistar
That's an odd way for an external program to return data. If you have the code for that, could it not be changed to return the type of data that you need?
You can fairly easily convert a string of 1s and 0s to an array of Bytes (or an Integer) by breaking the string up into 8 character chunks and then walking each of those 8 characters, using bitOr on a resultant Byte in JADE to set the bit to "on" if it's a 1. You'll need to know whether it's big-endian or little-endian to know which bit is the least significant.
If you need this to be fast I wouldn't do it in JADE, I'd do it in an external function which does effectively the same thing, except it uses "|" instead of "bitOr".
Re: Convert binary display to binary
Posted: Fri Nov 09, 2012 9:58 am
by ghosttie
Here is some old code I had lying around - it's a method on the Binary primitive to set a specific bit:
Code: Select all
setBitValue(pI_BitNumber : Integer; pB_Value : Boolean) updating;
vars
iByteNo, iBitNo, iBitMask, iBitIndex : Integer;
begin
// adjust the bit number so that from the outside it looks like bit 1 is the first one, where really bit 0 is
iBitIndex := pI_BitNumber - 1;
// get the number of the byte which contains the bit we want to modify
iByteNo := (iBitIndex div 8) + 1;
// get the number of the bit within that byte that we want to modify
iBitNo := 7 - (iBitIndex mod 8);
// create a bit mask which will let us modify the bit
iBitMask := 2^iBitNo;
// make sure the binary is big enough to set this bit
while self.length < iByteNo do
self := self & 0.Character.Binary;
endwhile;
if pB_Value then
self[iByteNo] := self[iByteNo].bitOr(iBitMask.Byte);
else
// in order to un-set a bit, we need to invert the source, set the bit and then invert it back
self[iByteNo] := self[iByteNo].bitNot.bitOr(iBitMask.Byte).bitNot;
endif;
end;
Then you just need to write a method to loop through your string and call the primitive method for each bit:
Code: Select all
bitstringtobinary();
constants
BitString : String = "0010101101001010";
vars
bin : Binary;
i : Integer;
begin
foreach i in 1 to BitString.length do
bin.setBitValue(i, BitString[i : 1] = "1");
endforeach;
write bin.display;
end;
There are lots of ways it could be improved.
Re: Convert binary display to binary
Posted: Fri Nov 09, 2012 12:22 pm
by JohnP
Is "0010101101001010" actually a string of bits? A 4 byte value would require 32 bits, not 16.
Re: Convert binary display to binary
Posted: Fri Nov 09, 2012 3:19 pm
by rolly
Thanks for this.
I know it's a strange way for a program to return data, but it's a command-line program using this method to display binary data which will include unprintable characters.
Also, yes JohnP, the example I typed in was too short.
Roland