How exactly does binary code get converted into letters? - binary

Out of curiosity, how exactly does binary code get converted into letters? I know there are sites that automatically convert binary to words for you but I wanna understand the specific, intermediary steps that binary code goes through before being converted into letters.

Here's a way to convert binary numbers to ASCII characters that is often simple enough to do in your head.
1 - Convert every 4 binary digits into one hex digit.
Here's a binary to hex conversion chart:
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = a (the hex number a, not the letter a)
1011 = b
1100 = c
1101 = d
1110 = e
1111 = f
(The hexadecimal numbers a through f are the decimal numbers 10 through 15. That's what hexadecimal, or "base 16" is - instead of each digit being capable of representing 10 different numbers [0 - 9], like decimal or "base 10" does, each digit is instead capable of representing 16 different numbers [0 - f].)
Once you know that chart, converting any string of binary digits into a string of hex digits is simple.
For example,
01000100 = 0100 0100 = 44 hex
1010001001110011 = 1010 0010 0111 0011 = a273 hex
Simple enough, right? It is a simple matter to convert a binary number of any length into its hexadecimal equivalent.
(This works because hexadecimal is base 16 and binary is base 2 and 16 is the 4th power of 2, so it takes 4 binary digits to make 1 hex digit. 10, on the other hand, is not a power of 2, so we can't convert binary to decimal nearly as easily.)
2 - Split the string of hex digits into pairs.
When converting a number into ASCII, every 2 hex digits is a character. So break the hex string into sets of 2 digits.
You would split a hex number like 7340298b392 this into 6 pairs, like this:
7340298b392 = 07 34 02 98 b3 92
(Notice I prepended a 0, since I had an odd number of hex digits.)
That's 6 pairs of hex digits, so its going to be 6 letters. (Except I know right away that 98, b3 and 92 aren't letters. I'll explain why in a minute.)
3 - Convert each pair of hex digits into a decimal number.
Do this by multiplying the (decimal equivalent of the) left digit by 16, and adding the 2nd.
For example, b3 hex = 11*16 + 3, which is 110 + 66 + 3, which is 179.
(b hex is 11 decimal.)
4 - Convert the decimal numbers into ASCII characters.
Now, to get the ASCII letters for the decimal numbers, simply keep in mind that in ASCII, 65 is an uppercase 'A', and 97 is a lowercase 'a'.
So what letter is 68?
68 is the 4th letter of the alphabet in uppercase, right?
65 = A, 66 = B, 67 = C, 68 = D.
So 68 is 'D'.
You take the decimal number, subtract 64 for uppercase letters if the number is less than 97, or 96 for lowercase letters if the number is 97 or larger, and that's the number of the letter of the alphabet associated with that set of 2 hex digits.
Alternatively, if you're not afraid of a little bit of easy hex arithmetic, you can skip step 3, and just go straight from hex to ASCII, by remembering, for example, that
hex 41 = 'A'
hex 61 = 'a'
So subtract 40 hex for uppercase letters or 60 hex for lowercase letters, and convert what's left to decimal to get the alphabet letter number.
For example
01101100 = 6c, 6c - 60 = c = 12 decimal = 'l'
01010010 = 52, 52 - 40 = 12 hex = 18 decimal = 'R'
(When doing this, it's helpful to remember that 'm' (or 'M') is the 13 letter of the alphabet. So you can count up or down from 13 to find a letter that's nearer to the middle than to either end.)
I saw this on a shirt once, and was able to read it in my head:
01000100
01000001
01000100
I did it like this:
01000100 = 0100 0100 = 44 hex, - 40 hex = ucase letter 4 = D
01000001 = 0100 0001 = 41 hex, - 40 hex = ucase letter 1 = A
01000100 = 0100 0100 = 44 hex, - 40 hex = ucase letter 4 = D
The shirt said "DAD", which I thought was kinda cool, since it was being purchased by a pregnant woman. Her husband must be a geek like me.
How did I know right away that 92, b3, and 98 were not letters?
Because the ASCII code for a lowercase 'z' is 96 + 26 = 122, which in hex is 7a. 7a is the largest hex number for a letter. Anything larger than 7a is not a letter.
So that's how you can do it as a human.
How do computer programs do it?
For each set of 8 binary digits, convert it to a number, and look it up in an ASCII table.
(That's one pretty obvious and straight forward way. A typical programmer could probably think of 10 or 15 other ways in the space of a few minutes. The details depend on the computer language environment.)

Assuming that by "binary code" you mean just plain old data (sequences of bits, or bytes), and that by "letters" you mean characters, the answer is in two steps. But first, some background.
A character is just a named symbol, like "LATIN CAPITAL LETTER A" or "GREEK SMALL LETTER PI" or "BLACK CHESS KNIGHT". Do not confuse a character (abstract symbol) with a glyph (a picture of a character).
A character set is a particular set of characters, each of which is associated with a special number, called its codepoint. To see the codepoint mappings in the Unicode character set, see http://www.unicode.org/Public/UNIDATA/UnicodeData.txt.
Okay now here are the two steps:
The data, if it is textual, must be accompanied somehow by a character encoding, something like UTF-8, Latin-1, US-ASCII, etc. Each character encoding scheme specifies in great detail how byte sequences are interpreted as codepoints (and conversely how codepoints are encoded as byte sequences).
Once the byte sequences are interpreted as codepoints, you have your characters, because each character has a specific codepoint.
A couple notes:
In some encodings, certain byte sequences correspond to no codepoints at all, so you can have character decoding errors.
In some character sets, there are codepoints that are unused, that is, they correspond to no character at all.
In other words, not every byte sequence means something as text.

Do you mean the conversion 011001100110111101101111 → foo, for example? You just take the binary stream, split it into separate bytes (01100110, 01101111, 01101111) and look up the ASCII character that corresponds to given number. For example, 01100110 is 102 in decimal and the ASCII character with code 102 is f:
$ perl -E 'say 0b01100110'
102
$ perl -E 'say chr(102)'
f
(See what the chr function does.) You can generalize this algorithm and have a different number of bits per character and different encodings, the point remains the same.

To read binary ASCII characters with great speed using only your head:
Letters start with leading bits 01. Bit 3 is on (1) for lower case, off (0) for capitals. Scan the following bits 4–8 for the first that is on, and select the starting letter from the same index in this string: “PHDBA” (think P.H.D., Bachelors in Arts). E.g. 1xxxx = P, 01xxx = H, etc. Then convert the remaining bits to an integer value (e.g. 010 = 2), and count that many letters up from your starting letter. E.g. 01001010 => H+2 = J.

http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/The_Characters.asp it just looks here... (not HERE but it has a table).
There are 8 bits in a byte. One byte can be one symbol. One bit is either on or off.

Why not just do this take 010010001001001 split it into two bits 8 letter each (01001000, 01001001). Then issue the powers
01001000. 01001001.
The first 8 ignore the first three they determine if it's capital or not, the go right to left doing powers of 2 (2^1, 2^2 2^3 2^4 2^5). So then add all the ones up , there's only one, and it = 8, and te eight letter in the alphabet is h so our first bit is the letter h, try it on the other bit

Related

How do I add two hexadecimal values?

this is a school assignment. I've been given homework and one of the problems is to figure out the value after adding two hexadecimal values.
0x80000000 and 0xD0000000. I understand that D = 13, but I don't understand how the answer is 15, because 8 + 13 = 23? Could someone explain what I am doing wrong, and what I should do instead?
Thanks!
It's easy if you think that every digit represents a quadruple, for example
0xDEADBEEF = 13*16⁷+14*16⁶+10*16⁵+13*16⁴+11*16³+14*16²+14*16¹+15*16⁰.
The above hexadecimal value needs an algorithm to translate to a format the the ALU can add, for instance binary numbers.
D is 13 in decimal because D is digit number 13 if A replaces 10 and so on (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F). The position of D is 7, so the number is 13*16⁷.
We notice that it is easier to LSB if we do this, and recognize that F is 15 in hexadecimal notation and therefore the number to the left will be 15*16⁰=15.
0xFF therefore means 15*16¹+15*16⁰=15*16+15=255 (you can check this with a calculator).
Now the algorithm is hopefully clear.
3735928559 is the decimal value of DEADBEEF because ==13*16^7+14*16^6+10*16^5+13*16^4+11*16^3+14*16^2+14*16^1+15*16^0=3735928559.
Some times I convert the hexadecimal into binary base 2 this because I feel more confident to do arithmetic with binary base 2than hexadecimal.
In order to do so you need to arrange every hexadecimal into group of 4 bits binary number.
hex 0x8 + 0xD
Convert to binary
binary 1000 + 1101 = 10101 = 21
group it again as 4 bits
0001 0101 = 0x15
I ignored if it's signed number and didn't used two's complement.

How to convert Binary to Decimal and Octal?

I have a BINARY number which i want to convert it into the DECIMAL and OCTAL.
(0100 1111 1011 0010)2
I know how to convert it into the decimal. But the question making me confuse. Because middle of every 4 digits there is a space "0101 1111"
can u help me how to understand this question.
Thanks
First of all, make sure that number you are converting into Decimal and Octal is actually 'Binary' and not 'Binary Coded Decimal (BCD)'. Usually when the number is grouped into 4 binary digits, it represents a BCD instead of just binary.
So, once you make sure its actually binary and not BCD, the conversion to both decimal and octal are simple steps.
For binary to octal, you group the binary number into sets of 3 digits, starting form the Least Significant Bit(LSB or right-most) to the Most Significant Bit(MSB or left-most). Add leading zeros if a group of 3 digits can not be formed at the MSB.
Now convert each group of digits from binary to octal:
(000) -> 0
(001) -> 1
.
.
(111) -> 7
Finally put the numbers together, and there you have your binary converted to octal.
Eg:-
binary - 00101101
split into groups of 2: -> 000 101 101 -> 0 5 5 - > 55
Difference between'Binary Coded Decimal' and 'Binary':
For the decimal number 1248
the binary would simply be 10011100000
However, the BCD would be -> 0001 0010 0100 1000
The spaces are not part of the number, it's just to make it easier for humans to read. Conversion from binary to octal is simple. Group the binary digits into sets of 3 (from right to left, add extra 0s to the leftmost group, then convert each group individually. Your example:
0100 1111 1011 0010 -> 100 111 110 110 010 -> 47662
The space is just for readability. Especially nice if you try to convert this to hex, because 4 binary digits make up one hex-digit.
Firstly, those spaces are for human readability; just delete them. Secondly, If this is not for a computer program, simply open up the windows calculator, go to view, and select programmer. Then chose the bin radio button and type in your number. the qword radio button should be selected. If it's for a program, I will need to know what language to help you.
To convert octal to decimal very quickly there are two methods. You can actually do the actual calculation in bitshift. In programming, you should do bitshift.
Example octal number = 147
Method one: From left to right.
Step 1: First digit is one. Take that times 8 plus 4. Got 12.
Step 2: Take 12 times 8 + 7. Got 103, and 103 is the answer.
Ultimately you can use method one to convert any base into base 10.
Method one is reading from left to right of the string. Make a result holder for calculation. When you read the first leftmost digit, you add that to a result value. Each time you read a new digit, you take the result value and multiply that by the base of the number(for octal, that would be 8), then you add the value of the new digit to the result.
Method 2, bitshift:
Octal Number: 147.
Step 1: 1 = 1(bin) = Shift << 3 = 1000(result value)
Step 2: 4 = 100(bin) + 1000(result value) = 1100(result value)
Step 3: 1100(result value) Shift << 3 = 1100000
Step 4: 7 = 111(bin) + 1100000(result value) = 1100111
Step 5: 1100111 binary is 103 decimal.
In a programming loop, you can do something like the below, and it is lightning fast. The code is so simple that it can be converted into any programming language. Note that there isn't any error checking.
for ( int i = 0; i < length; i++ ){
c = (str.charAt(i) ^ 48);
if ( c > 7 ) return 0; // <- if char ^ 48 > 7 then that is not a valid octal number.
out = (out << 3) + c;
}

Differ between numbers and letters in binary

in binary how do you differ between numbers and letters? I believe that positive numbers begin with 0000 and negative with 1000, then add the next 4 digits so -5 would be 1000 0101.
I know capital/lowercase letters start with 0100 and 0110 so just wondering if I was right about the number thing.
Also, if you could can tell me how to do decimals or special symbols that would be great,
Thanks - Jon
Binary is just a representation of a value. It's true that for signed values, if MSB is 1 the value is negative and if the MSB is 0 the value is positive. However the second part of your statement is not correct. 1000 0101 is not -5, it's actually -123. To represent -5 you take the value 5, which is 0000 0101, invert all bits, and add one, giving you 1111 1011. This is called two's complement.
Your next statement
I know capital/lowercase letters start with 0100 and 0110
May not necessarily be true. It depends on the character encoding. In ASCII, for example, uppercase Latin letters A-Z range from 65 to 90, which can be represented in binary as 0100 0001 through 0101 1010, and the lowercase letters a-z are from 97 to 122, which is represented in binary as 0110 0001 through 0111 1010.
Also, if you could can tell me how to do decimals or special symbols that would be great
Again, depends on the encoding. If we're talking about ASCII, a decimal is 46, which in binary is 0010 1110.
Here's a table with all 8-bit ASCII characters: http://ascii-code.com/
If you want other characters beyond ASCII, you'll need to look into Unicode.

Decimal/Hexadecimal/Binary Conversion

Right now I'm preparing for my AP Computer Science exam, and I need some help understanding how to convert between decimal, hexadecimal, and binary values by hand. The book that I'm using (Barron's) includes an example but does not explain it very well.
What are the formulas that one should use for conversion between these number types?
Are you happy that you understand number bases? If not, then you will need to read up on this or you'll just be blindly following some rules.
Plenty of books would spend a whole chapter or more on this...
Binary is base 2, Decimal is base 10, Hexadecimal is base 16.
So Binary uses digits 0 and 1, Decimal uses 0-9, Hexadecimal uses 0-9 and then we run out so we use A-F as well.
So the position of a decimal digit indicates units, tens, hundreds, thousands... these are the "powers of 10"
The position of a binary digit indicates units, 2s, 4s, 8s, 16s, 32s...the powers of 2
The position of hex digits indicates units, 16s, 256s...the powers of 16
For binary to decimal, add up each 1 multiplied by its 'power', so working from right to left:
1001 binary = 1*1 + 0*2 + 0*4 + 1*8 = 9 decimal
For binary to hex, you can either work it out the total number in decimal and then convert to hex, or you can convert each 4-bit sequence into a single hex digit:
1101 binary = 13 decimal = D hex
1111 0001 binary = F1 hex
For hex to binary, reverse the previous example - it's not too bad to do in your head because you just need to work out which of 8,4,2,1 you need to add up to get the desired value.
For decimal to binary, it's more of a long division problem - find the biggest power of 2 smaller than your input, set the corresponding binary bit to 1, and subtract that power of 2 from the original decimal number. Repeat until you have zero left.
E.g. for 87:
the highest power of two there is 1,2,4,8,16,32,64!
64 is 2^6 so we set the relevant bit to 1 in our result: 1000000
87 - 64 = 23
the next highest power of 2 smaller than 23 is 16, so set the bit: 1010000
repeat for 4,2,1
final result 1010111 binary
i.e. 64+16+4+2+1 = 87 in decimal
For hex to decimal, it's like binary to decimal, only you multiply by 1,16,256... instead of 1,2,4,8...
For decimal to hex, it's like decimal to binary, only you are looking for powers of 16, not 2. This is the hardest one to do manually.
This is a very fundamental question, whose detailed answer, on an entry level could very well be a couple of pages. Try to google it :-)

Converting binary to hexadecimal?

Just wondering on how I would go about converting binary to hexadecimal??
Would I first have to convert the binary to decimal and then to hexadecimal??
For example, 101101001.101110101010011
How would I go about converting a complex binary such as the above to hexadecimal?
Thanks in advance
Each 4 bits of a binary number represents a hexadecimal digit. So the best way to convert from binary to hexadecimal is to pad the binary number with leading zeroes so that the number of bits is divisible by four.
Then you process four bits at a time and convert them to a single hexadecimal digit:
0000 -> 0
0001 -> 1
0010 -> 2
....
1110 -> E
1111 -> F
No, you don't convert to decimal and then to hexadecimal, you convert to a numeric value, and then to hexadecimal.
(Decimal is also a textual representation of a number, just like binary and hexadecimal. Although decimal representation is used by default, a number doesn't have a textual representation in itself.)
As a hexadecimal digit corresponds to four binary digits you don't have to convert the entire string to a number, you can do it four binary digits at a time.
First fill up the binary number so that it has full groups of four digits:
000101101001.1011101010100110
Then you can convert each group to a number, and then to hexadecimal:
0001 0110 1001.1011 1010 1010 0110
169.BAA6
Alternatively, you can split the number into the two parts before and after the period and convert those from binary. The part before the period can be converted stright off, but the part after has to be padded to be correct.
Example in C#:
string binary = "101101001.101110101010011";
string[] parts = binary.Split('.');
while (parts[1].Length % 4 != 0) {
parts[1] += '0';
}
string result =
Convert.ToInt32(parts[0], 2).ToString("X") +
"." +
Convert.ToInt32(parts[1], 2).ToString("X");
You could simply have a small hash table, or other mapping converting each quadruplet of binary digits (as a string, assuming that's your input) into the corresponding hex digit (0 to 9, A to F) for the output string. You'll have to bunch the input bits up by 4, left-padding before the '.' and right-padding after it, with 0 in both cases, as needed.
So...:
locate the '.'
left of the '.', bunch by 4, left-padding the last bunch, going leftwards: in your example, 1001 leftmost, then 0110, finally 0001 (left-padding), that's it;
ditto to the right -- in your example 1011, then 1010, then 1010, finally 0110 (right-padding)
each bunch of 4 binary digits, via a hash or other form of hashing, turns into the hex digit to put in that place in the output string.
Want some pseudo-code for it, e.g., Python?
The simplest approach, especially if you already can convert from binary digits to internal numeric representation and from internal numeric representation to hexadecimal digits, is to go binary->internal->hex. I say internal and not decimal, because even though it may print as decimal, it is actually being stored internally in binary format. That said, it is possible to go straight from one to the other. This does not apply to your specific example, but in many cases when converting from binary to hex, you can go four digits at a time, and simply lookup the corresponding hex values in a table. There are all sorts of ways to convert.
BIN to HEX
Binary and hex are natively compatible. Just group 4 binary digits(bits) and substitute the corresponding HEX-digit.
More reference here:
http://en.wikipedia.org/wiki/Hexadecimal#Binary_conversion