Advertisement

ascii to string

Started by August 23, 2000 02:09 AM
1 comment, last by mattd 24 years, 4 months ago
i have a ascii code (as as integer) and i need to turn it into (or add it to the end of) a string. do i use itoa(), or what?

"The attitudes and personality of the programmer is reflected in the written program." So that's why my programs have waaaay too many bugs, aren't user-friendly, and swear when they create errors...

Ascii are size of a char, so i assume that the rest of the int is zero =) ( 0x000000XX )

so the int you got can be seen as a string =)

int i = ''A'';
char str[12] = {"test"};
strcat( str, (char*)&i );
MessageBox( NULL, str, "TEST", NULL );
Ries
Advertisement
That might work... but doesn''t it depend on whether the machine it''s running on is Big-Endian or Little-Endian? On a PC (Little-Endian), the first byte stored will be the low byte, which is the character, and then comes the null-termination, so it works. But I think that on a Big-Endian machine, your code would leave the string unchanged, because it would see (char*)&i as an empty string. Or maybe I''m just talking nonsense and don''t know what I''m talking about.

In any case, here''s how I would do it. Let''s say your ASCII code is stored in an integer called x, and you want to add the character whose ASCII code is x to the end of a string called text.

int len = strlen(text); // get string length
text[len] = (char)x; // add the character
text[len + 1] = 0; // null-terminate

-Ironblayde
Aeon Software
"Your superior intellect is no match for our puny weapons!"

This topic is closed to new replies.

Advertisement