當前位置:編程學習大全網 - 源碼下載 - C++ 字符串與16進制字符串之間的轉換

C++ 字符串與16進制字符串之間的轉換

staticintstr_to_hex(char*string,unsignedchar*cbuf,intlen)

{

BYTEhigh,low;

intidx,ii=0;

for(idx=0;idx<len;idx+=2)

{

high=string[idx];

low=string[idx+1];

if(high>='0'&&high<='9')

high=high-'0';

elseif(high>='A'&&high<='F')

high=high-'A'+10;

elseif(high>='a'&&high<='f')

high=high-'a'+10;

else

return-1;

if(low>='0'&&low<='9')

low=low-'0';

elseif(low>='A'&&low<='F')

low=low-'A'+10;

elseif(low>='a'&&low<='f')

low=low-'a'+10;

else

return-1;

cbuf[ii++]=high<<4|low;

}

return0;

}

/****************************************************************************

函數名稱:hex_to_str

函數功能:十六進制轉字符串

輸入參數:ptr字符串buf十六進制len十六進制字符串的長度。

輸出參數:無

*****************************************************************************/

staticvoidhex_to_str(char*ptr,unsignedchar*buf,intlen)

{

for(inti=0;i<len;i++)

{

sprintf(ptr,"%02x",buf[i]);

ptr+=2;

}

}

擴展資料

byte數組轉十六進制字符串

publicstaticStringbyteArraytoHexString(byte[]b){

intiLen=b.length;

StringBuffersb=newStringBuffer(iLen*2);

for(inti=0;i<iLen;i++){

intintTmp=b[i];

while(intTmp<0){

intTmp=intTmp+256;

}

if(intTmp<16){

sb.append("0");

}

sb.append(Integer.toString(intTmp,16));

}

returnsb.toString().toUpperCase();

}

  • 上一篇:璀璨星辰的小說目錄
  • 下一篇:南瓜新衣源代碼
  • copyright 2024編程學習大全網