Bytes2HexStr与hexStr2Bytes

来源:互联网 发布:php 开源网盘系统 编辑:程序博客网 时间:2024/06/18 06:51
/*   Byte值转换为bytes字符串 *   @param src:Byte指针 srcLen:src长度 des:转换得到的bytes字符串   **/  static void Bytes2HexStr(BYTE *src,int srcLen,BYTE *des){BYTE *res;int i=0;res = des;while(srcLen>0){sprintf((char*)(res+i*2),"%02x",*(src+i));i++;srcLen--;}}/**   * bytes字符串转换为Byte值   * @param String src Byte字符串,每个Byte之间没有分隔符   * @return byte[]   */   static BYTE* hexStr2Bytes(string src){char *strEnd;int m=0;int len = src.length()/2;BYTE* ret = new BYTE[len];for(int i =0;i<len;i++){m = i*2;string subs = src.substr(m,2);ret[i] = strtol(subs.c_str(),&strEnd,16);}return ret;}