转载自:http://qing.blog.sina.com.cn/1820422183/6c81702733001qvk.html
1.c版
int hexcharToInt(char c)
{
if (c >= '0' && c <= '9') return (c - '0');
if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
return 0;
}
void hexstringToBytes(char* hexstring,char* bytes,int hexlength)
{
cout<<"length is :"<<sizeof(hexstring)/sizeof(char)<<endl;
for (int i=0 ; i <hexlength ; i+=2) {
bytes[i/2] = (char) ((hexcharToInt(hexstring[i]) << 4)
| hexcharToInt(hexstring[i+1]));
}
}
void bytesToHexstring(char* bytes,int bytelength,char *hexstring,int hexstrlength)
{
char str2[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
for (int i=0,j=0;i<bytelength,j<hexstrlength;i++,j++)
{
int b;
b = 0x0f&(bytes[i]>>4);
char s1 = str2[b];
hexstring[j] = s1;
b = 0x0f & bytes[i];
char s2 = str2[b];
j++;
hexstring[j] = s2;
}
}
int main()
{
char csh[5] = {'1','2','3','4',' '};
cout<<"csh length is :"<<strlen(csh)<<endl;
char ch[4] = {'1','2','3','4'};
char str1[8];
bytesToHexstring(ch,4,str1,8);
for(int k=0;k<8;k++)
printf("hex:%x
",str1[k]);
char bte[4] ;
hexstringToBytes(str1,bte,8);
for(int m=0;m<4;m++)
printf("bytes:%x
",bte[m]);
return 1;
}
2.c++版
int hexCharToInt(char c)
{
if (c >= '0' && c <= '9') return (c - '0');
if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
return 0;
}
char* hexstringToBytes(string s)
{
int sz = s.length();
char *ret = new char[sz/2];
for (int i=0 ; i <sz ; i+=2) {
ret[i/2] = (char) ((hexCharToInt(s.at(i)) << 4)
| hexCharToInt(s.at(i+1)));
}
return ret;
}
string bytestohexstring(char* bytes,int bytelength)
{
string str("");
string str2("0123456789abcdef");
for (int i=0;i<bytelength;i++) {
int b;
b = 0x0f&(bytes[i]>>4);
char s1 = str2.at(b);
str.append(1,str2.at(b));
b = 0x0f & bytes[i];
str.append(1,str2.at(b));
char s2 = str2.at(b);
}
return str;
}
int main()
{
char ch[5] ={'1','2','3','4','5'};
string str = bytestohexstring(ch,5);
char *chs =new char[5];
strcpy(chs,str.c_str());
cout<<endl;
cout<<str<<endl;
for(int i = 0;i<10;i++)
{
printf("
%x",chs[i]);
}
cout<<endl;
char* sdf = hexstringToBytes(str);
for(int j = 0;j<5;j++)
{
printf("
%x",sdf[j]);
}
return 1;
}
3.java版
public int hexCharToInt(char c) {
if (c >= '0' && c <= '9') return (c - '0');
if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
throw new RuntimeException ("invalid hex char '" + c + "'");
}
public byte[]
hexStringToBytes(String s) {
byte[] ret;
if (s == null) return null;
int sz = s.length();
ret = new byte[sz/2];
for (int i=0 ; i <sz ; i+=2) {
ret[i/2] = (byte) ((hexCharToInt(s.charAt(i)) << 4)
| hexCharToInt(s.charAt(i+1)));
}
return ret;
}
public String
bytesToHexString(byte[] bytes) {
if (bytes == null) return null;
StringBuilder ret = new StringBuilder(2*bytes.length);
for (int i = 0 ; i < bytes.length ; i++) {
int b;
b = 0x0f & (bytes[i] >> 4);
ret.append("0123456789abcdef".charAt(b));
b = 0x0f & bytes[i];
ret.append("0123456789abcdef".charAt(b));
}
return ret.toString();
}