十六进制字符串、char数组的转换: c c++ java

 http://hi.baidu.com/wuruoyin/item/49c13b6c64fd132169105bbe

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();

    }

转载地址:http://qing.blog.sina.com.cn/1820422183/6c81702733001qvk.html?sudaref=www.sogou.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值