Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hiddenpremise

macrumors regular
Original poster
I am giving myself a headache over a seemingly simple problem
I have an NSString containing hex values "0x74 0x68 0x69 0x73 0x20 0x69 0x73 0x20 0x61 0x20 0x73 0x74 0x72 0x69 0x6e 0x67"

I convert that to array NSArray * myArray = [myString componentsSeparatedByString:mad:" "];

I then want to convert each of those strings to an int value.

How do I get from "0x73" to 115 to "s"?

I have tried calling [[myArray objectAtIndex:i] integerValue]; but that doesn't seem to work

Solved. Thanks for your help everyone!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I don't have time to write the code, but %x is the format specifier for hex. With C functions you could do the conversion, I'm not sure if there are similar things available for NSNumber, NSString, etc.

-Lee
 

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,422
A sea of green
I am giving myself a headache over a seemingly simple problem
I have an NSString containing hex values "0x74 0x68 0x69 0x73 0x20 0x69 0x73 0x20 0x61 0x20 0x73 0x74 0x72 0x69 0x6e 0x67"

I convert that to array NSArray * myArray = [myString componentsSeparatedByString:mad:" "];

I then want to convert each of those strings to an int value.

How do I get from "0x73" to 115 to "s"?

I have tried calling [[myArray objectAtIndex:i] integerValue]; but that doesn't seem to work
Break the problem down.

You've already broken it down once, by splitting the string into multiple strings. Break it down again until you have something you know how to solve.

Look at the NSScanner class reference docs, and see if it has anything for hex numbers.

integerValue can't possibly work, because it only decodes decimal representations (i.e. base 10), and hexadecimal is base 16. Read the reference doc for that NSString method.

Also, "0x" is only a marker, and not actually part of the numeric value. So one part of breaking it down is going to be skipping the "0x" part.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I don't have time to write the code, but %x is the format specifier for hex. With C functions you could do the conversion, I'm not sure if there are similar things available for NSNumber, NSString, etc.

-Lee

As Objective-C is a superset of C you could just use the C functions.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
You don't need to create a NSScanner object to acomplish this.

Code:
int n;
	NSString *s = @"0x544";
	sscanf([s UTF8String], "%x", &n);
n will be a regular int that you can put into an NSNumber if need be.

Even better.

Code:
#import <Cocoa/Cocoa.h>

@interface NSString (HexValue) 
-(int) hexValue;
@end


@implementation NSString (HexValue)
- (int) hexValue {
	int n = 0;
	sscanf([self UTF8String], "%x", &n);
	return n;
}
@end

then just call [(NSString) *)s hexValue] and it will return the int!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.