• 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: Convert hexadecimal NSString (0xff00) to decimal integer?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Convert hexadecimal NSString (0xff00) to decimal integer?


  • Subject: Re: Convert hexadecimal NSString (0xff00) to decimal integer?
  • From: Charles Bennett <email@hidden>
  • Date: Tue, 07 Jan 2003 11:04:57 -0500

I wrote a general version but as a catagory to NSNumber

After adding the error checking, it seems that strtol never changes errno!
can anyone else confirm the bug or am I doing something wrong.

NSLog(@" the converted number is %@\n",[NSNumber stringToNumber:@"0xff00" base:16]);
works just fine and returns 65280 as expected.

NSLog(@" the converted number is %@\n",[NSNumber stringToNumber:@"WHAT" base:16]);
returns 0 errno =0!

#import <errno.h>
#import <limits.h>

@interface NSNumber(NSExtensions)
#define CONVERSION_FAILED @"NSNumber: Conversion to Number Failed"
#define CONVERSION_FAILED_F @"NSNumber: Can't convert: %s"
#define CONVERSION_FAILED_SMALL_F @"NSNumber: Too Small: %s"
#define CONVERSION_FAILED_LARGE_F @"NSNumber: Too Large: %s"
#define CONVERSION_FAILED_RANGE_F @"NSNumber: Out Of Range: %s"
+ (NSNumber*) stringToNumber:(NSString*) _inStr base:(int) _base;

@end

@implementation NSNumber(NSExtensions)
+ (NSNumber*) stringToNumber:(NSString*) _inStr base:(int) _base {
char buff [25];
char *end_ptr;
long tmpL = -0l;
errno = 0;
tmpL = strtol([_inStr cString], &end_ptr, _base);
if ( end_ptr == buff) {
[NSException raise:CONVERSION_FAILED
format:CONVERSION_FAILED_F, strerror(errno)];
} else if (tmpL > LONG_MAX) {
[NSException raise:CONVERSION_FAILED
format:CONVERSION_FAILED_LARGE_F, strerror(errno)];
} else if (tmpL < LONG_MIN) {
[NSException raise:CONVERSION_FAILED
format:CONVERSION_FAILED_SMALL_F, strerror(errno)];
} else if (ERANGE == errno) {
[NSException raise:CONVERSION_FAILED
format:CONVERSION_FAILED_RANGE_F, strerror(errno)];
}
return [NSNumber numberWithLong:tmpL];
}
@end



Chuck


Greg Hurrell wrote:

I was rather surprised today when I couldn't find a way of converting from an NSString containing a hexadecimal representation of a number

eg. something like "0xff00"

to a decimal integer of that number eg. 65280.

NSString has an -intValue method which can return an int value from an NSString containing a *decimal* representation of an integer. But it won't work for hexadecimal representations.

I didn't want to "re-invent the wheel" so to speak, as this must surely be a fairly common requirement. I searched around a bit, couldn't find anything in Core Foundation to do it, nor any examples on the web, so in the end I did indeed "re-invent the wheel".

So I made a category that extends NSString and adds a method called -intValueFromHexadecimal. Now tell me if all that was a waste in time... (although it was useful as a programming exercise for a learner like me at least).

Perhaps it's bleedingly obvious and staring me in the face, but I couldn't find any shortcuts in NSString or NSNumber that might do this conversion in one hit... and I find that odd because Apple obviously has code which does this (if you look at the way their code can parse plists it's evident they have plenty of tricks for converting between various types of strings and numbers etc).

Cheers :-)
Greg
_______________________________________________
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.
_______________________________________________
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: 
 >Convert hexadecimal NSString (0xff00) to decimal integer? (From: Greg Hurrell <email@hidden>)

  • Prev by Date: Re: creating an NSImage from an NSView
  • Next by Date: [BUG] PDF generation incompatible with Acrobat
  • Previous by thread: Convert hexadecimal NSString (0xff00) to decimal integer?
  • Next by thread: Re: Convert hexadecimal NSString (0xff00) to decimal integer?
  • Index(es):
    • Date
    • Thread