日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

把ASCII码的字符串转为数字的16进制

發(fā)布時(shí)間:2025/4/16 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 把ASCII码的字符串转为数字的16进制 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

/*
函數(shù)名稱:AsciiStrToHexArray
功能:把ASCII碼的字符串轉(zhuǎn)為數(shù)字的16進(jìn)制
參數(shù)dst:存儲(chǔ)16進(jìn)制數(shù)組數(shù)據(jù)指針
參數(shù)src:存儲(chǔ)ASCII碼的字符串指針
返回值:16進(jìn)制的數(shù)組數(shù)據(jù)長(zhǎng)度
*/

int16_t AsciiStrToHexArray(uint8_t*dst,uint8_t*src)
{
? ?uint8_t ch;
? ?uint16_t index=0;

? ? if(strlen(src) == NULL)
? ? {
? ? ? ?return -1;
? ? }

? ? if(strlen(src)%2 == 0)//ASCII字符串偶數(shù)長(zhǎng)度
? ? {
? ? ? ?for(index = 0;index < strlen(src);index++)
? ? ? ?{
? ? ? ? ? ?if((src[index]>='0')&&(src[index]<='9'))//數(shù)字0-9范圍
? ? ? ? ? ?{
? ? ? ? ? ? ? ch = src[index]-0x30;
? ? ? ? ? ?}
? ? ? ? ? ?else if((src[index]>='A')&&(src[index]<='F'))//大寫(xiě)字母A-F范圍
? ? ? ? ? ?{
? ? ? ? ? ? ? ch = src[index]-0x37;
? ? ? ? ? ?}
? ? ? ? ? ?else if((src[index]>='a')&&(src[index]<='f'))//小寫(xiě)字母,a-f范圍
? ? ? ? ? ?{
? ? ? ? ? ? ? ch = src[index]-0x57;
? ? ? ? ? ?}else{

? ? ? ? ? ? ??return -1;

? ? ? ? ? }?
? ? ? ? ? ?if(index%2 != 0)//下標(biāo)從0開(kāi)始,取余數(shù)不為0則是偶數(shù)長(zhǎng)度的數(shù)據(jù)
? ? ? ? ? ?{
? ? ? ? ? ? ? dst[index/2]=dst[index/2]|ch;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? dst[index/2]=ch<<4;
? ? ? ? ? ? }
? ? ? ?}
? ? ? ?return (index / 2);//整除,把除數(shù)作為16進(jìn)制數(shù)組長(zhǎng)度返回
? ? }else{//ASCII字符串奇數(shù)長(zhǎng)度
? ? ? ?for(index = 0;index < strlen(src);index++)
? ? ? ?{
? ? ? ? ? ? if((src[index]>='0')&&(src[index]<='9'))//數(shù)字0-9范圍
? ? ? ? ? ? {
? ? ? ? ? ? ? ch = src[index]-0x30;
? ? ? ? ? ? }
? ? ? ? ? ? else if((src[index]>='A')&&(src[index]<='F'))//大寫(xiě)字母A-F范圍
? ? ? ? ? ? {
? ? ? ? ? ? ? ch = src[index]-0x37;
? ? ? ? ? ? }else if((src[index]>='a')&&(src[index]<='f'))//小寫(xiě)字母,a-f范圍
? ? ? ? ? ? {
? ? ? ? ? ? ? ch = src[index]-0x57;
? ? ? ? ? ? }

? ? ? ? ? ? else{

? ? ? ? ? ? ??return -1;

? ? ? ? ? ? }?

? ? ? ? ? ? if((index+1) == strlen(src))//奇數(shù)時(shí),最后單獨(dú)一個(gè)字符轉(zhuǎn)為16進(jìn)制后存儲(chǔ)緩存
? ? ? ? ? ? {
? ? ? ? ? ? ? ?dst[index/2] = ch;
? ? ? ? ? ? }else{//不是最后一個(gè)字符
? ? ? ? ? ? ? ? if(index%2 != 0)//下標(biāo)從0開(kāi)始,取余數(shù)不為0則是偶數(shù)長(zhǎng)度的數(shù)據(jù)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? dst[index/2]=dst[index/2]|ch;
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? dst[index/2]=ch<<4;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ?}
? ? ? ?return (index / 2+1);//不整除,把除數(shù)加1作為16進(jìn)制數(shù)組長(zhǎng)度返回
? ? }
}

int main()
{
? ? uint8_t str[]="1234567890abcdef"; //12 34 ab cd 0e
? ? uint8_t data[100];
? ? int16_t len = 0;

? ? memset(data,0,sizeof(data));

? ? len = AsciiStrToHexArray(data,str);

? ? if(len != -1)
? ? {
? ? ? ? for(uint8_t i = 0;i < len;i++)
? ? ? ? {
? ? ? ? ? ? printf("%0.2x ",data[i]);
? ? ? ? }
? ? }

? ? return 0;
}

程序運(yùn)行結(jié)果如下:

總結(jié)

以上是生活随笔為你收集整理的把ASCII码的字符串转为数字的16进制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。