十进制数值字符串转各种位宽的整形数据demo
本demo实现了字符串转各种位宽的函数,代码简单效率高,非常适合单片机嵌入式应用。需要注意的是字符串需要按照严格的输入要求输入才行,比如“-127”,“255”等,除了第一个字符可以是负号‘-’外,其他字符必须是数字,对应位宽的数字字符,必须选择对应的函数。demo如下,可以直接拷贝到C语言编译器,编译允许即可获得结果。
- #include
- #include
- /* exact-width signed integer types */
- typedef signed char int8_t;
- typedef signed short int int16_t;
- typedef signed int int32_t;
- typedef signed long long int64_t;
- /* exact-width unsigned integer types */
- typedef unsigned char uint8_t;
- typedef unsigned short int uint16_t;
- typedef unsigned int uint32_t;
- typedef unsigned long long uint64_t;
- int8_t cAto8(const char * pcString,uint8_t ucLength){
- int8_t cInt8 = 0;
- uint8_t ucI = 0;
- if(pcString[ucI] == '-'){//
- ucI++;
- }
- for(;ucI cInt8 = cInt8*10 + pcString[ucI]-'0';
- }
- if(pcString[0] == '-'){//
- cInt8 = ~cInt8 + 1;
- }
- return cInt8;
- }
- uint8_t ucAtou8(const char * pcString,uint8_t ucLength){
- uint8_t ucInt8 = 0;
- uint8_t ucI = 0;
- for(;ucI ucInt8 = ucInt8*10 + pcString[ucI]-'0';
- }
- return ucInt8;
- }
- int16_t sAto16(const char * pcString,uint8_t ucLength){
- int16_t sInt16 = 0;
- uint8_t ucI = 0;
- if(pcString[ucI] == '-'){//
- ucI++;
- }
- for(;ucI sInt16 = sInt16*10 + pcString[ucI]-'0';
- }
- if(pcString[0] == '-'){//
- sInt16 = ~sInt16 + 1;
- }
- return sInt16;
- }
- uint16_t usAtou16(const char * pcString,uint8_t ucLength){
- uint16_t usInt16 = 0;
- uint8_t ucI = 0;
- for(;ucI usInt16 = usInt16*10 + pcString[ucI]-'0';
- }
- return usInt16;
- }
- int32_t iAto32(const char * pcString,uint8_t ucLength){
- int32_t iInt32 = 0;
- uint8_t ucI = 0;
- if(pcString[ucI] == '-'){//
- ucI++;
- }
- for(;ucI iInt32 = iInt32*10 + pcString[ucI]-'0';
- }
- if(pcString[0] == '-'){//
- iInt32 = ~iInt32 + 1;
- }
- return iInt32;
- }
- uint32_t uiAtou32(const char * pcString,uint8_t ucLength){
- uint32_t uiInt32 = 0;
- uint8_t ucI = 0;
- for(;ucI uiInt32 = uiInt32*10 + pcString[ucI]-'0';
- }
- return uiInt32;
- }
- int64_t lAto64(const char * pcString,uint8_t ucLength){
- int64_t lInt64 = 0;
- uint8_t ucI = 0;
- if(pcString[ucI] == '-'){//
- ucI++;
- }
- for(;ucI lInt64 = lInt64*10 + pcString[ucI]-'0';
- }
- if(pcString[0] == '-'){//
- lInt64 = ~lInt64 + 1;
- }
- return lInt64;
- }
- uint64_t ulAtou64(const char * pcString,uint8_t ucLength){
- uint64_t ulInt64 = 0;
- uint8_t ucI = 0;
- for(;ucI ulInt64 = ulInt64*10 + pcString[ucI]-'0';
- }
- return ulInt64;
- }
- main(int argc, char *argv[])
- {
- int8_t a1;
- uint8_t a2;
- int16_t b1;
- uint16_t b2;
- int32_t c1;
- uint32_t c2;
- int64_t d1;
- uint64_t d2;
- a1 = cAto8("-128",strlen("-128"));
- printf("a1=%drn",a1);
- a2 = ucAtou8("255",strlen("255"));
- printf("a2=%drn",a2);
- b1 = sAto16("-32768",strlen("-32768"));
- printf("b1=%drn",b1);
- b2 = usAtou16("65535",strlen("65535"));
- printf("b2=%drn",b2);
- c1 = iAto32("-2147483648",strlen("-2147483648"));
- printf("c1=%ldrn",c1);
- c2 = uiAtou32("4294967295",strlen("4294967295"));
- printf("c2=%urn",c2);
- d1 = lAto64("-9223372036854775808",strlen("-9223372036854775808"));
- printf("d1=%ldrn",d1);
- d2 = ulAtou64("18446744073709551615",strlen("18446744073709551615"));
- printf("d2=%lurn",d2);
- return 0;
- }
- /*-------------------(C) COPYRIGHT 2020 头条@单片机嵌入式爱好者 ----------------*/
- 运行结果如下:
- a1=-128
- a2=255
- b1=-32768
- b2=65535
- c1=2147483648
- c2=4294967295
- d1=-9223372036854775808
- d2=18446744073709551615
|