|
// 16Hex.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//

#include "stdafx.h"

#include <stdlib.h>
#include <string.h>


// 16進(jìn)制輸出函數(shù) 把一個(gè)unsigned char *數(shù)組,按照16進(jìn)制輸出
// 調(diào)用前,請(qǐng)先初始化 dst, 并確保dst有足夠的空間存放
// dst的空間是 char數(shù)組的3倍+1

void ToHex( unsigned char * src, int length, char * dst )
  {
char temp[3];


for (int i = 0; i < length; ++i)
 { char result[3] = {'0', '0', ' '};
itoa(src[i], temp, 16);
if (strlen(temp) == 1)
memcpy(result + 1, temp, 1);
else
memcpy(result, temp, 2);

memcpy(dst + 3 * i, result, 3);
}

}



int _tmain(int argc, _TCHAR* argv[])
  {
 unsigned char ch1[2] = {0x01, 0xff};

char _dstbuf[1024] = "\0";

ToHex(ch1, 2, _dstbuf);


return 0;
}



|