将int(32位)转换为char(8位) [英] Convert int (32 bits) to char (8 bits)

查看:850
本文介绍了将int(32位)转换为char(8位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些定义:

int data = uartBaseAddress[UART_DATA_REGISTER / 4]; // data coming from UART RX port
char message[20]; // array of 20 chars

现在当我尝试这样做时:

Now when I try to do this:

message[0] = (char) data;
printf("%x", message[0]);

它打印(例如):ffffff9c
当然我只想要最后8位(9c),我不明白如何正确地从 int 转换为 char

It prints (for example): "ffffff9c". Of course I want only the last 8 bits ("9c") and I don't understand how to properly do the conversion from int to char.

编辑:我的意思是:我必须像这样填充数组:

I mean: i have to populate the array like this:

data = 0xFFFFFF9c;
message[0] = data & 0xFF; -- it has to contain only 9c
data = 0xFFFFFFde;
message[1] = data & 0xFF; -- it has to contain only de
etc...


推荐答案

转换是正确的。这是 printf 这就是问题所在。

The conversion is correct. It's the printf that's the problem.

显然是普通的 char 已在您的系统上签名(可以是签名或未签名)。

Apparently plain char is signed on your system (it can be either signed or unsigned).

我打算猜测打印的值是 ffffff9c (8位数),不是 ffffffff9c (10位数);请验证。

I'm going to guess that the value printed was ffffff9c (8 digits), not ffffffff9c (10 digits); please verify that.

数据的值可能是 -100 。将该值从 int 转换为 char 将产生 -100 ,因为该值在 char 类型的范围内(可能 -128 .. + 127 )。

The value of data was probably -100. Converting that value from int to char would yield -100, since that value is within the range of type char (probably -128 .. +127).

%x 格式说明符需要类型为<的参数code> unsigned int ,而不是 int message [0] 的值在传递给 printf时被提升为 int ,但 printf ,由于格式字符串,假定参数的类型为 unsigned int

But the %x format specifier requires an argument of type unsigned int, not int. The value of message[0] is promoted to int when it's passed to printf, but printf, because of the format string, assumes that the argument is of type unsigned int.

严格来说,行为未定义,但很可能 printf 将简单获取传递给它的 int 值,并将其视为 unsigned int (int)-100 (unsigned int)0xffffff9c 表示相同。

Strictly speaking, the behavior is undefined, but most likely printf will simply take the int value passed to it and treat it as if it were an unsigned int. (int)-100 and (unsigned int)0xffffff9c have the same representation.

没有 printf 格式说明符来打印十六进制的有符号值。如果您将格式从%x 更改为%d ,您至少会看到正确的值。

There is no printf format specifier to print a signed value in hexadecimal. If you change the format from %x to %d, you'll at least see the correct value.

但你应该退一步决定你想要完成什么。如果要提取数据的低8位,可以通过屏蔽它来实现,如放松的回答表明。或者您可以将其转换为 unsigned char 而不是普通的 char ,以保证您将获得无符号值。

But you should step back an decide just what you're trying to accomplish. If you want to extract the low-order 8 bits of data, you can do so by masking it, as unwind's answer suggests. Or you can convert it to unsigned char rather than plain char to guarantee that you'll get an unsigned value.

unsigned char 值仍然提升为 int 时传递给 printf ,所以为了完全正确,你应该明确地将它转换为 unsigned int

An unsigned char value is still promoted to int when passed to printf, so to be fully correct you should explicitly convert it to unsigned int:

int data = ...;
unsigned char message[20]; // change to unsigned char

message[0] = data;  // no cast needed, the value is implicitly converted
printf("%x", (unsigned int)message[0]);

严格来说,(unsigned int)没有必要。 message [0] unsigned char ,因此它将被转换为非负 int value,并且有一个特殊情况规则,表示 int unsigned int 具有相同值的参数可作为函数参数互换。尽管如此,我自己的偏好是使用强制转换,因为它更清晰,并且因为添加强制转换更容易(特别是对于阅读代码的任何人)而不是遵循说不必要的推理线。

Strictly speaking, the (unsigned int) isn't necessary. message[0] is an unsigned char, so it will be converted to a non-negative int value, and there's a special-case rule that says int and unsigned int arguments with the same value are interchangeable as function arguments. Still, my own preference is to use the cast because it's clearer, and because adding the cast is easier (particularly for anyone reading the code) than following the line of reasoning that says it's not necessary.

这篇关于将int(32位)转换为char(8位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