Manipulating C Strings with Multiple Null Characters in Memory

I need to search through a piece of memory for a character string, but some of these strings separate each character, for example:
   “I..am ...... ring“
with all. ”These are zero characters. My problem is because that it gets into memory. I tried several ways, for example:

 char* str2; 
 str2 = (char*)malloc(sizeof(char)*40);   
 memcpy((void*)str2, "123\0567\09abc", 12);    

It will put the following into memory: str2 points to: 123.7.9abc..
Something like
str2 = "123456789\0abcde\054321";
Will have str2 pointing to a block of memory that looks like 123456789.abcde,321where '.' is a null character, and ',' is the actual comma.

Thus, clearly inserting null characters in cstrings does not work as easily as I thought, for example, inserting a newline character. I ran into similar difficulties trying this with a string library as well. I could do individual tasks, for example:

 char* str;    
 str = (char*)malloc(sizeof(char)*40);  
 strcpy(str, "123");  
 strcpy(str+4, "abc");  
 strcpy(str+8, "ABC");  

But this, of course, is not preferable, and I believe that the problem lies in my understanding of how c-style strings are stored in memory. Obviously, "abc \ 0123" does not actually go into memory as 61 62 63 00 31 32 33(in hexadecimal format). How is it stored and how can I store what I need?

(I also apologize for not setting the code in blocks, this is my first question asking a question, and is somehow “four times” harder than I can apparently handle. Thanks, Lucian. Necessary .)

+5
source share
4

char , UTF-16. , .

, Windows, UTF-16 , wchar_t*, char* . char . , wcscpy, strcpy ..

+6

\0 - , " " ( ).


, , , (, "\ 012" 1) , C:

char const * p = "123456789" "\0" "abcde" "\0" "54321";

1. "\ 012" 0x0A, ; 0x00, '1' '2'.

+3

-, , NULL, - , , unsigned short s. wchar_t char wcsxxx() strxxx().

Windows 2- (UTF-16, ) , .

, strxxx() , . . memxxx() .

"abc\0123" , , \012 escape- - 12 ( 0a hex). , :

"abc\000123"
"abc\x00123"
"abc\0""123"

, , .

strcpy(str+strlen(str)+1, "123");

, .

+2

. , . 16- wchat_t, c. wchar, ascii, , , .

just (wchar_t *) XXX, where XXX is a pointer to this memory area and performs wchar_t wwsc_t operations such as wcscpy etc ... as for zeros between lines, this may be a known method of passing multiple string constructs. You can just iterate after reading each line until you come across two consecutive zeros.

I hope I answered your question. Good luck

0
source

All Articles