字符转化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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