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 char
s, which are the USB hex keycodes for those characters.
其中keys是字母表的NSArray,其他字符和值是NSArray的无符号字符,它们是这些字符的USB十六进制密钥代码。
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.
USB密钥代码是十六进制值,范围从0x04到0xE7。我正在尝试根据键盘上按下的键创建这两者之间的地图。
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"
理想情况下,当我运行此代码时......其中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
我希望能够回到0x0C,但是我作为一个int得到了12(在我想到它之后,这是有意义的)。我需要保留十六进制值。我不需要字符串值。我需要直接转换为十六进制值或更好的存储方式
uint8
is just a typedef unsigned char.
uint8只是一个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.
我需要这些代码的十六进制值,因为它们是通过内部公司网络发送的。此外,按下的键的值正在从big endian(或者很少,它正在逃避我现在的哪一个)转换到另一个,然后通过内部网络传输。我知道这些值存储在二进制文件中,但我需要以十六进制格式传输它们。
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.
另外,我说我从功能中退了12。我从调试器中读取12,而不是实际获得该值。这可能就是我感到困惑的原因。
3 个解决方案
#1
47
12 (in base 10) is 0x0c.
12(在基数10中)是0x0c。
If you want to print it out in hex, use the %x
format specifier e.g.
如果要以十六进制打印出来,请使用%x格式说明符,例如
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.
如果你想在调试器中看到十六进制(假设Xcode 3.2.x),请右键单击变量并选择十六进制作为格式。
#2
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
.
你知道int是以二进制形式存储的(即'hex'值始终是永远保留的),所以我将你的问题解释为与打印到屏幕有关。您应该可以使用格式说明符 - 例如%0x。
#3
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.
从-getUSBCode:方法返回的值不是两位十进制数字,而是一个八位字节。 “12”和“0x0C”都是表示该字节值的字符串,所以说你想要“0x0C”但不想要字符串是矛盾的。
#1
47
12 (in base 10) is 0x0c.
12(在基数10中)是0x0c。
If you want to print it out in hex, use the %x
format specifier e.g.
如果要以十六进制打印出来,请使用%x格式说明符,例如
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.
如果你想在调试器中看到十六进制(假设Xcode 3.2.x),请右键单击变量并选择十六进制作为格式。
#2
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
.
你知道int是以二进制形式存储的(即'hex'值始终是永远保留的),所以我将你的问题解释为与打印到屏幕有关。您应该可以使用格式说明符 - 例如%0x。
#3
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.
从-getUSBCode:方法返回的值不是两位十进制数字,而是一个八位字节。 “12”和“0x0C”都是表示该字节值的字符串,所以说你想要“0x0C”但不想要字符串是矛盾的。