convert hex into decimal number??(Type casting)

Is there single line syntax for converting hex into decimal number??(Type casting)

unsigned char* data_1;
int a;
data_1[0] = 0x01; //contains hex value
a = data_1[0]; //want to convert hex value in decimal
Are you sure you actually need to do any conversion?
consider this:
1
2
3
4
int a = 0xD431;
int b = 54321;
cout << "a: " << a << endl;
cout << "b: " << b << endl;

Output:
a: 54321
b: 54321

a and b are unrelated integer values.
When the value is output, we see it in decimal.
But internally they are both stored in binary. There is no need to explicitly convert anything in this example.

By the way, the original code has an uninitialised pointer data_1. Since this does not point to a valid address, attempting to assign data to it will give rise to unpredictable results.
Last edited on
The words hex and decimal have no sense when applied to variables. They have meaning only when you are printing variable or reading it - i.e. when you interchange value with user.

Here are functions like "itoa" which allow convert data shortly enough. "sprintf" and "sscanf" also will do much good for complex formatting.

http://www.cplusplus.com/reference/cstdlib/itoa/

If you feel uncomfortable about all this mess with numeral systems, perhaps this article from my site could help you a bit:

http://codeabbey.com/index/wiki/number-to-digits
Topic archived. No new replies allowed.