16进制格式化输出函数
// 16Hex.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// int main(int argc, char* argv[])
// {
// printf("Hello World!\n");
// return 0;
// }
// 16Hex.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
// 16进制输出函数 把一个unsigned char *数组,按照16进制输出
// 调用前,请先初始化 dst, 并确保dst有足够的空间存放
// dst的空间是 char数组的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 main(int argc, char* argv[])
{
unsigned char ch1[12] = {1, 255};//{0x01, 0xff};
char _dstbuf[1024] = "\0";
ToHex(ch1, 2, _dstbuf);
printf("%s\n",_dstbuf);
return 0;
}