Recently I made a VC ++ job. Program , To get a char array from the nth bit to the N + M bit, and then assign it to a char array, but there is always garbled code. After reading the information, because there is no ending '\ 0'
Part of the memory content after the string is stored Code Chinese characters are displayed. For example, the array is taken from 2nd bits to the last 2nd bits. Code
1 Char * Pbuf = New Char [Len_r - 4 ];
2 * (Pbuf + Len_r - 4 ) = ' \ 0 ' ;
3 Utility: chararraytochararray (BUF, pbuf, len_r ); // Extract the Buf (0132323104) data and assign it to pbuf (323231)
4
5 Int Iend = (Len_r - 4 ) / 2 + 1 ;
6 Byte * Dbuf = New Byte [Iend];
7 * (Dbuf + Iend - 1 ) = 0 ;
8 Utility: chararraytobytearray (pbuf, dbuf, len_r - 4 ); // Convert pbuf (323230) to dbuf (220)
9 Bool Utility: chararraytochararray ( Char * Charr, Char * Btarr, Int Charrlen)
10 {
11 Int J = 0 ;
12 For ( Int I = 2 ; I < Charrlen - 2 ; I ++ )
13 {< br> 14
15 * (btarr + J) =* (Charr + I);
16 j ++ ;< br> 17 }
18 Return True ;
19 }
20
21 Bool Utility: chararraytobytearray ( Char * Charr, Byte * Btarr, Int Charrlen)
22 {
23
24 For ( Int I = 0 ; I + 1 < Charrlen; I + = 2 ) // Converts the returned string to the actual data.
25 {
26 Byte IHI, ILO;
27
28 IHI = Hexchartobyte ( * (Charr + I ));
29 ILO = Hexchartobyte ( * (Charr + I + 1 ));
30 If (IHI > 15 | ILO > 15 )
31 Return False ;
32
33 * (Btarr + I / 2 ) = IHI * 16 + ILO;
34
35
36 }
37 Return True ;
38 }
Buf is the array to be converted, and len_r is the length.