Write a C program that implements that ASCII table given on Slide 30 of the firs
ID: 3877680 • Letter: W
Question
Write a C program that implements that ASCII table given on Slide 30 of the first slide set. That ASCII table will be reproduced below for reference.
Your program should first print out the column headings. It should then print out the binary, octal, decimal, hexadecimal and character versions of the ASCII characters from 32 to 126 (decimal), as indicated in the table. Two vertical columns should be sufficient.
Bin Oct Dec Hex Char Bin Oct Dec Hex Char
32
33
Note that the columns should run top to bottom (instead of left to right). The range between 32 and 126 has an odd number of entries. Therefore, the left hand column will have one more entry in it than the right hand column.
As we will see shortly, the decimal, octal, and hexadecimal versions of these numbers can be easily displayed using the built-in conversion codes of printf(). The binary versions, however, are more difficult. Two algorithms for converting a decimal number to binary are (1) repeatedly divide the decimal number and its quotients by 2 and collect the remainders (the last remainder collected will be the most significant digit of the result), (2) create the hexadecimal equivalent of the original decimal number and use that hexadecimal intermediate to create the binary. The latter technique depends on the fact that the 16 hexadecimal digits (ranging from 0 to F) exactly correspond to the 16 numbers that can be created out of four bits (ranging from 0000 to 1111). The four bit binary patterns can be created from the hexadecimal digits and concatenated to produce the final result.
For this assignment, let’s use the first algorithm: repeated divisions by 2. For an example, let’s say our current decimal number is 74 (corresponding to a ‘J’ in the ASCII table). If we use this method, we would have:
74 / 2 = 37 R0
37 / 2 = 18 R1
18 / 2 = 9 R0
9 / 2 = 4 R1
4 / 2 = 2 R0
2 / 2 = 1 R0
1 / 2 = 0 R1
After each division we capture the remainder and then divide the quotient from the previous division by 2. The process stops when the quotient becomes 0 (which it always will). The remainders are then displayed in reverse order (i.e., the last remainder collected is the most significant digit of the result) to produce the final answer: 1001010. Note that these digits should be printed out as they are in the chart, with a space between the first and second nibbles. Furthermore, for the purposes of this assignment, let’s also display the leading 0 bit (even though it is not shown in the table). The final result would then be displayed as:
0100 1010
The calculation of the binary should be placed in its own function, which ever method you use. Note that you should never return a pointer from a function to something that was declared locally within that function. Therefore, if you intend to return any kind of an array from a function, it must be passed into the function as an argument. (Alternatively, you could dynamically allocate the array. But we will discuss that issue later in the course.)
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
void toBinary(int n, char binary[])
{
int rem;
//initialize to 0
for(int i = 0; i < 8; i++)
binary[i] = '0';
binary[8] = '';
for(int i = 7; i >= 0 && n != 0; i--)
{
rem = n % 2;
if(rem == 1)
binary[i] = '1';
else
binary[i] = '0';
n /= 2;
}
}
int main()
{
char binary[9];
int half = (126 - 32)/2;
int c1 = 32, c2 = c1 + half + 1;
printf("%10s %4s %4s %4s %4s", "Binary", "Oct", "Dec", "Hex", "Char");
printf(" %10s %4s %4s %4s %4s ", "Binary", "Oct", "Dec", "Hex", "Char");
for(; c1 <= 32 + half ; c1++, c2++)
{
toBinary(c1, binary);
printf("%10s %4o %4d %4X %4c", binary, c1, c1, c1, c1);
if(c2 < 127)
{
toBinary(c2, binary);
printf(" %10s %4o %4d %4X %4c ", binary, c2, c2, c2, c2);
}
}
printf(" ");
}
Binary Oct Dec Hex Char Binary Oct Dec Hex Char
00100000 40 32 20 01010000 120 80 50 P
00100001 41 33 21 ! 01010001 121 81 51 Q
00100010 42 34 22 " 01010010 122 82 52 R
00100011 43 35 23 # 01010011 123 83 53 S
00100100 44 36 24 $ 01010100 124 84 54 T
00100101 45 37 25 % 01010101 125 85 55 U
00100110 46 38 26 & 01010110 126 86 56 V
00100111 47 39 27 ' 01010111 127 87 57 W
00101000 50 40 28 ( 01011000 130 88 58 X
00101001 51 41 29 ) 01011001 131 89 59 Y
00101010 52 42 2A * 01011010 132 90 5A Z
00101011 53 43 2B + 01011011 133 91 5B [
00101100 54 44 2C , 01011100 134 92 5C
00101101 55 45 2D - 01011101 135 93 5D ]
00101110 56 46 2E . 01011110 136 94 5E ^
00101111 57 47 2F / 01011111 137 95 5F _
00110000 60 48 30 0 01100000 140 96 60 `
00110001 61 49 31 1 01100001 141 97 61 a
00110010 62 50 32 2 01100010 142 98 62 b
00110011 63 51 33 3 01100011 143 99 63 c
00110100 64 52 34 4 01100100 144 100 64 d
00110101 65 53 35 5 01100101 145 101 65 e
00110110 66 54 36 6 01100110 146 102 66 f
00110111 67 55 37 7 01100111 147 103 67 g
00111000 70 56 38 8 01101000 150 104 68 h
00111001 71 57 39 9 01101001 151 105 69 i
00111010 72 58 3A : 01101010 152 106 6A j
00111011 73 59 3B ; 01101011 153 107 6B k
00111100 74 60 3C < 01101100 154 108 6C l
00111101 75 61 3D = 01101101 155 109 6D m
00111110 76 62 3E > 01101110 156 110 6E n
00111111 77 63 3F ? 01101111 157 111 6F o
01000000 100 64 40 @ 01110000 160 112 70 p
01000001 101 65 41 A 01110001 161 113 71 q
01000010 102 66 42 B 01110010 162 114 72 r
01000011 103 67 43 C 01110011 163 115 73 s
01000100 104 68 44 D 01110100 164 116 74 t
01000101 105 69 45 E 01110101 165 117 75 u
01000110 106 70 46 F 01110110 166 118 76 v
01000111 107 71 47 G 01110111 167 119 77 w
01001000 110 72 48 H 01111000 170 120 78 x
01001001 111 73 49 I 01111001 171 121 79 y
01001010 112 74 4A J 01111010 172 122 7A z
01001011 113 75 4B K 01111011 173 123 7B {
01001100 114 76 4C L 01111100 174 124 7C |
01001101 115 77 4D M 01111101 175 125 7D }
01001110 116 78 4E N 01111110 176 126 7E ~
01001111 117 79 4F O
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.