将字符串转换为有符号整型

发布于 2024-09-24 13:30:02 字数 177 浏览 9 评论 0原文

我想将字符串转换为有符号整数。以下是要求。我已将十六进制值作为字符串存储在缓冲区中。现在我想将该值转换为有符号整数。

buf = "fb869e" 将其转换为有符号整数。所以 o/p 应该是 -293218。但是当我尝试使用 strtol 进行转换时,我得到 16483998。那么我应该做什么?

I want to convert a string into a signed int. Following is the requirement. I have stored hex value as a string in buffer. Now I want to convert that value into signed int.

buf = "fb869e" Convert this into signed int. So o/p should be -293218. but when I'm trying to convert using strtol I'm getting 16483998. So what I should I do?

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

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

发布评论

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

评论(7

So尛奶瓶 2024-10-01 13:30:02

十六进制数0xfb869e不是负数。内置数字转换函数不会将其转换为负值,因为它的值为正数。

您所说的是,这是 24 位 2 补码负数的无符号十六进制等价物,并且您想要该数字。获得该值的方法是将其转换为正数,然后使用计算将其转换为等价的 24 位 2 补码:

char *buf = "fb869e";
long n;

n = strtol(buf, NULL, 16);
if (n > 0x7fffffL)
    n -= 0x1000000L;

The hexadecimal number 0xfb869e is not negative. The inbuilt number conversion functions will not convert it to a negative value, since its value is positive.

What you are saying is that this is the unsigned hexadecimal equivalent of a 24-bit 2s complement negative number, and you want that number. The way to get that is to convert it to the positive number, then use calculations to convert it to the 24-bit 2s complement equivalent:

char *buf = "fb869e";
long n;

n = strtol(buf, NULL, 16);
if (n > 0x7fffffL)
    n -= 0x1000000L;
も让我眼熟你 2024-10-01 13:30:02

其他人建议使用strtol()。我只想提到 sscanf() 作为替代方案,例如:

int i;
char *buf = "fb869e";
if (sscanf(buf, "%x", &i) == 1)
   ...

Others have suggested strtol(). I just want to mention sscanf() as an alternative, eg:

int i;
char *buf = "fb869e";
if (sscanf(buf, "%x", &i) == 1)
   ...
蓝海似她心 2024-10-01 13:30:02

strtol 将字符串转换为长整数

输出是正确的,它将是 16483998

并且如果您使用 atoi,当它将字符串转换为整数时,如果超出可表示值的范围,则返回正确的值,INT_MAX 或 INT_MIN 。

strtol converts string to long integer

The output is correct and it will be 16483998

And if you use atoi, while it converts to string to integer, the correct value if out of the range of representable values, INT_MAX or INT_MIN is returned.

清风夜微凉 2024-10-01 13:30:02

为什么 0x00fb869e 应该是负数?您应该必须提供数字系统的基础,甚至可以判断一种格式的值在另一种格式中是否为负数

why should 0x00fb869e be a negative number? you should have to provide the base of your number system, to even be allowed to tell, whether a value in one format is a negative in another format

客…行舟 2024-10-01 13:30:02
0xfb869e == 0x00fb869e == 16483998

作为有符号整数,必须设置高位才能产生负数。由于给定数字的高位未设置,因此它必须是正数。

如果您希望该数字被视为 24 位数字,则必须将第 23 位填充到剩余的高位。这是一种方法:

long n = strtol(...);
if (n > 0xffffff) n |= 0xff000000;
0xfb869e == 0x00fb869e == 16483998

As a signed integer, the high bit must be set to produce a negative number. Since the high bit of the given number isn't set, it must be positive.

If you want the number to be treated as a 24-bit number, you'll have to pad bit 23 out to the remaining high-bits. Here's one way to do it:

long n = strtol(...);
if (n > 0xffffff) n |= 0xff000000;
梦里兽 2024-10-01 13:30:02

使用 strtolstring 转换为 long 整数。

Use strtol which converts string to a long integer.

情深如许 2024-10-01 13:30:02

尝试下面的代码块,它对我有用。

char *p = "0x820";
uint16_t intVal;
sscanf(p, "%x", &intVal);

printf("value x: %x - %d", intVal, intVal);

Output is:

value x: 820 - 2080

Try below block of code, its working for me.

char *p = "0x820";
uint16_t intVal;
sscanf(p, "%x", &intVal);

printf("value x: %x - %d", intVal, intVal);

Output is:

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