I have a char variable that I want convert into an int variable. I thought of using atoi
function, but I think that is for the char*
pointer type.
Is there any smart way to changing a character to an integer in the C programming language?
I have a char variable that I want convert into an int variable. I thought of using atoi
function, but I think that is for the char*
pointer type.
Is there any smart way to changing a character to an integer in the C programming language?
First lets say we have declared a char value as here:
char someChar = '5';
Okay if you simply want to change the type from char to int then you would do something like this
atoi(&someChar);
However note that this simply changes the type from char to int. It will not convert a letter value to the ASCII number that represents it. So like the letter a
would really be 97
, but I do not think if you used the code above it would give you 97
. It would probably make the type an int, but the value 0
.
Anyway if you want to actually convert the letter to the number that represents it in ASCII terms you would simply do:
int(someChar);
Oops, I was wrong, looks like atoi
does work. I got it wrong the first time because I forgot the &
address operator.
Do you know how to concat a single char to the end of a char array? I tried strcat
but seems like it's not working. I get the following error message:
warning: passing arg 2 of `strcat' makes pointer from integer without a cast
Here is how I declared the character array and other variables:
unsigned char a[256], b, c
I want to concat b
and c
into a
. I attempted to do this with the following C code:
strcat(a, b);
strcat(a, c);
When I do that, the above error is thrown.
I just found out there is a problem with using atoi
. I used:
n = atoi(&c);
where c
is a variable declared as:
char c[256];
but then when I printf
the n
, it turns out it is 0
. It's of ZERO value! How could that be? Actually I want to get the value of that char
, like a char 0x80
. I want to get the value int 8 in decimal. You have any idea?
Yup re-read what I said above. You will see I already explained that about the character turning to 0. Goto the top and re-read everything 😁
o...yes everything's alright now
cool thanz !! 😁
Can you tell me how can I use the int(someChar);
code above using ANSI C?
I want to convert, for example a character A
to its ASCII decimal integer number with the value of 65
.
Finally, i've found the answer to what i was looking for.
char dest = 'a';
int c = (int)dest;
and c
has the ASCII value of a which is 97 dec
or 61 hex
, but this will work only for one character. Does anyone knows how to do this for a string? For example for the string abc
we should have an integer with the value 616263 hex
?
You mean the value 0x414243
? Or the value 0x434241
?
For the string abc
, the values are stored as {'a','b','c',0}
in the array. So you could use:
char* x = "abc";
int k;
k = *(int*)x; /* assigns 0x434241 on little-endian machines with 32-bit ints */
That's a pretty bad way to do it, though, since it's extremely non-portable. (If you use it on a big-endian machine, you'll get 0x41424300.) And I don't even think it's the value you want.
To get the value 0x414243 from a string like abc
, you could write a little function that looks at each character:
unsigned int weird_int_value(char* s) {
int ret = 0;
ret = s[0] * 65536;
ret = ret + s[1] * 256;
ret = ret + s[2];
return ret;
}
The problem with this is, it only works on three-character strings. Do you want the same function to take ab
and return 0x4142
?
The problem with this in general is that it could only possibly work up to 4-byte strings (assuming sizeof(int)
is 4 bytes, which it usually is), so it seems like a kind of silly thing to want to do.
So with all of that said, what exactly are you trying to do?
I think what stzo wants is to take a character array and convert each of the character in the array into it's integer equivalent and concat those values together.
An example of what I think he wants to do:
main()
{
char[2] input='abc';
for(int trav=0;trav < 2;trav++)
cout << (int)dest[trav];
}
Something to that affect.
The thing that I want to do is to convert a char = 'abc'
to int = '0x616263'
, which is something like sevster mentioned above.
I am receiving from the serial port some bytes that are stored into a char
, and then put the equivalent value of the char's characters concatenated together in an int
so as to use this to a CGI file. However, I manage something with the a = (int)char[0]
but it works only for the the char[0]
.