Friday 17 November 2017

Brainfuck: Convert ASCII to decimal number

First of all when you read the input from user, it will be stored as ASCII equivalent.

Decimal Number
ASCII Equivalent
0
48
1
49
2
50
3
51
4
52
5
53
6
54
7
55
8
56
9
57

By subtracting the ASCII equivalent of a number with 48 we can get the actual number.
0 = 48 – 48
1 = 49 – 48
2 = 50 – 48
3 = 51 – 48
4 = 52 – 48
5 = 53 - 48
6 = 54 - 48
7 = 55 - 48
8 = 56 – 48
9 = 57 – 48

Below snippet reads a number and convert it to decimal.

,                #Read a number

# Logic to convert ASCII numbers to their decimal format
>
++++++++         # Init second cell to 8 
[
 -
 <
 ------
 >
]
<


Previous                                                 Next                                                 Home

No comments:

Post a Comment