- #include <stdio.h>
- #include <string.h>
- /* 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<ucLength;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<ucLength;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<ucLength;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<ucLength;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<ucLength;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<ucLength;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<ucLength;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<ucLength;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=%d\r\n",a1);
- a2 = ucAtou8("255",strlen("255"));
- printf("a2=%d\r\n",a2);
- b1 = sAto16("-32768",strlen("-32768"));
- printf("b1=%d\r\n",b1);
- b2 = usAtou16("65535",strlen("65535"));
- printf("b2=%d\r\n",b2);
- c1 = iAto32("-2147483648",strlen("-2147483648"));
- printf("c1=%ld\r\n",c1);
- c2 = uiAtou32("4294967295",strlen("4294967295"));
- printf("c2=%u\r\n",c2);
- d1 = lAto64("-9223372036854775808",strlen("-9223372036854775808"));
- printf("d1=%ld\r\n",d1);
- d2 = ulAtou64("18446744073709551615",strlen("18446744073709551615"));
- printf("d2=%lu\r\n",d2);
- return 0;
- }
- /*-------------------(C) COPYRIGHT 2020 头条@单片机嵌入式爱好者 ----------------*/
- 运行结果如下:
- a1=-128
- a2=255
- b1=-32768
- b2=65535
- c1=2147483648
- c2=4294967295
- d1=-9223372036854775808
- d2=18446744073709551615
来源:单片机嵌入式爱好者