How to print a string with embedded nulls so that “(null)” is substituted for '\0'

后端 未结 4 532
别那么骄傲
别那么骄傲 2021-01-21 15:51

I have a string I composed using memcpy() that (when expanded) looks like this:

char* str = \"AAAA\\x00\\x00\\x00...\\x11\\x11\\x11\\x11\\x00\\x00...\";
<         


        
4条回答
  •  萌比男神i
    2021-01-21 16:22

    One of the key cornerstones of C is strings are terminated by '\0'. Everyone lives by that rule. so I suggest you not think of your string as a string but as an array of characters.

    If you traverse the array and test for '\0', you can print "(null)" out in place of the character. Here is an example. Please note, your char * str was created either as a char array or on the stack using malloc. This code needs to know the actual buffer size.

    char* str = "AAAA\x00\x00\x00...\x11\x11\x11\x11\x00\x00...";
    int iStrSz = 
    
    int idx;
    
    for(idx=0; idx

提交回复
热议问题