转换成字符串INT签署 [英] convert string into signed int

查看:169
本文介绍了转换成字符串INT签署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个字符串转换为符号整数。以下是对要求。我已存储的十六进制值作为缓冲字符串。现在我想的值转换成有符号整数。

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转换为符号整型这一点。所以,O / P应-293218。但是当我试图采用strtol我越来越转换16483998.所以我我该怎么办?

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?

推荐答案

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

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.

你所说的是,这是无符号的十六进制相当于一个24位的二进制补码负数,并且希望这个数字。来获取的方法是将其转换为正数,然后使用计算将其转换为24位二进制补码等效的:

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;

这篇关于转换成字符串INT签署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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