简体   繁体   中英

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. 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 ). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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