Most languages provide convenient library routines to perform these operations. Occasionally you might need to do this for yourself. This shows you how to deal with positive integer/real numbers.

Here are some examples of character-codes and characters:

- unicode character-code x32 is the number character: 2
- unicode character-code x54 is the English character: T
- unicode character-code x03C0 is the math character: π
- unicode character-code x4E2D is the Chinese character: 中
- unicode character-code x06A0 is the Arabic character: ڠ
- unicode character-code x1F60A is the emoji character: 😊

`digit = character-code -'0';`

for '0' to '9'`digit = character-code -'A' + 10;`

for 'A' to 'Z'

- initialize:
`value = 0`

- loop over the characters in the string, left to right
- convert the character to a digit
- compute:
`value = value * base + digit`

- repeat steps 3 and 4 for all the characters
`value`

now contains the correct number

- value = 0
- "
**2**63" - value = 10 * 0 + 2 = 2 - "2
**6**3" - value = 10 * 2 + 6 = 26 - "26
**3**" - value = 10 * 26 + 3 = 263

- value = 0
- "
**0**10011" - value = 2 * 0 + 0 = 0 - "0
**1**0011" - value = 2 * 0 + 1 = 1 - "01
**0**011" - value = 2 * 1 + 0 = 2 - "010
**0**11" - value = 2 * 2 + 0 = 4 - "0100
**1**1" - value = 2 * 4 + 1 = 9 - "01001
**1**" - value = 2 * 9 + 1 = 19

- value = 0
- "
**C**8A" - value = 16 * 0 + 12 = 12 - "C
**8**A" - value = 16 * 12 + 8 = 200 - "C8
**A**" - value = 16 * 200 + 10 = 3210

Alternatively, one can convert only the fractional portion of the string to an integer value using the procedure descibed above. Then do the division by the power of the base and add the result to the integer portion of the number (i.e. the part before the '.'). Under what circumstances might this be a better solution than the original one?

- value = 0
- "
**0**10.011" - value = 2 * 0 + 0 = 0 - "0
**1**0.011" - value = 2 * 0 + 1 = 1 - "01
**0**.011" - value = 2 * 1 + 0 = 2 - "010.
**0**11" - value = 2 * 2 + 0 = 4 - "010.0
**1**1" - value = 2 * 4 + 1 = 9 - "010.01
**1**" - value = 2 * 9 + 1 = 19

` ``x = x * (base`^{N} / base^{N})

Now, by algebraic manipulation:
` ``x = (x * base`^{N}) / base^{N}

But, `x * base`^{N}

is simply `x`

with the
'.' move right by N places. Thus `x`

has been transformed from
a number containing a '.', to one where the '.' is at the right end and
is an integer. So, use the integer conversion, then correct the result by
doing the division.
Alternatively, convert the strings left and right of the '.' to values
(2 and 3) in the previous example. The final result is 2 + 3/2^{3}
(because there are 3 haracters to the right of '.'), resulting in 2.375.

- initialize the output string to empty (i.e. "")
- compute: r = value % base (i.e. the remainder)
- compute: value = value / base
- convert r to a character (e.g. 10 to 'A' in hex) For decimal digits the expression (char)(r + '0') will give the correct character.
- prepend the character to the ouput string
- if value is 0, quit; otherwise return to step 2

- output = ""
- r = 156 % 10 = 6; val = 156 / 10 = 15; output = "
**6**" - r = 015 % 10 = 5; val = 015 / 10 = 01; output = "
**5**6" - r = 001 % 10 = 1; val = 001 / 10 = 00; output = "
**1**56"

- output = ""
- r = 23 % 2 = 1; val = 23 / 2 = 11; output = "
**1**" - r = 11 % 2 = 1; val = 11 / 2 = 05; output = "
**1**1" - r = 05 % 2 = 1; val = 05 / 2 = 02; output = "
**1**11" - r = 02 % 2 = 0; val = 02 / 2 = 01; output = "
**0**111" - r = 01 % 2 = 1; val = 01 / 2 = 00; output = "
**1**0111"

- output = ""
- r = 231 % 16 = 07; val = 23 / 16 = 14; output = "
**7**" - r = 014 % 16 = 14; val = 14 / 16 = 0; output = "
**E**7"

- output=""
- r = 231 % 2 = 1; val = 231 / 2 = 115; output = "
**1**" - r = 115 % 2 = 1; val = 115 / 2 = 057; output = "
**1**1" - r = 057 % 2 = 1; val = 028 / 2 = 057; output = "
**1**11" - r = 028 % 2 = 0; val = 028 / 2 = 014; output = "
**0**111" - r = 014 % 2 = 0; val = 014 / 2 = 007; output = "
**0**0111" - r = 007 % 2 = 1; val = 007 / 2 = 003; output = "
**1**00111" - r = 003 % 2 = 1; val = 003 / 2 = 001; output = "
**1**100111" - r = 001 % 2 = 1; val = 001 / 2 = 000; output = "
**1**1100111"

- 1110
**0111**gives the value 7 **1110**0111 gives the value E

- append '.' to the output
- Multiply the fractional part by the base
- Convert the integer portion of the result to a character
- append the character to the output
- discard the integer portion of the result and return to step 2
- repeat for as many digits as needed, or until the fractional part becomes 0

- output = "10." convert integer portion and add '.'
- fract = 0.375; 0.375 * 2 =
**0**.75; output = "10.**0** - fract = 0.750; 0.750 * 2 =
**1**.50; output = "10.0**1** - fract = 0.500; 0.500 * 2 =
**1**.00; output = "10.01**1**

- value = 0
- "
**C**F" - value = 26 * 0 + ('C' - 'A') + 1 = 00 + 3 = 3 - "C
**F**" - value = 26 * 3 + ('F' - 'A') + 1 = 78 + 6 = 84

- output = ""
- val = 47 -1 = 46; r = 46 % 26 = 20; val = 46 / 26 = 1; output = "
**T**" - val = 01 -1 = 00; r = 00 % 26 = 00; val = 00 / 26 = 0; output = "
**A**T"

(c) Fritz Sieker, 2009-2016