NSStrings and data bytes
NSStrings and data bytes
- Subject: NSStrings and data bytes
- From: "D.K. Johnston" <email@hidden>
- Date: Mon, 26 Jun 2006 21:51:16 -0700
(Sorry, I forgot the subject line again.)
I want to read the bytes from a file as an NSString so I can use
scanners, etc. But then I need to get at the original bytes in
order to process them. (Sometimes I even have to get the right and
left nibbles of a byte.) I've been playing around with NSString
methods like cStringUsingEncoding:, but I can't seem to get the
bytes back. Any suggestions?
Go the other way around. Read the bytes into an NSData object and
then use initWithData:encoding: to get the string for your text,
specifying the proper encoding, of course. From there, just do your
work with NSScanner on that object. (I assume the order in which
your data and string are created doesn't matter.)
You can also use NSString's initWithBytes:length:encoding: if you
just want to read in the data using the standard C libraries.
My problem is that the file I'm reading contains both data bytes and
text bytes. The former are distinguished from the latter only by
having their high bits set. So the first thing I want to do is use an
NSScanner to separate the data bytes from the text bytes; and to do
this I read the file as an NSString. (Why don't NSData objects have
scanners?) This gives me one array of strings composed of the data
bytes, and another array of strings composed of the text bytes. The
text byte strings are fine just the way they are. But to use the data
byte strings, I need to get the original bytes back again.
In the previous version of the programme, I did read the file as an
NSData object. But then I found it complicated (and time-consuming)
to separate the data bytes (high bit set) from the text ones. I also
have to look for sub-sequences of data bytes, and this is so much
simpler (and faster) using the rangeOfString: method. Or have I been
missing something simple here?
dkj
_______________________________________________
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