This article is collected and collated by www.169it.com
If a C string contains both printable and non-printable characters, if you want to write the string to a file, and it is convenient to open the file view or print it in the console without garbled characters, you can convert the non-printable character in the string to 16 binary, here is a function to use:
12345678910111213141516171819202122232425262728 |
void
printhex(unsigned
char
*src,
int
len)
{
if
(src==NULL)
{
return
;
}
if
(len>(1024*1024*3-1))
{
return
;
}
char
x[1024*1024*3]={0};
int
i=0;
for
(i=0;i<len;i++)
{
char
tmp[10]={0};
if
(isprint(src[i]))
{
snprintf(tmp,8,
"%c"
,src[i]);
strcat
(x,tmp);
}
else
{
snprintf(tmp,8,
"(%X)"
,src[i]);
strcat
(x,tmp);
}
}
printf
(
"%s"
,x);
return
;
}
|
With this function, we can easily print out the contents of the binary file and analyze it in the console, if it is 16 binary, it is non-printable character, if the printable character is displayed directly.
Article Source: non-printable characters in the Linux C string are converted to 16 binary
Non-printable characters in the Linux C string are converted to 16 binary