One algorithm question: byte [0] = ox12 byte [1] = 0 × 34 byte [2] = 0 × 56. Finally, convert it to string 123456.

Source: Internet
Author: User

I am lazy.

 

<Br/> // byte2char. cpp: defines the entry point of the console application. <Br/> // </P> <p> # include "stdafx. H "</P> <p> typedef unsigned char byte; </P> <p> byte data [] ={< br/> 0x12, 0x34,0x56 <br/> }; </P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> char TMP [64] = {0 }; </P> <p> for (INT I = 0; I <sizeof (data)/sizeof (byte); I ++) <br/>{< br/> char s [12]; <br/> ITOA (data [I], S, 16); <br/> strcat (TMP, s); <br/>}</P> <p> printf ("% s/n", TMP ); </P> <p> system ("pause"); </P> <p> return 0; <br/>}</P> <p>

 

The accurate algorithm is

 

# Include <iostream> </P> <p> Using STD: cout; <br/> Using STD: Endl; </P> <p> typedef char byte; <br/> bcdconvert (res_arr, direction, sizeof (c2_arr); <br/> bcdconvert (res_arr, direction, sizeof (c2_arr); <br/> bcdconvert (res_arr, c3_arr, sizeof (c3_arr); </P> <p> byte * bcdconvert (byte * Cr, byte * Cl, int cl_len) {<br/> byte * cl_end = Cl + cl_len; <br/> byte * cr_begin = CR; <br/> If (cl_len = 0) return NULL; </P> <p> For (; CL <cl_end; ++ Cl) {<br/> * Cr ++ = 0x30 | (* Cl & 0xf0)> 4 ); <br/> * Cr ++ = 0x30 | * Cl & 0x0f; <br/>}</P> <p> // cr [-1] = 0x30? Cr [-1] = 0: Cr [0] = 0; </P> <p>/**/<br/> while (Cr> cr_begin & * -- Cr = 0x30) NULL; <br/> If (CR! = Cr_begin) <br/> Cr [1] = 0; <br/> else return NULL; <br/>/**/</P> <p> return CR; <br/>}</P> <p> int main (INT argc, char * argv []) {<br/> byte c1_arr [] = {0x12, 0x34, 0x56 }; <br/> byte c2_arr [] = {0x12, 0x34, 0x50 }; <br/> byte c3_arr [] = {0x00, 0x00, 0x00}; </P> <p> byte res_arr [32]; </P> <p> If (bcdconvert (res_arr, c1_arr, sizeof (c1_arr ))! = NULL) <br/> cout <res_arr <Endl; </P> <p> If (bcdconvert (res_arr, c2_arr, sizeof (c2_arr ))! = NULL) <br/> cout <res_arr <Endl; </P> <p> If (bcdconvert (res_arr, c3_arr, sizeof (c3_arr ))! = NULL) <br/> cout <res_arr <Endl; </P> <p> return 0; <br/>}</P> <p> if only the last 0 is deleted, <br/> Cr [-1] = 0x30? Cr [-1] = 0: Cr [0] = 0; <br/> uncomment, </P> <p> additional comment: <br/>/**/<br/> while (Cr> cr_begin & * -- Cr = 0x30) NULL; <br/> If (CR! = Cr_begin) <br/> Cr [1] = 0; <br/> else return NULL; <br/>/**/<br/> change the first/**/to comment it out! </P> <p> If the returned value is not null, the conversion is correct. If the returned value is null, the input byte [] is incorrect, or all values are 0 (this may only occur when you use while ).

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.