wow-type文件说明
- 项目地址:https://gitee.com/wow-iot/wow-iot7
- 本文件的的功能主要用于处理数据类型的转换,包含字符串、数组、BCD码;
- 由于涉及的类型较多,仅以uint64_t类型进行描述说明;
指针强制类型转换
#define p2u64(x) ((uint64_t)(size_t)(x))
#define p2s64(x) ((sint64_t)(size_t)(x))
字符串转数据类型
/*brief 二进制字符串转int64*/
int S2toU64(const char* pcStr,uint64_t* pu64Data);
/*brief 十进制字符串转int64*/
int S10toU64(const char* pcStr,uint64_t* pu64Data);
/*brief 十六进制字符串转int64 */
int S16toU64(const char* pcStr,uint64_t* pu64Data);
具体实现如下:
二进制实现
uint64_t _S2toU64(const char* pcStr)
{
while (isspace(*pcStr)) pcStr++;
int sign = 0;
if (*pcStr == '-') {
sign = 1;
pcStr++;
}else if(*pcStr == '+'){
pcStr++;
}
if (pcStr[0] == '0' && (pcStr[1] == 'b' || pcStr[1] == 'B')){
pcStr += 2;
}
while ((*pcStr) == '0') pcStr++;
uint32_t val = 0;
while (*pcStr){
char ch = *pcStr;
CHECK_RET_BREAK(isdigit2(ch));
val = (val << 1) + (ch - '0');
pcStr++;
}
return (sign)?(~val + 1):val;
}
十进制进制实现
uint64_t _S10toU64(const char* pcStr)
{
while (isspace(*pcStr)) pcStr++;
int sign = 0;
if (*pcStr == '-') {
sign = 1;
pcStr++;
}else if(*pcStr == '+'){
pcStr++;
}
while ((*pcStr) == '0') pcStr++;
uint32_t val = 0;
while (*pcStr){
char ch = *pcStr;
CHECK_RET_BREAK(isdigit10(ch));
val = val * 10 + (ch - '0');
pcStr++;
}
return (sign)?(~val + 1):val;
}
十六进制进制实现
uint64_t _S16toU64(const char* pcStr)
{
while (isspace(*pcStr)) pcStr++;
int sign = 0;
if (*pcStr == '-') {
sign = 1;
pcStr++;
}else if(*pcStr == '+'){
pcStr++;
}
if (pcStr[0] == '0' && (pcStr[1] == 'x' ||pcStr[1] == 'X')) {
pcStr += 2;
}
while ((*pcStr) == '0')pcStr++;
uint32_t val = 0;
while (*pcStr){
char ch = *pcStr;
if (isdigit10(ch))
val = (val << 4) + (ch - '0');
else if (ch > ('a' - 1) && ch < ('f' + 1))
val = (val << 4) + (ch - 'a') + 10;
else if (ch > ('A' - 1) && ch < ('F' + 1))
val = (val << 4) + (ch - 'A') + 10;
else break;
pcStr++;
}
return (sign)?(~val + 1):val;
}
数据类型转字符串
#define StrtoS64(str,v) do {sscanf(str,"%lld", v); } while (0);
#define StrtoU64(str,v) do {sscanf(str,"%llu", v); } while (0);
#define S64toStr(v, buf,n) do {snprintf(buf, n,"%lld", v); } while (0);
#define U64toStr(v, buf,n) do {snprintf(buf, n,"%llu", v); } while (0);
数据类型与数组
/*brief long类型转数组*/
int long2char(uint64_t u64Data,uint8_t* pu8Data)
{
CHECK_RET_VAL_P(pu8Data,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
#if WOW_BIG_ENDIAN == 1
byte_set_64_be_impl(pu8Data,u64Data);
#elif WOW_LITTLE_ENDIAN == 1
byte_set_64_le_impl(pu8Data,u64Data);
#else
byte_set_64_be_impl(pu8Data,u64Data);
#endif
return WOW_SUCESS;
}
/*brief 数组转long类型 */
int char2long(uint8_t* pu8Data,uint32_t *pu64Data)
{
CHECK_RET_VAL_P(pu8Data && pu64Data,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
#if WOW_BIG_ENDIAN == 1
*pu64Data = byte_get_64_be_impl(pu8Data);
#elif WOW_LITTLE_ENDIAN == 1
*pu64Data = byte_get_64_le_impl(pu8Data);
#else
*pu64Data = byte_get_64_be_impl(pu8Data);
#endif
return WOW_SUCESS;
}
BCD码与字符串
int BCD2String(uint8_t *pu8BcdData, int nBcdLen, uint8_t *pu8StrData, int nStrLen)
{
int iPosFlag = 0;
char cBCD = 0;
CHECK_RET_VAL_P(pu8BcdData && pu8StrData ,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
CHECK_RET_VAL_P(2 * nBcdLen <= nStrLen,-PARAM_INPUT_SIZE_IS_SMALL,"param input size invalid!\n");
for (iPosFlag = 0; iPosFlag < nBcdLen; iPosFlag++)
{
cBCD = (pu8BcdData[iPosFlag] >> 4);
if (0x0f == cBCD){
break;
}
pu8StrData[2 * iPosFlag] = cBCD + '0';
CHECK_RET_VAL_P(isdigit(pu8StrData[2 * iPosFlag]),-1,"pu8StrData[2 * %d] is not digit!\n", iPosFlag);
cBCD = (pu8BcdData[iPosFlag] & 0x0f);
if (0x0f == cBCD){
break;
}
pu8StrData[2 * iPosFlag + 1] = cBCD + '0';
CHECK_RET_VAL_P(isdigit(pu8StrData[2 * iPosFlag + 1]),-1,"pu8StrData[2 * %d] is not digit!\n", iPosFlag);
pu8StrData[2 * iPosFlag + 1] = cBCD + '0';
}
return WOW_SUCESS;
}
int String2BCD(const uint8_t *pu8StrData, int nStrLen, uint8_t *pu8BcdData, int nBcdLen)
{
int iPosFlag = 0;
CHECK_RET_VAL_P(pu8BcdData && pu8StrData ,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
CHECK_RET_VAL_P(2 * nBcdLen >= nStrLen,-PARAM_INPUT_SIZE_IS_SMALL,"param input size invalid!\n");
for (iPosFlag = 0; iPosFlag < nStrLen; iPosFlag+=2)
{
CHECK_RET_VAL_P(isdigit(pu8StrData[iPosFlag]),-1,"pu8StrData[%d] is not digit!\n", iPosFlag);
pu8BcdData[iPosFlag / 2] = pu8StrData[iPosFlag] - '0';
pu8BcdData[iPosFlag / 2] <<= 4;
if ((iPosFlag + 1 >= nStrLen)){
break;
}
CHECK_RET_VAL_P(isdigit(pu8StrData[iPosFlag+ 1]),-1,"pu8StrData[%d+ 1] is not digit!\n", iPosFlag);
pu8BcdData[iPosFlag / 2] += pu8StrData[iPosFlag + 1] - '0';
}
return WOW_SUCESS;
}