Re: Tiger Decoding problem
Re: Tiger Decoding problem
- Subject: Re: Tiger Decoding problem
- From: Dustin Voss <email@hidden>
- Date: Mon, 30 May 2005 18:12:37 -0700
On 30 May 2005, at 3:51 PM, Nicholas Crosbie wrote:
The data "is a continuous bit stream with no
delimiters", sent in the form of unsigned binary
integer. I'm attaching the data segment. It looks
binary to me - at least, it's a pile of hieroglyphics
when opened in a text editor.
It's a good thing I spoke up, then. You should not be using NSString.
You need to know the endian-ness and size of the integers. They could
be 1-byte, 2-byte, 4-byte, or 8-byte, and little-endian or big-
endian. Looking at the file in a hex editor, it looks like 2-byte
little-endian integers, almost entirely less than 768.
I thought first to use NSData; however, NSData's
deserialize methods (which, to the newbie, look like
they might help me to convert the data to a human
readable form - but perhaps I'm way of track) are
deprecated in Tiger. So how would I make proceed if
using NSData?
A good way to handle binary data is to treat NSData as if it holds an
in-memory C data structure.
An undelimited series of unsigned 2-byte integers looks just like a C
array of UInt16. A C array is just a pointer to the first element of
the array, with some friendly syntax to access other elements. So do
this:
const UInt16 *intStream = (const UInt16 *)[fileData bytes];
int intCount = [fileData length] / sizeof(UInt16);
You can access each element of the array with intStream[0], intStream
[1], etc., and it will work out right.
The only other thing to watch out for is endian-ness. The elements
may have the structure of a UInt16 array, but unless you correct the
endian-ness, the elements won't have the correct values. So, for each
element, do this:
UInt16 realValue = CFSwap16LittleToHost (intStream[i]);
This will make the little-endian 2-byte integers right for the Mac.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden