Hi all,
I'll try to be brief... I am trying to convert a string (well a character
buffer) that can contain values 0 ... 255 to a pritable string (kinda a
normal C-like notation). Basically I'll be happy if I could find a system
function that would convert a string containing a NULL character in the
middle (the actual memory could look like this: 0x61 0x6c 0x00 0x61 0x6c)
into a printable form of "al\x0al" or "al\0al".
I would be even more happy if there was a routine that would take "al\0al"
and a pointer as input and would produce the required buffer and returned
it's size (not the number of character to the first NULL).
Now, I know it's reasonably easy to convert \x0 manually, but then I'll have
to do the same with \n (not \x0A), \r, \t .... and then allow the user to
specify non-printable characters as a hex number \x00 - \xFF.
I know sscanf and sprintf (snprintf) are very powerful but I couldn't make
them do it ... I tried various combinations (see below - PPS), but with no
luck. Maybe someone had already gone through this hassle and knows how to do
it in a simple manner (kind of automagically)?
Thanks
Tom
PS. I know that literal strings of the form "al\0al" are converted to
appropriate buffers during compilation, but what I need is to get this input
from a file/user and then convert it to raw buffer or alternativelly
(preferrably take a raw buffer and convert it to user form - like in the
samle code below).
PPS. sample trial code
#include <string.h>
#include<stdio.h>
#include<string>
int main(int argc, char **argv)
{
char pbuffer[1024];
const char * const buffer = "ali\0is\0tall";
size_t len = 11;
size_t i = 0;
std::string str;
printf("\n\tchar * = %s\n\tchar buffer = ",buffer);
for(i = 0; i< len; ++i) printf("%02x ",buffer[i]);
printf("\n");
// See what STL has for us
str = string(buffer,len);
printf("\tstring (str) = %s\n",str.c_str());
std::cout << "\tstring as such = " << str << std::endl;
// See if we can sacan 11 characters and convert them together
memset(pbuffer,0, 1024);
sscanf(buffer,"%11c",pbuffer);
printf("\tscanf (11c) is = %s (",pbuffer);
for(i = 0; i< strlen(pbuffer); ++i)
printf("%02x ",pbuffer[i]);
printf(")\n");
// See what happens if we sprintf 11 characters
memset(pbuffer,0, 1024);
snprintf(pbuffer,11,"%c",buffer);
printf("\tsprintf (11c) is= %s (",pbuffer);
for(i = 0; i< len; ++i)
printf("%02x ",pbuffer[i]);
printf(")\n");
// See what happens if we use snprintf to repeat the 11 char printf
memset(pbuffer,0, 1024);
snprintf(pbuffer,11,"%s",buffer);
printf("\tsprintf (11s) is= %s (",pbuffer);
for(i = 0; i< len; ++i)
printf("%02x ",pbuffer[i]);
printf(")\n");
// And try doing them one by one
memset(pbuffer,0, 1024);
for(i = 0; i< len; ++i)
sprintf(pbuffer,"%s%c",pbuffer,buffer[i]);
printf("\tloop result is = %s (",pbuffer);
for(i = 0; i< len; ++i)
printf("%02x ",pbuffer[i]);
printf(")\n");
printf("\n\n");
return argc;
Quote:}
within the e-mail):
char * = ali
char buffer = 61 6c 69 00 69 73 00 74 61 6c 6c
string (str) = ali
scanf (11c) is = ali (61 6c 69 )
sprintf (11c) is= \340 (ffffffe0 00 00 00 00 00 00 00 00 00 00 )
sprintf (11s) is= ali (61 6c 69 00 00 00 00 00 00 00 00 )
loop result is = aliistall (61 6c 69 69 73 74 61 6c 6c 00 00 )