c - Convert string containing decimal digit to unsigned char -


i have array of char (string) contain decimal number. how convert unsigned char?

char  my_first_reg[2]; memcpy( my_first_reg, &my_str_mymatch[0], 1 ); my_first_reg[1] = '\0'; // my_first_reg contain reg number ... how convert unsigned char   

to convert my_first_reg[0] ascii character numeric value:

unsigned char value = my_first_reg[0] - '0'; 

this works because digits in ascii table sequential:

     '0' = 0x30 = 48     '1' = 0x31 = 49     '2' = 0x32 = 50     '3' = 0x33 = 51     '4' = 0x34 = 52     ...     '9' = 0x39 = 57 

the above converting 1 character. if have longer string, consider using atoi(), strtol(), sscanf(), or similar.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -