• 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: How do I convert data in NSString to NSImage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I convert data in NSString to NSImage


  • Subject: Re: How do I convert data in NSString to NSImage
  • From: j o a r <email@hidden>
  • Date: Wed, 10 Sep 2003 10:11:15 +0200

On onsdag, september 10, 2003, at 09:47 AM, Chris Ridd wrote:

That's only going to work by luck, isn't it?
Since any JPEG is quite likely to have a NUL byte somewhere inside it,
[picstr cStringLength] is going to return a "too short" value. The value
you're passing for length probably doesn't really matter, but if NSData
starts to copy stuff around, you're going to lose.
Conversely, if the JPEG *doesn't* have a NUL byte inside it, [picstr
cStringLength] is going to walk off reading unallocated memory and possibly
crash your app :-(
How exactly did you read the JPEG into an NSString in the first place? Is it
possible to read it into a more appropriate data structure?

You could use a string, but you'd have to make sure that you encode it properly to avoid problems like this. I have a category on NSData that I use for a similar purpose (I think it's applicable here, let me know if I'm wrong). Using these methods it should be easy to go from data - string and then back to data on the other end.

@implementation NSData (HexStringRepresentations)

#define Dec2Hex(d) ((d) < 10 ? (d) + 48 : (d) - 10 + 'a')
#define Hex2Dec(x) ((x) >= '0' && (x) <= '9' ? (x) - 48 : (x) - 'a' + 10)

+ (NSData *) dataWithHexString:(NSString *) hexString
{
// NSLog(@"+NSData(HexStringRepresentations).dataWithHexString: %@", hexString);

// We should never fail this test since the string passed to this method
// should be a string created by the complementary method "hexStringRepresentation"
// NSAssert([string canBeConvertedToEncoding:NSASCIIStringEncoding], @"Assertion failure: String cannot be converted to plain ASCII encoding.");

{
NSMutableData *data = [NSMutableData data];
const unsigned char *p = [hexString cString];

int i;
for (i = 0; i < [hexString cStringLength]; i += 2)
{
char d = Hex2Dec(p[i]) * 16 + Hex2Dec(p[i+1]);
// NSLog(@"d: %c", d);
[data appendBytes: &d length: 1]; // 1 == sizeof(char)
}

return data;
}
}

- (NSString *) hexStringRepresentation
{
// NSLog(@"-NSData(HexStringRepresentations).hexStringRepresentation, %@", self);

{
NSMutableString *string = [NSMutableString string];

int i;
unsigned char *b = (unsigned char *)[self bytes];
for (i = 0; i < [self length]; i++)
{
// NSLog(@"b: %c", *b);
[string appendFormat: @"%c%c", Dec2Hex(*b / 16), Dec2Hex(*b % 16)];
b++;
}

// NSLog(@"hexString: %@", string);

return string;
}
}

@end

j o a r
_______________________________________________
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.

  • Follow-Ups:
    • Re: How do I convert data in NSString to NSImage
      • From: Chris Ridd <email@hidden>
References: 
 >Re: How do I convert data in NSString to NSImage (From: Chris Ridd <email@hidden>)

  • Prev by Date: Re: How do I convert data in NSString to NSImage
  • Next by Date: Re: Newbie question - is where Obj-C analog for a C++ static data members?
  • Previous by thread: Re: How do I convert data in NSString to NSImage
  • Next by thread: Re: How do I convert data in NSString to NSImage
  • Index(es):
    • Date
    • Thread