# Number (decimal) in ascii -> binary

#### Rlern

Joined Nov 25, 2021
20
Hello,

I have an homework to do, but I am a bit lost on how I am supposed to realise it.
I attach a file with the explanation of the task.
If someone can give me some explanation on how to realise it?

Thanks!

#### Attachments

• 50.5 KB Views: 27

#### DickCappels

Joined Aug 21, 2008
10,104
Do you know how numerals are represented in ASCII?

Can you convert, for example ASCII 5 ($35 or 0011 1001) into binary 5? Thread Starter #### Rlern Joined Nov 25, 2021 20 Do you know how numerals are represented in ASCII? Can you convert, for example ASCII 5 ($35 or 0011 1001) into binary 5?
If I understood correctly, numbers which are encoded as text will be treated as text
ASCII "0" -> 30 -> '0011' '0000'
ASCII "5" -> 35 -> '0011' '0101'

Right?

#### DickCappels

Joined Aug 21, 2008
10,104
Right!
So far, so good.
That implies that you can also convert binary to ASCII.
If you have the binary value corresponding to 95 decimal (0101 1111, what method would you use to convert that to ASCII?

#### Rlern

Joined Nov 25, 2021
20
I think (i am not sure that ascii can be represented in 8 bits or less.
So I will try to read each number to do this (with for example : 01011111)

0*2^8+1*2^7+....+1*2^0=95
But to know what this number is associated in the ASCII table, I don't know at all

#### atferrari

Joined Jan 6, 2004
4,730
Not intending to disturb the thread, what "variable-length" actually means?

#### ericgibbs

Joined Jan 29, 2010
18,226
hi R,
Do you have a copy of this table.?
E

#### Rlern

Joined Nov 25, 2021
20
Not intending to disturb the thread, what "variable-length" actually means?
This is one of my problem too, I have no idea what does it mean, and I don't understand the goal of auxiliary subroutine too

hi R,
Do you have a copy of this table.?
E
View attachment 253486
Yes I have it, but the problem is that it is supposed to be directly in the code, not by looking a table I think
By the way, the teacher gave us a part of code to help us, I will joint it now

Last edited:

#### dl324

Joined Mar 30, 2015
16,146
Not intending to disturb the thread, what "variable-length" actually means?
This is one of my problem too, I have no idea what does it mean, and I don't understand the goal of auxiliary subroutine too
The number can be 1-5 characters, representing 0 - 65535.
Yes I have it, but the problem is that it is supposed to be directly in the code, not by looking a table I think
Can you see the relationship between the binary, hex, octal, or decimal code for the character '1' and the number 1?

#### xox

Joined Sep 8, 2017
831
This is one of my problem too, I have no idea what does it mean, and I don't understand the goal of auxiliary subroutine too

Yes I have it, but the problem is that it is supposed to be directly in the code, not by looking a table I think
By the way, the teacher gave us a part of code to help us, I will joint it now

Consider the following equivalent representations of the number sixty-five (which also just so happens to be the numerical equivalent of the ASCII symbol for the letter 'A').

Binary: 1000001 = 1*2^7 + 1*2^0

Octal: 101 = 1*8^2 + 1*8^0

Decimal: 65 = 6*10^1 + 5*10^0

As you can see each digit in the number corresponds to a coefficient which gets multiplied by some power of the given number base.

So now let's look at a function that can convert any whole number into any given symbolic representation:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef size_t whole;

void print_digits(whole number, const char* digits) {
whole base = strlen(digits);
/*
Reversing the number just makes it a little easier to work with...
*/
whole reverse = 0;
do {
reverse *= base;
reverse += number % base;
number /= base;
} while (number != 0);
/*
Now we just extract the reversed digits for the given base
*/
do {
whole index = reverse % base;
putchar(digits[index]);
reverse /= base;
} while (reverse != 0);
}

void display(const char* preamble, whole number, const char* digits) {
printf("%12s: ", preamble);
print_digits(number, digits);
putchar('\n');
}

int main(int argc, char** argv) {
if (argc == 1) {
puts("Convert numbers to different bases");
printf("Usage: %s [...VALUES...]\n", argv[0]);
} else
for (;;) {
char* text = *(++argv);
if (text == NULL)
break;
whole next = atoi(text);
printf("-- %zu ---\n", next);
display("Binary", next, "01");
display("Base-3", next, "012");
display("Octal", next, "01234567");
display("Decimal", next, "0123456789");
}
}
There are much more efficient methods of course but hopefully that should at least give you a bit more concrete idea of how to approach this sort of problem.

#### Rlern

Joined Nov 25, 2021
20
The number can be 1-5 characters, representing 0 - 65535.
Can you see the relationship between the binary, hex, octal, or decimal code for the character '1' and the number 1?
Why until 65535 and not more? (maybe it's obvious but I don't know)
I think that it's the same signification for 0 to 9 but It change after, for 11 in decimal it will be B in Hexa ?

#### Rlern

Joined Nov 25, 2021
20
Consider the following equivalent representations of the number sixty-five (which also just so happens to be the numerical equivalent of the ASCII symbol for the letter 'A').

Binary: 1000001 = 1*2^7 + 1*2^0

Octal: 101 = 1*8^2 + 1*8^0

Decimal: 65 = 6*10^1 + 5*10^0

As you can see each digit in the number corresponds to a coefficient which gets multiplied by some power of the given number base.

So now let's look at a function that can convert any whole number into any given symbolic representation:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef size_t whole;

void print_digits(whole number, const char* digits) {
whole base = strlen(digits);
/*
Reversing the number just makes it a little easier to work with...
*/
whole reverse = 0;
do {
reverse *= base;
reverse += number % base;
number /= base;
} while (number != 0);
/*
Now we just extract the reversed digits for the given base
*/
do {
whole index = reverse % base;
putchar(digits[index]);
reverse /= base;
} while (reverse != 0);
}

void display(const char* preamble, whole number, const char* digits) {
printf("%12s: ", preamble);
print_digits(number, digits);
putchar('\n');
}

int main(int argc, char** argv) {
if (argc == 1) {
puts("Convert numbers to different bases");
printf("Usage: %s [...VALUES...]\n", argv[0]);
} else
for (;;) {
char* text = *(++argv);
if (text == NULL)
break;
whole next = atoi(text);
printf("-- %zu ---\n", next);
display("Binary", next, "01");
display("Base-3", next, "012");
display("Octal", next, "01234567");
display("Decimal", next, "0123456789");
}
}
There are much more efficient methods of course but hopefully that should at least give you a bit more concrete idea of how to approach this sort of problem.
Thanks I will try to understand how to use this code
But I still don't understand what is the goal of the "hint" : doing subroutine? Because the multiplication by 10 is useful to have a decimal number, not a binary number?

#### MrChips

Joined Oct 2, 2009
29,848
ASCII<->binary is one of those fundamental concepts that beginners have difficulty with. It would be very beneficial to spend the time now to try to understand these concepts.

As far as the computer is concerned, everything is binary.
Decimal, hexadecimal, ASCII are all human constructs for humans to wrap their heads around.

So, to begin,
4 bits can represent 16 things.
The total possible combinations of 4 bits are:
0000
0001
0010
:
:
1110
1111

We can write out the total number of different patterns for any number of bits:

n-bit #patterns
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536

You will notice that the number increases by multiples of 2. In other words, the total number of combinations is 2^n.
Memorize this table because it will come in handy later in life.

Thus, for a 16-bit pattern, there are 65536 possible patterns.
We can use this to represent 65536 different colours, for example, or even 65536 different numbers which do not even have to be in sequence.

So to keep it simple, let us use this to represent the numbers starting at 0.

0000 0000 0000 0000 = 0
0000 0000 0000 0000 = 1
:
1111 1111 1111 1110 = 65534
1111 1111 1111 1111 = 65535

Notice that the number sequence starting a 0 ends at 65535 and not 65536.

#### Rlern

Joined Nov 25, 2021
20
ASCII<->binary is one of those fundamental concepts that beginners have difficulty with. It would be very beneficial to spend the time now to try to understand these concepts.

As far as the computer is concerned, everything is binary.
Decimal, hexadecimal, ASCII are all human constructs for humans to wrap their heads around.

So, to begin,
4 bits can represent 16 things.
The total possible combinations of 4 bits are:
0000
0001
0010
:
:
1110
1111

We can write out the total number of different patterns for any number of bits:

n-bit #patterns
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536

You will notice that the number increases by 2. In other words, the total number of combinations is 2^n.
Memorize this table because it will come in handy later in life.

Thus, for a 16-bit pattern, there are 65536 possible patterns.
We can use this to represent 65536 different colours, for example, or even 65536 different numbers which do not even have to be in sequence.

So to keep it simple, let us use this to represent the numbers starting at 0.

0000 0000 0000 0000 = 0
0000 0000 0000 0000 = 1
:
1111 1111 1111 1110 = 65534
1111 1111 1111 1111 = 65535

Notice that the number sequence starting a 0 ends at 65535 and not 65536.
Okay I understand this now, I wasn't sure at all about this number, thanks for the explanation

#### MrSalts

Joined Apr 2, 2020
2,767
If you think a bit harder, the number 10 (decimal) would take four bits of memory to create in binary as 1010 (binary). So the question becomes, why stop there. There are six unused combinations that are possible but unused if we stop at 10 in a 4-bit register. So, to be more efficient, computer scientists fill all four with all 16 possible combinations.

if you have an 8-bitnumber, you can get 0-127. It has always been thought that counting in binary to fill the registers and roll over with standard flip-flops is much easier to calculate. Stopping at 10 and somehow shifting to to the next register just doesn't make for simple and efficient hardware snd the only time it is important how the number is represented is when it must be displayed on screen so you can read it. That is a quick calculation with modulo Divide (%) as shown in the example code above to get the place-value of each digit. Annoying, yes (kind-of) but much less complicated than keeping tract of all the individual digits in all the variables in each step of the code. So, in the end, much less annoying to do it once at the end when the result needs to be displayed.

#### MrChips

Joined Oct 2, 2009
29,848
Just be careful here.

1010 is not necessarily decimal 10.

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

is just a complete set of possible patterns of 4 bits.
It can represent 16 different colours.
We can also use it to represent 16 different numbers:

Just to be crazy:

0000 = 99
0001 = 108
0010 = -32
0011 = 12345
0100 = 23
0101 = 16
0110 = -84
0111 = 987
1000 = 34
1001 = 9999
1010 = 512678234
1011 = 3546
1100 = 56567
1101 = 8888
1110 = 77
1111 = 24424

Sounds crazy, but just to make my point, it could be 16 different telephone numbers.
More realistically, we could use it to represent 16 RGB 24-bit colours, for example,

0000 = BLACK = 0, 0, 0
0001 = RED = 255 ,0, 0
:
1000 = GRAY = 128, 128, 128
1001 = BLUE = 0, 0 , 255
:
1111 = WHITE = 255, 255, 255

Thus, it represents whatever you want it to represent so long as everyone who needs to use this table agrees on what it represents.

#### dl324

Joined Mar 30, 2015
16,146
Why until 65535 and not more? (maybe it's obvious but I don't know)
That's what was given in the problem statement and it happens to be the largest unsigned integer that will fit in 16 bits.

#### MrAl

Joined Jun 17, 2014
10,906
Hello,

I have an homework to do, but I am a bit lost on how I am supposed to realise it.
I attach a file with the explanation of the task.
If someone can give me some explanation on how to realise it?

Thanks!
It looks to me like the number to convert will be stored in ASCII format one digit after another.
So for example the memory for the decimal number 821 would contain:
8
2
1

however that would be in ascii format:
0x38
0x32
0x31

So the first step i would do is to convert that to BCD by subtracting 0x30 from each memory location leaving you with this in binary BCD:
00001000
00000010
00000001

and you can convert that to actual BCD or use it as is (BCD with extended zeros forming 8 bit BCD),
and next you would convert that into a full binary number, so you would do a BCD to binary conversion. There are different ways to do that you can look on the web.
One way that is not that efficient is to create a adder routine that can add 16 bit numbers.
Then, for each entry add the power of 10 for each digit. For our example above, since the 8 is in the hundreds position add 100 binary 8 times (of course if you create a multiplication routine you can multiply instead but that takes more code space if you dont already have the routine).
For the 2 you would add 10 twice to the previous sum.
For the 1 you would add 1 1 time which is just adding 1.
So you would get 100 added 8 times plus 10 added 2 times plus 1.
The result will be the full binary equivalent of decimal 821.

See if you can handle that kind of coding. That's the most straightforward way i think.

#### Rlern

Joined Nov 25, 2021
20
Hello, sorry for the late of my answer but I had a lot of exams between that and I didn't have time.
I read again all of the answer but I am a bit lost on how to do it in assembler. Doing it in C-code or another langage seems okay, but doing it in assembler is another thing...
I have a part of the code here, but I don't know how to complete the "TODO".

#### djsfantasi

Joined Apr 11, 2010
9,131
Doing it in C gives you a direct map to doing it in assembler. You just have to understand what the C statement does and how to do the same thing in assembler.

Seems like the latter half of that process is where you’re stuck in converting to assembler.

The trick here is to understand a shortcut in assembler to multiply by 10. Go back to algebra. Multiplying by 10 is the same as adding two different factors. 10 can be the sum of what two numbers? There are many possible answers, but you have a hint in that you want to pick two numbers that are both easy to multiply with in assembler.

Think of how you multiply a number by 4 in assembler (and no, that isn’t necessarily one of the two numbers). Does the answer to this hypothetical question give you a hint as to the two numbers (which total to 10) that you should pick?