Objective-C Converting an integer to a hex value
Asked Answered
L

3

15

I've got a dictionary initialized like so...

keyDictionary = [[NSDictionary dictionaryWithObjects:values forKeys:keys]retain];

where keys is an NSArray of the alphabet and other characters and values is an NSArray of unsigned chars, which are the USB hex keycodes for those characters.

The USB key codes are hex values that range from 0x04 to 0xE7. I'm trying to create a map between these two depending on what key is pressed on the keyboard.

The values array is created like so...

NSArray *values = [NSArray arrayWithObjects: 
                  [NSNumber numberWithUnsignedChar:0x04]/*A*/,
                  [NSNumber numberWithUnsignedChar:0x05]/*B*/, /*ETC*/]; 

So ideally when I run this code... where character == @"I"

- (uint8) getUSBCode:(NSString *)character
{
    NSNumber *val = [keyDictionary objectForKey:character];

    return (uint8)[val unsignedCharValue];
}

I would expect to get back 0x0C, but I'm getting 12 back as an int (which after I thought about it, makes sense). I need the hex value preserved. I do NOT need a string value. I need a straight conversion to the hex value or a better way to store

uint8 is just a typedef unsigned char.

EDIT I was not clear when I posted this earlier. Here's what I need.

I need the hex value of these codes because they are being sent over the internal company network. In addition, the pressed key's value is being converted from big endian (or little, it's escaping me right now which one it is) to the other, then being transmitted over an internal network. I understand that these values are stored in binary, but I need to transmit them in hex.

Also, I stated I was getting 12 back from the function. I was reading 12 from the debugger, not actually getting the value. That might be why I was getting confused.

Logician answered 29/3, 2011 at 14:10 Comment(2)
12 decimal and 0x0C are the same number. You could try comparing the return value with 0x0C and see if they're the same. i.e. if(retval == 0x0c) NSLog(@"They're the same"); else NSLog(@"They're not the same"); The bits that get passed down the USB serial pipe will be the same with decimal 12 or hex C: ...00001100Conner
You're all technically right. I was being a total moron when I asked this. For some reason I assumed that I HAD to send the codes over as hex, but in the end, we are essentially sending binary.Logician
M
55

12 (in base 10) is 0x0c.

If you want to print it out in hex, use the %x format specifier e.g.

NSLog(@"Hex value of char is 0x%02x", (unsigned int) c);

If you want to see it in hex in the debugger (assuming Xcode 3.2.x) right click on the variable and select hexadecimal as the format.

Modification answered 29/3, 2011 at 14:30 Comment(0)
L
0

You know that an int is stored in binary (i.e. the 'hex' value is always and never preserved), so I'm interpreting your question as pertaining to printing to the screen.
You should be able to use a format specifier for that -- something like %0x.

Linguist answered 29/3, 2011 at 14:19 Comment(0)
B
0

The value that's returned from your -getUSBCode: method isn't two decimal digits, it's one eight-bit byte. Both "12" and "0x0C" are strings that represent that byte's value, so saying you want "0x0C" but don't want a string is a contradiction.

Basting answered 29/3, 2011 at 14:20 Comment(1)
You are correct. I guess I wasn't very clear in my question. Going to edit it now.Logician

© 2022 - 2024 — McMap. All rights reserved.