• 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: NSData to long
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSData to long


  • Subject: Re: NSData to long
  • From: Malte Tancred <email@hidden>
  • Date: Tue, 8 Apr 2003 16:58:37 +0200

On tisdag, apr 8, 2003, at 03:50 Europe/Stockholm, David Blanton wrote:
NSAppleEventDescriptor * aDescriptor = [theDescriptor descriptorAtIndex:i];

theData = [aDescriptor data];

NSLog(@"theData %@",theData); yields : theData <00000024 >

I want to convert the NSData object <00000024> to an NSString that would be
"36"


NSString * str = [[NSNumber numberWithLong:foo] stringValue];

Would work if I could make <00000024> into a long. So how does one get a
long value from an NSData object?

Still puzzled.

You could perhaps do it the hard way:

char data[4] = { 0,0,0,0x24 };
long number = 0;
char *tmp = (char *)(&number);

*tmp = data[0]; ++tmp;
*tmp = data[1]; ++tmp;
*tmp = data[2]; ++tmp;
*tmp = data[3];

printf("number: %d\n", number);

Using this approach you really have to know how large the types are -- ie long is four bytes -- and whether the data is big or little endian.

I'm no expert in C so the above code migth be really bad in a way I don't grasp. Use with care!

There might also be a cleaner way of doing this. Perhaps the deprecated NSSerializer stuff did this, I don't know.

NSValue might also do it, but I'm not sure how to convert an NSValue created with -initWithBytes:objCType: into an appropriate NSNumber subclass (NSNumbers inherit NSValue); the init-method seem to always create an NSConcreteValue while NSNumber's +numberWithInt:, for example, creates a NSCFNumber. Perhaps I missed something in the documentation when I glanced through it.

Anyway, this was just a few thoughts intended to bring you total confusion. :-)

Cheerio,
Malte

--
Malte Tancred
Computer Programmer
Oops AB, http://oops.se/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >NSData to long (From: David Blanton <email@hidden>)

  • Prev by Date: Re: NSData to long
  • Next by Date: Re: How-To: private framework
  • Previous by thread: Re: NSData to long
  • Next by thread: Re: NSData to long
  • Index(es):
    • Date
    • Thread