Convert binary display to binary

Forums for specific tips, techniques and example code
User avatar
rolly
Posts: 11
Joined: Mon Aug 17, 2009 9:08 am

Convert binary display to binary

Postby rolly » Fri Nov 09, 2012 9:34 am

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

allistar
Posts: 156
Joined: Fri Aug 14, 2009 11:02 am
Location: Mount Maunganui, Tauranga

Re: Convert binary display to binary

Postby allistar » Fri Nov 09, 2012 9:51 am

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".

User avatar
ghosttie
Posts: 181
Joined: Sat Aug 15, 2009 1:25 am
Location: Atlanta, GA, USA
Contact:

Re: Convert binary display to binary

Postby ghosttie » Fri Nov 09, 2012 9:58 am

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.
I have a catapult. Give me all the money or I will fling an enormous rock at your head.

JohnP
Posts: 73
Joined: Mon Sep 28, 2009 8:41 am
Location: Christchurch

Re: Convert binary display to binary

Postby JohnP » Fri Nov 09, 2012 12:22 pm

Is "0010101101001010" actually a string of bits? A 4 byte value would require 32 bits, not 16.

User avatar
rolly
Posts: 11
Joined: Mon Aug 17, 2009 9:08 am

Re: Convert binary display to binary

Postby rolly » Fri Nov 09, 2012 3:19 pm

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


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 1 guest