Objective-C 将整数转换为十六进制值

发布于 2024-10-27 15:04:24 字数 1232 浏览 0 评论 0原文

我有一个像这样初始化的字典...

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

其中 keys 是字母表和其他字符的 NSArray ,而 values 是一个 unsigned char 的 >NSArray,它们是这些字符的 USB 十六进制键码。

USB 密钥代码是范围从 0x04 到 0xE7 的十六进制值。我试图根据键盘上按下的键在这两者之间创建一个映射。

values 数组的创建方式如下...

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

所以理想情况下,当我运行此代码时... 其中 character == @"I"

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

    return (uint8)[val unsignedCharValue];
}

我希望返回 0x0C,但我得到的是 12 作为 int (在我考虑之后,这是有道理的) )。我需要保留十六进制值。我不需要字符串值。我需要直接转换为十六进制值,或者更好的存储

uint8 的方法只是 typedef 无符号字符。

编辑我之前发布此内容时并不清楚。这就是我需要的。

我需要这些代码的十六进制值,因为它们是通过公司内部网络发送的。此外,按下的键的值正在从大端(或小端,我现在不知道是哪一个)转换为另一个,然后通过内部网络传输。我知道这些值以二进制存储,但我需要以十六进制传输它们。

另外,我说过我从这次活动中得到了 12 个回报。我从调试器中读取 12,但实际上并未获取该值。这可能就是我感到困惑的原因。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清欢 2024-11-03 15:04:24

12(以 10 为基数) 0x0c。

如果您想以十六进制打印它,请使用 %x 格式说明符,例如

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

如果您想在调试器中以十六进制查看它(假设 Xcode 3.2.x),请右键单击变量并选择十六进制作为格式。

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.

人间☆小暴躁 2024-11-03 15:04:24

您知道 int 以二进制存储(即“十六进制”值始终且从不保留),因此我将您的问题解释为与打印到屏幕有关。
您应该能够使用 格式说明符< /a> ,例如 %0x

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.

他夏了夏天 2024-11-03 15:04:24

从 -getUSBCode: 方法返回的值不是两位十进制数字,而是一个八位字节。 “12”“0x0C”都是表示该字节值的字符串,因此说您想要“0x0C”但不想要字符串是矛盾的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文