• 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: Does NSData rearrange the order of bits?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Does NSData rearrange the order of bits?


  • Subject: Re: Does NSData rearrange the order of bits?
  • From: Bryan Henry <email@hidden>
  • Date: Wed, 02 Dec 2009 05:02:23 -0500

Your log messages show that the NSData's bytes are stored completely correctly, you're just interpreting it incorrectly.

NSData's description method will list the bytes in order, so you see "510600f0". On the other hand, you used the %x format specifier to create your string, which will print the first byte last, so you get "f0000651". That's exactly the same infomation-wise, the order of bytes is just flipped.

If you want to create an NSString from the bytes of an NSData where the lower bytes are ordered first, you can use this:

NSUInteger len = [theToken length];
const unsigned char *bytes = [theToken bytes];
NSMutableString *tokenStr = [NSMutableString stringWithCapacity:len*2];
for (NSUInteger i = 0; i < len; ++i) {
	[tokenStr appendFormat:@"x", bytes[i]];
}

- Bryan

(Original reply to Brad directly resubmitted to list at request)

On Nov 30, 2009, at 2:27:55 PM, Brad Gibbs wrote:

> Hi,
>
> I'm doing bit-packing via a C function.  Logging the bits of the C function shows the expected result.  If I create a string with a hex value format, I get the correct hex string, but, if I try to put the bytes into an NSData object with [NSData dataWithBytes: length], the order of the bits changes.  All of the right elements are there, but they're in the wrong order (target data should be f0000651, as shown in the Target string is ... log).
>
> My code:
>
>
> 	// get the target int from the text field
> 	unsigned int tgtValue = [self.tgtTF intValue];
>
> 	// use the target int and type to pack the bits into an int
> 	uint32_t tgtBinary = setAnalogValueForIndex(cid, tgtValue);
> 	NSString *tgtString = [NSString stringWithFormat:@"%x", tgtBinary];
> 	NSData *tgtData = [NSData dataWithBytes: &tgtBinary length: sizeof(tgtBinary)];
> 	NSLog(@"Target data is %@.  Target string is %@", tgtData, tgtString);
>
>
> The logs:
>
> 11110000000000000000011001010001
> 2009-11-30 11:02:26.126 CertTest[11959:a0f] Target data is <510600f0>.  Target string is f0000651
> 2009-11-30 11:02:26.204 CertTest[11959:a0f] After adding target, cmdData is <510600f0>
>
> If NSData is rearranging the bits, is there some way to prevent this?
>
>
> Thanks.
>
> Brad
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: Application crashing on iPod Touch, not on iPhone
  • Next by Date: Modifying TableHeaderCell
  • Previous by thread: Re: Does NSData rearrange the order of bits?
  • Next by thread: Re: iterating and removing objects from a collection
  • Index(es):
    • Date
    • Thread