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 ).