Skip to content

Instantly share code, notes, and snippets.

@xkyii
Created July 2, 2011 11:34
Show Gist options
  • Save xkyii/1059958 to your computer and use it in GitHub Desktop.
Save xkyii/1059958 to your computer and use it in GitHub Desktop.
字符转化
#include <string>
std::string byteToHexStr(unsigned char byte_arr[], int arr_len)
{
std::string hexstr;
for (int i=0;i<arr_len;i++)
{
char hex1;
char hex2;
int value=byte_arr[i]; //直接将unsigned char赋值给整型的值,系统会自动强制转换
int v1=value/16;
int v2=value % 16;
//将商转成字母
if (v1>=0&&v1<=9)
hex1=(char)(48+v1);
else
hex1=(char)(55+v1);
//将余数转成字母
if (v2>=0&&v2<=9)
hex2=(char)(48+v2);
else
hex2=(char)(55+v2);
//将字母连接成串
hexstr=hexstr+hex1+hex2;
}
return hexstr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment