Encodings

New Java Syntax
Level 9 , Lesson 2
Available

"There is nothing better than effective coding, Amigo! Trust an old robot."

"Are you talking about ciphers used by spies?"

"Of course not. I'm talking about presenting information in a digestible form. About numeral systems. You are aware that in everyday life most people use the decimal system. It uses 10 symbols to represent every number: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. There are 10 numerals, so the system is called decimal."

"That was convenient for humans with their ten fingers. But programmers are big-time inventors. They immediately came up with encodings that use a different number of digits. For example, 2, 8, 16, or 64 digits. They did this to make it convenient for computers, which rely on 'there is a signal / there is no signal'."

"Ah, I see what they have in common... All these systems are based on powers of two.

Octal encoding

"Good observation. Let's start with an encoding that involves 8 digits. Humans may find this the easiest: just drop the numbers 8 and 9 and — boom — you have the octal encoding (numeral system). You were recently told about literals, right?"

"Yes, I was."

"Well, surprise! You can set numeric literals encoded using the octal system. If, of course, you really need to. It's easier than it sounds. Just put 0 in front of the whole number.

"So if a numeric literal starts with zero, does that mean it's octal?"

"Yes, Java will treat it as octal.

Examples:

Code Notes
int x = 015; 
x is 13: 1*8+5
int x = 025; 
x is 21: 2*8+5
int x = 0123; 
x is 83: 1*64+2*8+3 == 1*82+2*81+3*80
int x = 078;
This will not compile: 8 is not one of the symbols used in the octal encoding.

"It's unlikely that you need to write octal numbers in your code, but you should know what they are. "After all, you will have to read code written by others. And as mentioned above, programmers are big inventors.

Well, remember that you can't just go and write 0 in front of every number."

"But if I intend for it to be octal, then I can?"

"Yes.

9
Task
New Java Syntax, level 9, lesson 2
Locked
Octal converter
The public static toOctal(int) method must convert the integer received as an input parameter from the decimal numeral system into octal. And conversely, the public static toDecimal(int) method converts from the octal system into decimal. The methods only work with positive numbers. If the input par

Binary encoding

"Even if you don't understand it yet, binary encoding is your native language. Let me remind you about it. If octal has only the digits 0-7, then binary has only 0 and 1."

"Why is this encoding necessary?"

"As I mentioned above, this has everything to do with the internal structure of a computer. Everything in a computer runs on electricity, and as it happens, the most efficient way to store and transmit something using electricity is to use two states: either there is no electricity in the wire (zero) and there is electricity (one)."

"That's why it is so popular... Hmm, it seems that I am indeed starting to remember this language!"

"All robots understand it perfectly. Although it is not used very often in Java. Java is considered a high-level language, completely abstracted from the hardware it runs on. Indeed, do you really care what format is used to store and process data inside a computer?

"But over the past decades, programmers have come to love the binary encoding (and other encodings based on it). As a result, Java has operators that take binary numbers as inputs. And the accuracy of floating-point numbers depends on their binary representation.

"In short, it is better for you to know about this encoding than to not know."

"Right. And as was the case with octal encoding, Java has a way to encode literals using the binary system."

"So they will only be made up of 0s and 1s?"

"Exactly. In order for the Java compiler to understand that the code contains a numeric literal encoded in binary rather than simply a decimal number consisting of zeros and ones, it is customary for all binary literals to begin with the prefix 0b (the 'b' comes from the word binary).

Examples:

Code Notes
int x = 0b100; 
х is 4: 1*4+0*2+0
int x = 0b1111; 
х is 15: 1*8+1*4+1*2+1
int x = 0b1111000111; 
х is 967: 1*29+1*28+1*27+1*26+0*25+0*24+0*23+1*22+1*2+1;
int x = 0b12000;
This will not compile: 2 is not one of the symbols used in the binary encoding.
9
Task
New Java Syntax, level 9, lesson 2
Locked
Binary converter
The public static toBinary(int) method must convert the integer received as an input parameter from the decimal numeral system to binary and return its string representation. And conversely, the public static toDecimal(String) method converts from the string representation of a binary number to a de

Hexadecimal encoding

"What's two to the fourth power?"

"Sixteen. You figured out the right question to ask a robot that has come as far as me!"

"It seems to you that you have come far. Anyway, sixteen. In addition to octal and binary encodings, literals can also be written in hexadecimal. This is a very popular encoding.

"That is because although binary notation is as close as possible to how numbers are actually stored, it is too difficult for humans to effectively work with such numbers: in binary, the number one million 20 digits, not 7.

"That's why programmers came up with the hexadecimal system. After all, as you correctly noted, 16 is 2 raised to the 4th power, so exactly 4 bits correspond to one hexadecimal digit.

"So every 4 bits can now be written in a single hexadecimal digit."

"Right. The hexadecimal encoding also has its own unique prefix: 0x. Examples:

Decimal number Binary notation Hexadecimal notation
17 0b00010001 0x11
41 0b00101001 0x29
85 0b01010101 0x55
256 0b100000000 0x100

"Ok, so it's clear enough how we got the octal system: we just threw out the numbers 8 and 9. But where do we get the 6 missing digits for the hexadecimal system? I would like to see them!"

