简体   繁体   English

如何在C++中将十六进制值转换为整数?

[英]How to convert hexadecimal value to integer in c++?

I am having the hexadecimal value in unsigned char and i am not finding any way to change this hexadecimal value as integer.我在 unsigned char 中有十六进制值,但我找不到任何方法将此十六进制值更改为整数。 Please assist on this.请对此提供帮助。

unsigned char c = '0x01';
int convertasint = (int)c;//not working
int convertasint1 = (atoi)c;//not working

Please guide me how to achieve this.请指导我如何实现这一目标。

unsigned char variables are integers already (so are char ). unsigned char变量已经是整数( char )。 You don't have to do anything special to convert them.您无需执行任何特殊操作即可转换它们。 This code works此代码有效

unsigned char c = 0x01;       // assign integer to unsigned char
int convertasint = c;         // now assign to int
cout << convertasint << '\n'; // this prints 1

Also please note that hexadecimal integers are just integers, just like decimal integers.另请注意,十六进制整数只是整数,就像十进制整数一样。 You also don't need to convert between them.您也不需要在它们之间进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM