Non-printable characters in the Linux C string are converted to 16 binary

Source: Internet
Author: User
Tags printable characters

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 voidprinthex(unsigned char*src,intlen){    if(src==NULL)    {        return;    }    if(len>(1024*1024*3-1))    {              return;    }    charx[1024*1024*3]={0};    inti=0;    for(i=0;i<len;i++)    {        chartmp[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.