What I want to do is for a Brainfuck code to print out the ascii value of the input. For example, typing in an input of "a" will give an output of 97. The python equivalent of this is print(ord(input()))
. What I'm thinking is that once I get the input with the ,
command, I can split the input value's digits into separate cells, and then print each cell individually. What I mean by this is let's say you type in an input of a
. The ,
command will store the ascii value of a
in the first cell(cell 0), which is 97 in this case. Then I run some algorithm that will split the 97 into its individual digits. So, in this case, cell 1 will have a value of 0(because 97 has a hundred digit of 0), cell 2 will have a value of 9, and cell 3 will have a value of 7. Then we can add 48 to each of those cells(0 has an ascii value of 48) and print each cell individually, starting from cell 1(the hundreds place). The problem I'm facing is writing the digit separation algorithm. I can't seem to make it work. My idea is to subtract 100 from the original number until that number is less than 100 while keeping track of how many times 100 has been subtracted, then repeatedly subtract 10, and finally we are left with the ones place. But the problem with this idea is that I have no idea how to track if the number falls under 100 or 10. Any suggestions or ideas? Thanks for the help in advance.
Asked
Active
Viewed 576 times
2

Aiden Chow
- 391
- 6
- 17
-
2@ggorlen I'm not talking about python I'm talking about Brainfuck. I know how to get the ascii value of an input in python. I even put that as an example. I want to know how to do the same thing in Brainfuck. – Aiden Chow Jul 16 '20 at 01:15
-
Sorry, I didn't pay attention. Does [this](https://stackoverflow.com/questions/6168584/brainfuck-compare-2-numbers-as-greater-than-or-less-than) help? – ggorlen Jul 16 '20 at 01:46
-
I mean I guess I can use that but my code might get really messy. – Aiden Chow Jul 16 '20 at 01:52
-
Actually, I think I found a dupe: https://stackoverflow.com/questions/12569444/printing-a-number-in-brainfuck – ggorlen Jul 16 '20 at 01:55
-
Does thishelp you? [Printing a number in brainfuck?](https://stackoverflow.com/questions/12569444/printing-a-number-in-brainfuck) – DaCuteRaccoon Mar 03 '22 at 00:43
1 Answers
0
What you are trying to implement is called "divmod". divmod is a function that divides two numbers (in your case positive integers) and stores the result and the remainder. Implementations for this in brainfuck exist: Divmod algorithm in brainfuck
Good luck!

ben rapoport
- 101
- 5