转载自: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','\0'};
   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\n",str1[k]);
   char bte[4] ; 
   hexstringToBytes(str1,bte,8);
   for(int m=0;m<4;m++)
   printf("bytes:%x\n",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("\n%x",chs[i]);
   }
   cout<<endl;
   char* sdf = hexstringToBytes(str);
    for(int j = 0;j<5;j++)
   {
      printf("\n%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();
    }

最新文章

  1. Android Studio导入github下载的project和module
  2. sql server实现自定义分割月功能
  3. Anaconda died after receiving signal 7
  4. 关于z-index
  5. C# 类型参数的约束
  6. 腾讯新浪通过IP地址获取当前地理位置(省份)的接口
  7. 【C51】单片机独立按键与矩阵按键
  8. 161031、java.util.StringTokenizer使用及源码
  9. js获取随机色
  10. 学习:Log中&#39;main&#39;, &#39;system&#39;, &#39;radio&#39;, &#39;events&#39;
  11. 安卓自动化测试之MonkeyRunner环境的搭建
  12. Web工程软件升级之数据库升级(二)
  13. 10003 Cutting Sticks(区间dp)
  14. ubuntu 14.0.4下安装有道字典
  15. [css 揭秘]:CSS揭秘 技巧(二):多重边框
  16. javascript之页面打印
  17. 毕业样本=[华威大学毕业证书]Warwick原件一模一样证书
  18. spring-security实现的token授权
  19. VirtualBox VM&#21551;&#29992;3D&#21152;&#36895;
  20. centOS连接没问题,使用SecureCRT就不能连接

热门文章

  1. 【FAQ】Ubuntu环境下ant编译android代码问题
  2. apache添加mod_limitipconn限制单个ip并发连接数
  3. homestead虚拟机,通过npm下载依赖包和解决运行gulp报错问题 yarn出错问题
  4. Python手势识别与控制
  5. lucene 索引中文档的属性建立与不建立带来的影响总结
  6. 使用dynamic类型来优化反射
  7. 对象关系映射 EmitMapper 及Tuple的使用
  8. sqlserver利用链接服务器查询或同步本地数据库和远程数据库
  9. SpringMVC学习(一)小demo
  10. E - Leading and Trailing 求n^k得前三位数字以及后三位数字,保证一定至少存在六位。