"It's all straightforward. The first 6 letters of the English alphabet were taken as the 6 missing digits: A (10), B (11), C (12), D (13), E (14), F (15).

Examples:

Hexadecimal notation Binary notation Decimal number
0x1 0b00000001 1
0x9 0b00001001 9
0xA 0b00001010 10
0xb 0b00001011 11
0xC 0b00001100 12
0xD 0b00001101 13
0xE 0b00001110 14
0xF 0b00001111 15
0x1F 0b00011111 31
0xAF 0b10101111 175
0xFF 0b11111111 255
0xFFF 0b111111111111 4095
9
Task
New Java Syntax, level 9, lesson 2
Locked
Hexadecimal converter
The public static toHex(String) method must convert the integer received as an input parameter from the decimal numeral system to hexadecimal and return its string representation. And conversely, the public static toDecimal(String) method converts from the string representation of a hexadecimal numb

"How do you convert a hexadecimal number to decimal?"

"It's very simple. Let's say you have the number 0xAFCF. How much is that in decimal? First, we have a positional number system, which means the contribution of each digit to the overall number increases by a factor of 16 as we move from right to left:

A*163 + F*162 + C*161 + F

The symbol A corresponds to the number 10, the letter C says we have the number 12, and the letter F represents fifteen. We get:

10*163 + 15*162 + 12*161 + 15

Raising 16 to the various powers that correspond to the digits, we get:

10*4096 + 15*256 + 12*16 + 15

We sum everything up and get:

45007

"Now you know how 45007 is stored in memory."

"Yes, I do. It is 0xAFCF"

"Now let's convert it to binary. In binary it would be:

0b1010111111001111

"Every set of four bits corresponds to exactly one hexadecimal character. That's super convenient. Without any multiplication or exponentiation."

9
Task
New Java Syntax, level 9, lesson 2
Locked
Binary to hexadecimal converter
The public static toHex(String) method must convert the string representation of a binary number, received as an input parameter, from the binary numeral system to hexadecimal and return its string representation. And conversely, the public static toBinary(String) method converts from the string rep
Comments (15)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
larsintech Level 15, Switzerland Expert
27 November 2024
You might want to look into the toCharArray() String method if you're stuck implementing binary to decimal
nealgoogs Level 21, Eliot, United States
22 August 2024
I think I would just skip this for now, just until more methods are learned.
Daniel Ketcheson Level 28, Canada
27 March 2023
I took one exercise/day on conversions and took on further lessons meanwhile. Preserved my eagerness to learn this way.
Rather Be Coding Level 14, Detroit, United States
12 November 2023
This is definitely making me feel braindead.
Simphiwe Level 33, Johannesburg, South Africa
19 February 2023
Yep! I would say it was hard and tricky. The debug mode and some online convertor really helped. ✌️
Winston Level 9, Netherlands
24 November 2022
The first few are not too hard. The last conversion was the hardest, especially the condition for the hexadecimal check. Tried a couple of ways. But the taks with the most time to make. Need time to get comfortable with the last conversion.
Prakash K Level 18, Bangalore, India
31 October 2022
It took me one and hour for these tasks by far longest I spent.
Galax Level 10, Houston, United States
6 August 2022
Was it a mistake when you typed not not 7 in one of the paragraphs of the hexadecimal part?
Daniel Bressner Level 13, United Kingdom, United Kingdom
19 January 2022
This was the first set of tasks where I really struggled, definitely the hardest so far but not letting that discourage me. I needed a lot of outside help, but one thing that was very effective was using the debugger! A few "aha" moments stepping through various loops.
Jonaskinny Level 25, Redondo Beach, United States
8 February 2022
That's the spirit !! This one got me as well, on the toHex trying to understand vs. just decoding the psuedo-code help. Plenty of tutorials on binary_2_hex_2_decimal_etc and some are really clear. For me the aha moment was getting the base 16/10 conversion. After that it is just binary.
Jcode Level 27, United Kingdom
17 January 2022
Interesting set of tasks - hardest so far, but really starting to come together. Multiple ways to solve the same task. I went back and tried a few different ways to challenge myself (took ages tho). Remember it's not a race, knowledge gained now will be invaluable, when the tasks get harder.
whoseunassailable Level 28, India, India
17 January 2022
I think the same code is something we will be able to solve in the latter section properly. One thing that bothers me is i would be more intrigued if tasks like this i mean only in this particular chapter are any essential in real life. I don't think there are any problems out there that asks for conversion of numbers so.
Jcode Level 27, United Kingdom
17 January 2022
Programming is really just about problem-solving. The conversion tasks involve using loops, if statements, the Strings class and its methods etc etc ... conversion between bases may not appear a lot in real life but the skills needed to carry out those conversions is required everywhere in coding,
Jonaskinny Level 25, Redondo Beach, United States
8 February 2022
Me too, a few hours but I want to cement these concepts so I understand (so I remember).
Jonaskinny Level 25, Redondo Beach, United States
8 February 2022
I have actually scene data extracts imported as hex (or unicode) needing conversion prior to storage, but that was a major one-off corner case and we just used higher level classes for the conversion method not permitted in this exercise, so I learned more here than doing that actual work ;)