-2

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.

sivanesan1
  • 779
  • 4
  • 20
  • 44
  • 2
    It seems you need to spend some time to learn the very basics of C++. Please get [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take a couple of classes. – Some programmer dude Nov 09 '20 at 07:21
  • is it possible to convert from char to decimal – sivanesan1 Nov 09 '20 at 07:29
  • `'0x01'` is a*multi-character* constant. How they are handled is *implementation defined*. If you want a single character then use `'\x01'`, which has the actual integer value `1`. If you want a *string* containing literally `0x01` then you need e.g. `std::string c = "0x01";` – Some programmer dude Nov 09 '20 at 07:32
  • @sivanesan1 It's possible to convert from char to anything. But that wasn't what you originally asked. Please be clear what you are asking about. – john Nov 09 '20 at 07:34
  • @sivanesan1 chars, strings and integers are all different things, which can take many different values. Unless you are clear about precisely what you want to convert to what, you are going to get unhelpful answers. – john Nov 09 '20 at 07:36

1 Answers1

0

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.

john
  • 85,011
  • 4
  • 57
  • 81