• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSStrings and data bytes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSStrings and data bytes


  • Subject: Re: NSStrings and data bytes
  • From: Chris Suter <email@hidden>
  • Date: Tue, 27 Jun 2006 15:49:12 +1000

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

It's not valid to store data in an NSString that's not of the encoding you specify i.e. you should only put text in an NSString. I think the right way to do this is to write your own scanner and use NSData.


It shouldn't be that difficult to write a scanner that will work. Something like the code below would get your data bytes (I haven't tried it so it might have a bug or two in it), and I'm sure you could modify it easily to store the text parts of your data.

int len_to_go = [data length];
u_int8_t *start = (u_int8_t *)[data bytes], *end;

while (len_to_go > 0) {
  --len_to_go;
  if (*start & 0x80) {
    end = start + 1;
    while (len_to_go && (*end & 0x80)) {
      --len_to_go;
      ++end;
    }
    // Got a bit of data between start and end, do something with it
    // ...

    start = end + 1;
    --len_to_go;
  } else
    ++start;
}

If you're worried about speed, you can change it so that you check 4 bytes at a time.

- Chris

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
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

References: 
 >NSStrings and data bytes (From: "D.K. Johnston" <email@hidden>)

  • Prev by Date: NSStrings and data bytes
  • Next by Date: Action of the checkboxes in a table view not getting called
  • Previous by thread: NSStrings and data bytes
  • Next by thread: how do I use a SEL from another class?
  • Index(es):
    • Date
    • Thread