How to convert variable unsigned char with HEX value to variable int with DEC value. I have got:
unsigned char length_hex = 0x30; // HEX
How to convert to int DEC?
int length_dec = length_hex; // With result length_dec = 48
As some people have commented the conversion is automatic. Try:
int main (void)
{
unsigned char length_hex = 0x30;
printf("0x30 is char:%c and int:%d.",length_hex,length_hex);
return 0;
}
and see what you get - when you convert it to a char it's '0' (the character 0) and as an int it's 48. Also, it doesn't really matter if I save the constant 0x30 as an int or char. Just look at:
printf("0x30 is char:%c and int:%d.",0x30,0x30);
Related
I have the following piece of code:
const unsigned char *a = (const unsigned char *) input_items;
input_items is basically the contents of a binary file.
Now a[0] = 7, and i want to convert this value to unsigned int. But when I do the following:
unsigned int b = (unsigned int) a[0];
and print both of these values, I get:
a[0] = 7
b = 55
Why does b not contain 7 as well? How do I get b = 7?
I think I see the problem now: You print the value of the character as a character:
unsigned char a = '7';
std::cout << a << '\n';
That will print the character '7', with the ASCII value 55.
If you want to get the corresponding integer value for the digit character, you can rely on that all digit characters must be consecutively encoded, starting with zero and ending with nine. Which means you can subtract '0' from any digit character to get its integer value:
unsigned char a = '7';
unsigned int b = a - '0';
Now b will be equal to the integer value 7.
I have this piece of code:
#include <stdio.h>
#include <iostream>
int main(int n, char** args)
{
int i = 140;
char c = i;
int j = c;
printf("%d", j);
system("pause");
}
Output:
-116
As I understand, char c = i would assign the character which has ASCII code 140 to c, but I wonder what happened when assigning int j = c; after that and where does -116 value come from?
if I change char c = i; to unsigned char c = i; then the output is 140.
Is there anyway to get the value of j equal to 140 if I don't use unsigned here? (assumed that i >= 0 and i <= 255)
The range of values for an unsigned char are [0,255] inclusive - but the range of values for a signed char are [-128,127] inclusive.
Your value of 140 is outside the range of the signed char, so it overflows and wraps round to -116. At least that's what seems to be happening - overflow of a signed integer is undefined behaviour.
There's not really any way to avoid this without using unsigned char or a larger alternative to char - you're simply trying to represent a value that can't be represented by that type.
I just want to know how to convert an hexadecimal value contained in a char (byte) into an integer. I want to convert the color buffer from a .bmp file which is of course in hexadecimal and convert it in integers.
For example :
char rgb_hexa[3] = {0xA8, 0xF4, 0xD3};
After conversion :
int rgb_int[3] = {168, 244, 211};
I always tried to use strtol but it seems to only works with char *. I tried to do the following test but it does not work :
char src_hexa_red = 0xA8;
char src_hexa_green = 0xF4;
char src_hexa_blue = 0xD3;
std::cout << "R=" << strtol(&src_hexa_red, (char**)NULL, 16) << ", G="
<< strtol(&src_hexa_green, (char**)NULL, 16) << ", B="
<< strtol(&src_hexa_blue, (char**)NULL, 16) << std::endl;
Does anyone can help me please ?
Thanks in advance for your help.
A single char never contains hexadecimal. Nor decimal, for
that matter. Strictly speaking, a char contains an integral
value; the C++ standard requires it to use a binary
representation for the value. The value can be interpreted as
a character, but this is not always the case; there are contexts
where the integral value is used directly.
Hexadecimal and decimal are just ways of representing the value
in text format. They only have meaning when dealing with text.
for(int i = 0; i < 3; ++i)
rgb_int[i] = (unsigned char)rgb_hexa[i];
char is an integer type in C & C++ just like short, int and long. It's just the smallest integer type. Mostly, char is signed & the maximum which can fit is 127. So if the hex value was below or equal to 127, you wouldn't have to do anything. However, in this case the hex values you have are > 127 - hence you would have to cast them to unsigned to get the value you want.
Note that both the statements are identical to the compiler.
char rgb_hexa[3] = {0xA8, 0xF4, 0xD3};
char rgb_hexa[3] = {168, 244, 211};
You could have even used octal if you wanted
char rgb_hexa[3] = {0250, 0364, 0323};
It's all the same.
The values in the char array are already in a binary form, so you can cast them to an int, if you need them as such.
int v = (int)rgb_hexa[0];
You should be aware though that using signed char they will be sign extendend.
So 0xFA becomes 0xFFFFFFFA when converted to an int.
If you want to keep the values then you should use unsigned char and unsigned int which makes it 0x000000FA depending on how you want to use the values.
int v0 = (int)a[1]; <-- sign extended
unsigned int v1 = (unsigned int)a[1]; <-- sign extended
unsigned int v1 = (unsigned int)((unsigned char *)a)[1]; <-- not sign extended
You don't need to do any conversion because hexa/decimal are just ways to represent values.
For example 0xA8 in hexadecimal is the same value as 180 in decimal and 250 in octal. As in languages for example, "two", "deux" and "dois" represent all the same number (2).
In your case if you want to print the values do the following:
short y = (short) x & 0x00FF; // x is the char you want to print
cout << "Value (decimal): " << y;
cout << "Value (hexa): " << hex << y;
cout << "Value (oct): " << oct << y;
Why can't you do this
int main(int argc, char *argv[])
{
char rgb_hexa[3] = {0xA8, 0xF4, 0xD3};
int rgb_int[3] = {0,};
int i = 0;
for( i = 0 ; i < 3 ;i++)
rgb_int[i] = (unsigned char)rgb_hexa[i];
for( i = 0 ; i < 3 ;i++)
printf("%d ",rgb_int[i]);
return 0;
}
pretty straight forward ..
For type conversion, there is static_cast:
unsigned char source = 168; // note that this has for compiler same meaning as:
// unsigned char source = 0xA8; // because data is stored in binary anyway
unsigned int dest = static_cast<int>(source); // the conversion
std::cout << source << std::endl;
dest and source have same binary meaning, but they are of a different type.
I've used unsigned types, because signed char stores usually values from -127 to 127, see limits.
I have a char '╞' with ASCII value (198). How I can manipulate this char such that the resulting char will be one of the following?
0 to 9 and A to F
Later on, I want to convert the resulting char back into the original char i.e. '╞'
I guess this is a question about decimal to hex conversion. I would do it like this
char dec = 198;
char hex[3];
sprintf(hex, "%02X", (unsigned char)dec);
To convert back it would be
int tmp;
sscanf(hex, "%X", &tmp);
dec = tmp;
There's other ways, using std::stringstream for instance, but the above is fine by me.
I have a variable unsigned char that contains a value, 40 for example.
I want a int variable to get that value.
What's the simplest and most efficient way to do that?
Thank you very much.
unsigned char c = 40;
int i = c;
Presumably there must be more to your question than that...
Try one of the followings, it works for me. If you need more specific cast, you can check Boost's lexical_cast and reinterpret_cast.
unsigned char c = 40;
int i = static_cast<int>(c);
std::cout << i << std::endl;
or:
unsigned char c = 40;
int i = (int)(c);
std::cout << i << std::endl;
Google is a useful tool usually, but the answer is incredibly simple:
unsigned char a = 'A'
int b = a
Actually, this is an implicit cast. That means that your value is automatically casted as it doesn't overflow or underflow.
This is an example:
unsigned char a = 'A';
doSomething(a); // Implicit cast
double b = 3.14;
doSomething((int)b); // Explicit cast neccesary!
void doSomething(int x)
{
...
}
Depends on what you want to do:
to read the value as an ascii code, you can write
char a = 'a';
int ia = (int)a;
/* note that the int cast is not necessary -- int ia = a would suffice */
to convert the character '0' -> 0, '1' -> 1, etc, you can write
char a = '4';
int ia = a - '0';
/* check here if ia is bounded by 0 and 9 */
char *a="40";
int i= a;
The value in 'a' will be the ASCII value of 40 (won't be 40).
Instead try using strtol() function defined in stdlib.h
Just be careful because this function is for string. It won't work for character