Garbled characters in values of char arrays under VC ++

Source: Internet
Author: User
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.

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.