Click to See Complete Forum and Search --> : Unprintable character codes and char*


nulloverflow
October 19th, 2004, 08:14 AM
For those of you who use mIRC, you know when you press ctrl+k you can select a color and your text will appear in that color. What mIRC actually does is prepend the text with a single char, then the color numbers and text. The character prepended is character 3 (from the ASCII table). Since this char is unprintable it shows up as a little box in mIRC (depending on font). My question is this: How can i copy an unprintable character (like chr(3)) to a char*.

Here is a little snippet that causes my DLL to crash mIRC:
------------------------------------------------------
1: char colorcode = (char) 3; // char 3 = ctrl+k
2: strcpy(data,colorcode); // data is a var defined elsewhere
3: strcat(data,CC_BLUE) // CC_BLUE is defined elsewhere as "02";
4: strcat(data,text) // text containts...um..text
5: AppendWindowBuffer(hWnd,data) // Append the window
------------------------------------------------------
If i comment out line 2, and change line 3 to strcpy the program runs with no problems, but if i leave it as is, it crashes when it try to copy colorcode.

Any/All help is appreciated.
-James

TheCPUWizard
October 19th, 2004, 08:20 AM
First, this is ALOT easier (and better) is you will use STL (or other) string objects.

You can not (meaningfully) copy a char to a char *. You CAN copy a char to a character position that is being pointed to by a char *, but you need to make sure of a number of things.

1) The location being pointed to must be valid and properly allocated.
2) You must make sure that there is a null character (0x00) at a valid location somewhere after the character you are storing.

There is no difference between poerations using a printable character than an un printable character, the only limitation is that you can not store a null in a buffer area that is being references by a char * (as that will act as a termination for the string).

NoHero
October 19th, 2004, 09:10 AM
1: char colorcode = (char) 3; // char 3 = ctrl+k
2: strcpy(data,colorcode); // data is a var defined elsewhere
3: strcat(data,CC_BLUE) // CC_BLUE is defined elsewhere as "02";
4: strcat(data,text) // text containts...um..text
5: AppendWindowBuffer(hWnd,data) // Append the window


At first TheCPUWizard gave the ultimate answer, but I want to post the correct code for this solution you have:


BYTE colorCode[2], k[2];

colorCode[0] = CC_BLUE;
colorCode[1] = '\0'; // like here ...
k[0] = (char)3;
k[1] = '\0'; // ... and here you need a trailing null character

strcpy(data, k);
strcat(data, colorCode);
strcat(data, text);
AppendWindowBuffer(hWnd, data);