WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Binary Functions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

BinaryPoke and Bit Swapping Discussion

Keywords: 	  BinaryPoke Bit

Question:

I have a binary buffer, into the first 4 bytes must be placed a 16bit integer within the value range 73-2048. I have been practicing with 838, which I believe should appear as 03460000 in the WB browser, but I get 46030000, so I guess I need some magic to unswap the bytes. I am using
BinaryPoke2(b,0,838)
or do I need to break 838 into 2 8bit numbers?

Answer:

Probably have to swap 'em.

BinaryPoke2 stuffs the data using the Intel format. Apparently your data wants it differently.

x=838
BinaryPoke(bb,0, (x>>8)&255)
BinaryPoke(bb,1,x&255)

Question (cont'd):

One more question on bit swapping in general...

If one were to open a binary file with the WB browser, and the first 2 bytes were HEX 0346 or [838], the recommended way to process the date would be ( assuming binbuf is the binary buffer )

first_two_bytes= (BinaryPeek(binbuf,0) << 8)+BinaryPeek(binbuf,1)
In you last post, you alluded to how the original data might be written. Is this a "when to swap, when not to swap" dilemma, or are there rules governing binary behavior?

I realize I will get back a lot of Litle/Big Endian logic, so the question is from a layman without that instant binary calculation mentality.

Answer:

Ummmm That looks right.

When to swap.

Depends. Mostly you need to either have or be able to infer the layout of the data in the file.

In general most of the problems will involve numeric fields larger than one byte. The value of each field must be understood and the number built from the bytes.

BinaryPeek2, BinaryPeek4 and BinaryPeekFloat all assume a prticular byte ordering that is common to Intel processors.

It turn out that (IMHO) the Intel ordering is most consistent, but that other (very) confusing factors make it seem like the oddball.

Basically any processor can make any file format, but fileformats that are "native" to the processor are usually chosen.

Mostly you have to get the bytes in a numeric field and figure out how to arrange them so that they give you the number you want.


Question (cont'd):

But there are no standards for this byte order...?

Answer:

No standards whatsoever. In fact they do not even need to be adjacent. It all has to do with the specifications of the file format.

Often the file format designers even overlook the byte order problem, not even realizing such a problem exists. However they are at least usually consistent within a file.

OK. Now I will explain the "blackboard problem".

In my years of working with this problem, I think I can trace the entire little endian, big endian and related problems to early Computer science professors. Here is how it works:

In CompSci 101 in college, the professor draws a large box on the blackboard and calles it a visual represntation of the machine's memory.

For this discussion lets say he draws a long, narrow box across the blackboard so that everything is laid out in one line.

Then he subdivides the box into individual bytes. Now the trick part comes. Lets say he makes 32 bytes. And numbers them.

Some professors start numbering from the left. 0, 1, 2, 3, ... 31

Some professors number from the right

31, 30, 29, ... 2, 1, 0

In the first case it is numbers kind of how left-to-right languages read, from left to right. In the other case it is numbered from right to left. (how right-reading languages read, and more in keeping with bit numbering where the left-most bit of a 32 bit number is considerd bit 31 (the most significant bit) and the right-most is 0 (the least significant bit).

(It helps if you draw this on a piece of paper)

Now lets day you have a 16-bit number (2 bytes) and you wish to store this into memory. You need two memory locations for this. Lets pick locations 2 and 3.

In the left to right ordering it would make sense (kind of) to put the high-order byte into location 2 and the low order byte into location 3. Seems reasonable, but the the high order byte is in the low numbered address and the low order byte is in the high numbered address. Hmmmm

In the other case the high-order byte would fit naturally into location 3 and the low order byte into location 2. So the high ordered byte is in the higher numbered memory location and the low ordered byte is in the low numbered memory location. Seems logically consistent.

However another disaster occurred. It turns out that most hex edit/view programs display the bytes in a left to right numbering scheme, making the Wintel bytes appear in the wrong order, and thus making the Wintel numbers appear "flipped".

Then there are other manifestations of this issue. It's a mess.


A Related Question):

I've got a file which is output from an SGI Unix box. The file has loads of 32bit floats as the data values. As the file is output in Big Endian format from the SGI, how can I get at the values?. The BinaryPeekFlt function is looking at 8 bytes as it returns a 64bit float and I need to get at the 32bit float values. Is there a way that I can BinaryPeek a 32bit float?

Answer:

To get a 32-bit value you would use BinaryPeek4, not BinaryPeekFlt, but then it wouldn't be treated as floating point. Plus you'd need to switch the byte order around.

If you make a new temporary 8-byte binary buffer, then BinaryPeek() the four individual bytes that comprise the 32-bit value, and then BinaryPoke() them into the temp buffer in swapped order in such a way that you can then do a BinaryPeekFlt on the temp buffer to get the correct value (setting the other four bytes of the 64-bit value to zero) this would probably leave the problem that the most significant bit of the value is the sign bit, which you would have to move from byte 8 to byte 16 using bitwise operators.

Unfortunately, byte swapping alone will not do it. Here is a link to an MSFT site that shows the difference between 32 and 64 bit little endian floating point formats.

http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html

Article ID:   W15446
File Created: 2003:05:13:11:27:56
Last Updated: 2003:05:13:11:27:56