How to extract the 10 and 16 binary numbers from a string, see the following code:
#include <stdio.h>typedef char TUINT8; typedef int TUINT32; TUINT32 read_decnumber (const tuint8* str); TUINT32 read_hexnumber (const tuint8* str); int main (void) {int ret = Read_decnumber ("n"), int d = read_hexnumber ("A");p rintf ("Converts a number in a string to 10 binary:%d\n", ret); printf ("Convert 16 binary in string to 10 binary:%d\n", d); return 0;} Converts a number in a string to 10 decimal TUINT32 read_decnumber (const tuint8* str) {TUINT32 value; if (! Str) {return 0; } value = 0; while (*str >= ' 0 ') && (*str <= ' 9 ')) {value = value*10 + (*str-' 0 '); str++; } return value; Converts a 16 binary number in a string to a 10-digit TUINT32 read_hexnumber (const tuint8* str) {TUINT32 value; if (! Str) {return 0; } value = 0; while (1) {if (*str >= ' 0 ') && (*str <= ' 9 ')) {value = value*16 + (*str-' 0 ' ); } else if ((*str >= ' a ') && (*str <= ' F ')) {value = value*16 + (*str-' a ') + 10; } else if (*str >= ' a ') && (*str <= ' F ')) {value = value*16 + (*str-' a ') + 10; } else {break; } str++; } return value;
Operation Result:
C Language Implementation string (10 binary and 16 binary) to decimal number