Re: Int Val of Hex From NSData
Re: Int Val of Hex From NSData
- Subject: Re: Int Val of Hex From NSData
- From: Christian Longshore Claiborn <email@hidden>
- Date: Mon, 10 Mar 2003 16:03:18 -0800
First, an [NSData intValue] or whatever would not have enough
information to do proper integer encoding. Is "AB9" base 13, base 16
or base 20? Are you asking it to convert the numeric values of the
bytes or the ASCII-encoded hexidecimal equivalents? What do you do
when you overload (when you get a value too big for a 32-bit int)?
Second, there are many existing ways to read a hex value into an
integer. You could use sscanf with the "%x" format specifier, for
example.
int value;
sscanf("AB9", "%x", &value);
NSLog(@"%d", value);
Someone better with Foundation may have suggestions for a function that
you can call in an Objective-C message (something with NSScanner,
maybe).
You can also, of course, convert such a thing trivially by iterating
through from the beginning of the string to the end. The value for
each hexit is merely bytes[i] - 48 (if it's between '0' and '9') and
bytes[i] - 55 (if it's between 'A' and 'F'). Once you've got the value
for that hexit, you can shift up four bytes (multiplying by sixteen)
and continue on.
int hexValue(char *incoming)
{
int accumValue = 0;
int i = 0;
for (i = 0; i < strlen(incoming); i++)
{
accumValue = accumValue << 4;
if (incoming[i] >= '0' && incoming[i] <= '9')
{
accumValue += incoming[i] - 48;
}
else if (incoming[i] >= 'A' && incoming[i] <= 'F')
{
accumValue += incoming[i] - 55;
}
}
return accumValue;
}
Christian
On Monday, March 10, 2003, at 02:46 pm, Seth Willits wrote:
>
On Monday, March 10, 2003, at 07:32 AM, Clark S. Cox III wrote:
>
>
> On Monday, Mar 10, 2003, at 10:05 US/Eastern, Seth Willits wrote:
>
>
>
>> If I have an NSData object that has three bytes in it (A9B), how can
>
>> I get the integer equivalent (2715)?
>
>
>
> I'm assuming that you actually mean 3 bytes (and not 3 hex characters)
>
>
>
> This should do it:
>
>
>
> const unsigned char *bytes = [nsData bytes]
>
> unsigned int value;
>
>
>
> NSAssert([nsData length] >= 3);
>
> value = bytes[0] << 16 | bytes[1] << 8 | bytes[2];
>
>
>
Seriously? Wow... I'm just very used to higher level languages then.
>
So there isn't any
>
int = [[NSData subdataWithRange:range] integerValue]
>
>
or what about:
>
>
NSString = [[NSData subdataWithRange:range] stringValue]
>
>
or
>
>
[NSData setBytes: NSData atOffset: int length: int]
>
>
>
>
Strange...
>
>
>
>
Seth Willits
>
-----------------------------------------------------------------------
>
----
>
President and Head Developer of Freak Software - http://www.freaksw.com
>
Q&A Columnist for REALbasic Developer Magazine -
>
http://www.rbdeveloper.com
>
>
"To be a great is to be misunderstood."
>
-- Ralph Waldo Emmerson
>
-----------------------------------------------------------------------
>
----
>
_______________________________________________
>
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.