hi everyone...
I have an array of chars:
ifname[50]
in this array i can have anything, letters, numbers, special characteres...
i have to get the ascii code of the char as a integer...
i cannot figure out how to do this...
i tried a lot of things...all went wrong:
sprintf(m1,"%i",ifname[i]); gives me segmentation fault
1 2
m1 = ifname[i];
ch = (int)m1;
gives me strange numbers, such as -1564789432
its very simple, but i have run out of ideas
anyone can help me?
chars are just integers. You don't need to do anything special to get their values:
1 2 3
char s[]="Hello, World!\n";
int ascii=s[0];
std::cout <<ascii<<std::endl;
I can't imagine how you could have gotten -1564789432 from casting a signed 8-bit integer to a bigger signed integer. It just shouldn't happen. At worst, you should have gotten, say, -128 from a 128 character.