Click the blue text
Follow us
Due to changes in the public account’s push rules, please click “Read” and add “Star” to get exciting technical shares at the first time
Source from the internet, please delete if infringing
1. String to Hexadecimal
Code implementation:
void StrToHex(char *pbDest, char *pbSrc, int nLen)
{
char h1,h2;
char s1,s2;
int i;
for (i=0; i<nLen/2; i++)
{
h1 = pbSrc[2*i];
h2 = pbSrc[2*i+1];
s1 = toupper(h1) - 0x30; //toupper converts to uppercase
if (s1 > 9)
s1 -= 7;
s2 = toupper(h2) - 0x30;
if (s2 > 9)
s2 -= 7;
pbDest[i] = s1*16 + s2;
}
}
2. Hexadecimal to String
Code implementation:
void HexToStr(char *pszDest, char *pbSrc, int nLen)
{
char ddl, ddh;
for (int i = 0; i < nLen; 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';
}
Or
u16 Hex2StringArray (u8 *pSrc, u16 SrcLen, u8 *pObj)
{
u16 i=0;
for(i=0; i<SrcLen; i++)
{
sprintf((char *)(pObj + i * 2), "%02X", *(pSrc + i));
}
*(pObj + i * 2) = '\0';
return (i * 2);
}
Effect: Hexadecimal: 0x13 0xAA 0x02 to string: “13AAA2”
3. String to Decimal
Code implementation:First method, if it has a negative sign this is the implementation of the atoi function:
int my_atoi(const char *str)
{
int value = 0;
int flag = 1; // Determine the sign
while (*str == ' ') // Skip spaces at the beginning of the string
{
str++;
}
if (*str == '-') // If the first character is '-', it may be negative
{
flag = 0;
str++;
}
else if (*str == '+') // If the first character is '+', it may be positive
{
flag = 1;
str++;
}// If the first character is not '+' or '-' and not a digit, return 0 directly
else if (*str >= '9' || *str <= '0')
{
return 0;
}
// End conversion when encountering a non-digit character or '
'
while (*str != '\0' && *str <= '9' && *str >= '0')
{
value = value * 10 + *str - '0'; // Convert digit character to corresponding integer
str++;
}
if (flag == 0) // For negative numbers
{
value = -value;
}
return value;
}
Effect: String: “-123” to -123Second method, if without a negative sign:
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;
}
}
Effect: String: “123” to 123Third method: Includes conversion to floating point:
//m^n function
//Return value: m^n power.
u32 NMEA_Pow(u8 m,u8 n)
{
u32 result=1;
while(n--)result*=m;
return result;
}
//str converts to number, ends with ',' or '*'
//buf: number storage area
//dx: decimal point position, returned to the calling function
//Return value: converted value
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) // Get the length of integer and decimal
{
if(*p=='-'){mask|=0X02;p++;}// Is negative
if(*p==','||(*p=='*'))break;// Encountered end
if(*p=='.'){mask|=0X01;p++;}// Encountered decimal point
else if(*p>'9'||(*p<'0')) // Invalid character
{
ilen=0;
flen=0;
break;
}
if(mask&0X01)flen++;
else ilen++;
p++;
}
if(mask&0X02)buf++; // Remove negative sign
for(i=0;i<ilen;i++) // Get integer part data
{
ires+=NMEA_Pow(10,ilen-1-i)*(buf[i]-'0');
}
if(flen>5)flen=5; // Take at most 5 decimal places
*dx=flen; // Decimal point position
for(i=0;i<flen;i++) // Get decimal part data
{
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;
}
Effect: String: “123.456” first converted to 123456, then divided by 1000 to get 123.456
4. Decimal to String
Code implementation:If it is just a single decimal to string, use sprintf function.If it is a decimal array:
u16 DectoStr (u8 *pSrc, u16 SrcLen, u8 *pObj)
{
u16 i=0;
for(i=0; i<SrcLen; i++)
{
sprintf((char *)(pObj + i * 2), "%02d", *(pSrc + i));
}
*(pObj + i * 2) = '\0';
return (i * 2);
}
Effect: Decimal array 13 14 to string “1314”
5. u8, u32 Conversion
For example: In ASCII code
Here is the description of the image Character ‘A’, one byte 8bit, which is u8 hexadecimal 0x41 binary 0100 0001And the corresponding decimal is 65 integer 65, 4 bytes 32bit, which is u32 hexadecimal 0x41 binary 0000 0000 0000 0000 0000 0000 0100 0001Convert u32 number to u8 arrayNote: Here is a character array, not a stringA string is a char array terminated with a null character (\0)
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);
}
Effect: Integer 50 to character array {‘\0′,’\0′,’\0′,’2’}u8 array to u32
void U8ArrayToU32(uint8_t *buf, uint32_t *u32Value)
{
*u32Value = (buf[0] <<24) + (buf[1] <<16) + (buf[2] <<8) + (buf[3] <<0);
}
Effect: Character array {‘\0′,’\0′,’\0′,’2’} to integer 50
6. Big Endian and Little Endian
Finally, there is the big-endian and little-endian issue. STM32 defaults to little-endian mode, so how to convert to big-endian?1. Convert to big-endian
For big-endian:
pPack[0] = (u8)((len >> 8) & 0xFF);
pPack[1] = (u8)(len & 0xFF);
For little-endian:
pPack[0] = (u8)(len & 0xFF);
pPack[1] = (u8)((len >> 8) & 0xFF);
Effect: len is a data type u16 (short), for example 0x11 0x22, converted to u8 (unsigned char) array.Big-endian:
pPack[0] (0x11 )
pPack[1] (0x22)
Little-endian:
pPack[0] (0x22)
pPack[1] (0x11)
If you are over 18 years old and find learning 【C language】 too difficult? Want to try other programming languages, then I recommend you learn Python. Currently, a 499 yuan Python zero-based course is available for free for a limited time, with only 10 spots!
▲ Scan the QR code - Get it for free
Click to read the original text for more information