01
字符串轉(zhuǎn)十六進制
void?StrToHex(char?*pbDest,?char?*pbSrc,?int?nLen)
{
??char?h1,h2;
??char?s1,s2;
??int?i;
????for?(i=0;?i????{
????????h1?=?pbSrc[2*i];
????????h2?=?pbSrc[2*i+1];
????????s1?=?toupper(h1)?-?0x30;?//toupper?轉(zhuǎn)換為大寫字母
????????if?(s1?>?9)
????????????s1?-=?7;
????????s2?=?toupper(h2)?-?0x30;
????????if?(s2?>?9)
????????????s2?-=?7;
????????pbDest[i]?=?s1*16?+?s2;
????}
}
02
十六進制轉(zhuǎn)字符串
void?HexToStr(char?*pszDest,?char?*pbSrc,?int?nLen)
{
????char????ddl,?ddh;
????for?(int?i?=?0;?i?????{
????????ddh?=?48?+?pbSrc[i]?/?16;
????????ddl?=?48?+?pbSrc[i]?%?16;
????????if?(ddh?>?57)?ddh?=?ddh?+?7;
????????if?(ddl?>?57)?ddl?=?ddl?+?7;
????????pszDest[i?*?2]?=?ddh;
????????pszDest[i?*?2?+?1]?=?ddl;
????}
????pszDest[nLen?*?2]?=?'\0';
}
u16?Hex2StringArray?(u8?*pSrc,??u16?SrcLen,?u8?*pObj)
{
????u16?i=0;
????for(i=0;????i????{
????????sprintf((char?*)(pObj?+?i?*?2),?"%02X",?*(pSrc?+?i));
????}
????*(pObj?+?i?*?2)?=?'\0';
????return??(i?*?2);
}
03
字符串轉(zhuǎn)十進制
int?my_atoi(const?char?*str)
{
????int?value?=?0;
????int?flag?=?1;?//判斷符號
????while?(*str?==?'?')??//跳過字符串前面的空格
????{
????????str++;
????}
????if?(*str?==?'-')??//第一個字符若是‘-’,說明可能是負(fù)數(shù)
????{
????????flag?=?0;
????????str++;
????}
????else?if?(*str?==?'+')?//第一個字符若是‘+’,說明可能是正數(shù)
????{
????????flag?=?1;
????????str++;
????}//第一個字符若不是‘+’‘-’也不是數(shù)字字符,直接返回0
????else?if?(*str?>=?'9'?||?*str?<=?'0')?
????{
????????return?0;????
????}
????//當(dāng)遇到非數(shù)字字符或遇到‘\0’時,結(jié)束轉(zhuǎn)化
????while?(*str?!=?'\0'?&&?*str?<=?'9'?&&?*str?>=?'0')
????{
????????value?=?value?*?10?+?*str?-?'0';?//將數(shù)字字符轉(zhuǎn)為對應(yīng)的整形數(shù)
????????str++;
????}
????if?(flag?==?0)?//負(fù)數(shù)的情況
????{
????????value?=?-value;
????}
????return?value;
}
void?StrtoDec(uint32_t?*pbDest,?char?*pbSrc,?int?nLen)
{
????int?i;
????int?tmp=0;
????if(nLen?>?10)
????????*pbDest?=?0;
????tmp?=?1;
????*pbDest?=?0;
????for?(i=nLen-1;?i>=0;?i--)
????{
????????*pbDest?+=?tmp*(*(pbSrc+i)-'0');
????????tmp?=?tmp*10;
????}
}
//m^n函數(shù)
//返回值:m^n次方.
u32?NMEA_Pow(u8?m,u8?n)
{
????u32?result=1;????
????while(n--)result*=m;????
????return?result;
}
//str轉(zhuǎn)換為數(shù)字,以','或者'*'結(jié)束
//buf:數(shù)字存儲區(qū)
//dx:小數(shù)點位數(shù),返回給調(diào)用函數(shù)
//返回值:轉(zhuǎn)換后的數(shù)值
int?NMEA_Str2num(u8?*buf,u8*dx)
{
????u8?*p=buf;
????u32?ires=0,fres=0;
????u8?ilen=0,flen=0,i;
????u8?mask=0;
????int?res;
????while(1)?//得到整數(shù)和小數(shù)的長度
????{
????????if(*p=='-'){mask|=0X02;p++;}//是負(fù)數(shù)
????????if(*p==','||(*p=='*'))break;//遇到結(jié)束了
????????if(*p=='.'){mask|=0X01;p++;}//遇到小數(shù)點了
????????else?if(*p>'9'||(*p<'0'))???//有非法字符
????????{???
????????????ilen=0;
????????????flen=0;
????????????break;
????????}???
????????if(mask&0X01)flen++;
????????else?ilen++;
????????p++;
????}
????if(mask&0X02)buf++;?//去掉負(fù)號
????for(i=0;i????{??
????????ires+=NMEA_Pow(10,ilen-1-i)*(buf[i]-'0');
????}
????if(flen>5)flen=5;???//最多取5位小數(shù)
????*dx=flen;???????????//小數(shù)點位數(shù)
????for(i=0;i????{??
????????fres+=NMEA_Pow(10,flen-1-i)*(buf[ilen+1+i]-'0');
????}?
????res=ires*NMEA_Pow(10,flen)+fres;
????if(mask&0X02)res=-res;?????????
????return?res;
}?
04
十進制轉(zhuǎn)字符串
u16?DectoStr?(u8?*pSrc,?u16?SrcLen,?u8?*pObj)
{
????u16?i=0;
????for(i=0;????i????{
????????sprintf((char?*)(pObj?+?i?*?2),?"%02d",?*(pSrc?+?i));
????}
????*(pObj?+?i?*?2)?=?'\0';
????return??(i?*?2);
}
05
u8、u32轉(zhuǎn)換
void?U32ToU8Array(uint8_t?*buf,?uint32_t?u32Value)
{
????buf[0]?=?((u32Value?>>?24)?&?0xFF);
????buf[1]?=?((u32Value?>>?16)?&?0xFF);
????buf[2]?=?((u32Value?>>?8)?&?0xFF);
????buf[3]?=?(u32Value?&?0xFF);
}
void?U8ArrayToU32(uint8_t?*buf,?uint32_t?*u32Value)
{
????*u32Value?=?(buf[0]?<<24)?+?(buf[1]?<<16)?+?(buf[2]?<<8)?+?(buf[3]?<<0);
}
06
大小端(高低位)轉(zhuǎn)換
為大端:
pPack[0]?=?(u8)((len?>>?8)?&?0xFF);
pPack[1]?=?(u8)(len?&?0xFF);
為小端:
pPack[0]?=?(u8)(len?&?0xFF);
pPack[1]?=??(u8)((len?>>?8)?&?0xFF);
pPack[0]?(0x11?)?
pPack[1]?(0x22)?
pPack[0]?(0x22)?
pPack[1]?(0x11)
關(guān)注微信公眾號『玩轉(zhuǎn)嵌入式』,后臺回復(fù)“128”獲取干貨資料匯總,回復(fù)“256”加入技術(shù)交流群。
精彩技術(shù)文章推薦
01 |單片機的Bootloader,可以實現(xiàn)用戶輕松升級程序 |
02 |模塊化編程,是團隊協(xié)作編程的前提 |
03 |PWM是什么,有哪些用處? |
04 |零基礎(chǔ)如何學(xué)習(xí)單片機,一位入門者的進階路徑,可參考 |
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!