How to convert char into int?

  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

Hi,

OK another stupid question. I now have a char that I wanna make it int. So I thought of using atoi but I think it is for char*..of coz one can use the tedious method....declaring another variable and then change ....So, any smart way to change the type?

Thanz!! :D
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

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

Not sure if that helped.
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

o.........stupid me......yes atoi works....hehe......i got it wrong at the first place...forgot that "&" :(
thanz a lot haha :D
o...another thing...do u know how to concat a single char to the end of a char array?
i tried strcat but seems like it's not working...error message
Code: [ Select ]
warning: passing arg 2 of `strcat' makes pointer from integer without a cast

i have
Code: [ Select ]
unsigned char a[256], b, c

and i wanan concat b and c into a...
i used
Code: [ Select ]
strcat(a,b);
strcat(a.c);
  1. strcat(a,b);
  2. strcat(a.c);

and then that error message pops out........ :roll:
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

o hey wait i just found out there is a problem....
for that atoi
i used
Code: [ Select ]
n = atoi(&c);
where c is declared as
Code: [ Select ]
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 wanna get the value int 8 in decimal...u got any idea? hehe :D
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

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 :D
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

o...yes everything's alright now
cool thanz !! :D
  • stzo
  • Newbie
  • Newbie
  • stzo
  • Posts: 5

Post 3+ Months Ago

Hello!
Can you tell me how can i use the "int(someChar);" ?
I want to convert for example "A" to its ascii dec number "65".
Thanks and waiting for your help!
  • Mas Sehguh
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1853

Post 3+ Months Ago

aoeu
  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1589
  • Loc: Hants, UK

Post 3+ Months Ago

Sorry, What language we talking bout here
C#
C++
VB?
  • stzo
  • Newbie
  • Newbie
  • stzo
  • Posts: 5

Post 3+ Months Ago

I reffer to C ANSI and i just want to convert a character (i.e. "A") to its integer (i.e. "65"). Thanks
  • stzo
  • Newbie
  • Newbie
  • stzo
  • Posts: 5

Post 3+ Months Ago

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 = 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 take an integer with the value '616263' hex?
  • Mas Sehguh
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1853

Post 3+ Months Ago

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:

Code: [ Select ]
char* x = "abc";
int k;

k = *(int*)x; /* assigns 0x434241 on little-endian machines with 32-bit ints */
  1. char* x = "abc";
  2. int k;
  3. 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:

Code: [ Select ]
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;
}
  1. unsigned int weird_int_value(char* s) {
  2.  int ret = 0;
  3.  ret = s[0] * 65536;
  4.  ret = ret + s[1] * 256;
  5.  ret = ret + s[2];
  6.  return ret;
  7. }


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, what exactly are you trying to do?
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 518

Post 3+ Months Ago

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:

Code: [ Select ]
main()
{
 char[2] input='abc';

 for(int trav=0;trav < 2;trav++)
  cout << (int)dest[trav];

}
  1. main()
  2. {
  3.  char[2] input='abc';
  4.  for(int trav=0;trav < 2;trav++)
  5.   cout << (int)dest[trav];
  6. }


Something to that affect.
  • stzo
  • Newbie
  • Newbie
  • stzo
  • Posts: 5

Post 3+ Months Ago

The thing that i want to do is to convert a char='abc' to int='0x616263' something like sevster show us above. I am receiving from the serial port some bytes that are stored into a char, and the put the equivalent value of the char's characters concat 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].

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: Liamw411 and 42 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.