C++ does not support octal encoding within source code (only decimal and hexadecimal are supported), so the octal must be represented with a string. The binary output must also be a string. Every octal digit represents three bits of binary, thus the binary output string will always be three times the length of the octal input string.
The loop will begin with the octal character at index 0 and work through each character from left to right. On each iteration, the binary output string will be appended with a 3-character string, as follows:
octal = binary
"0" = "000"
"1" = "001"
"2" = "010"
"3" = "011"
"4" = "100"
"5" = "101"
"6" = "110"
"7" = "111"
Thus octal input "437" will begin with the "4" producing an output of "100". On the next iteration, "3" will append "011" to produce "100011". On the final iteration, "7" will append "111" to produce "100011111".
Chat with our AI personalities
Binary is a base 2 number system, while octal is base 8. This happens to make conversion between binary and octal fairly trivial, although more complex than conversion to hexadecimal. To convert to octal from binary, take each three bits, starting from the least significant bit, and convert them to their octal equivalent. Examples: 25510 = 111111112 = 11 111 111 = 3778 17410 = 101011102 = 10 101 110 = 2568 You can repeat this process for as many bits as you need. A 24-bit number should translate into 8 octal numbers, for reference.
Convert each hex digit to four binary digits. If you get less than three (for example, 7 --> 111), fill it out with zeroes to the left (in this case, 0111).
Each hexadecimal digit represent four binary bits. Using the table... 0 = 0000 1 = 0001 2 = 0010 3 = 0011 4 = 0100 5 = 0101 6 = 0110 7 = 0111 8 = 1000 9 = 1001 A = 1010 B = 1011 C = 1100 D = 1101 E = 1110 F = 1110 ... replace each hexadecimal digit with its correspnding binary digits. As an example, 37AB16 is 00110111101010112.
Octal (base 8) and hexadecimal (base 16) are simply shorthand notations for binary sequences. We can actually use any base that is a power of 2 (including base 4, base 32, base 64, and so on) as a shorthand for binary, but we use octal and hexadecimal because they are fairly easy to work with. If we look at base 4 first, we can better understand the relationship between binary and all other bases that are a power of 2. Base 4 only uses 4 symbols, 0, 1, 2 and 3. In binary these would be represented by 00, 01, 10 and 11 respectively. Thus a single base 4 digit can be used in place of every two binary digits, essentially halving the length of any binary sequence. Base 8 (octal) uses 8 symbols, 0, 1, 2, 3, 4, 5, 6 and 7. In binary that is 000, 001, 010, 011, 100, 101, 110 and 111. Thus a single octal digit can be used in place of every 3 binary digits, reducing the length of a binary sequence by two-thirds. Similarly, base 16 (hexadecimal) uses 16 digits, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e and f, and each digit can represent 4 binary digits. By the same token a single base 32 digit represents 5 binary digits, while a single base 64 digit represents 6 binary digits, and so on. Since octal notation splits a binary sequence into groups of three bits, it is ideal when the number of bits is a multiple of 3, such as in a 24-bit system. That is, a 24-bit sequence (with 24 digits), can be reduced to an octal sequence of just 8 digits. And since hexadecimal notation splits a binary sequence into groups of 4 bits, it is ideal when the number of bits is a multiple of 4, such as in a 32-bit system. Again, a 32-bit sequence (with 32 digits) can be reduced to a hexadecimal sequence of just 8 digits. We predominantly use hexadecimal as a shorthand notation for binary numbers because a byte is typically 8-bits long, thus we only need two hexadecimal digits to represent all possible values in a byte. A single hexadecimal digit therefore represents half a byte, which we affectionately call a nybble. You may well ask why we don't just use decimal notation as a shorthand for binary sequences and save us humans all the hassle of translating altogether. After all, if computers can be programmed to translate hexadecimal and octal notation into their native binary, then surely they can also be programmed to do the same for decimal. In actual fact they can and do. But when presented with a long sequence of binary such as: 01111101010011010110010011110100 it's much easier to split the sequence into groups of 4 digits and assign a single hex digit to each than it is to work out what the decimal equivalent would be. In this case the binary number splits as follows: 0111 1101 0100 1101 0110 0100 1111 0100 Then we can assign each group its hexadecimal equivalent: 7 D 4 D 6 4 F 4 Thus the hexadecimal value is 7D4D64F4. In decimal we would have to write the value 2,102,224,116 instead. While this is only 2 digits longer than the hexadecimal (if we remove the comma separators), it takes a lot longer to work it out (I cheated and used a calculator). Hexadecimal is not only a much simpler conversion (you can easily do it in your head), it also works in reverse to reveal the original binary value. Remember that we don't really care what the decimal value is -- all we're really interested in is the binary value and how we can notate it as quickly as possible using as few symbols as possible. And counting up to 16 is really not much more difficult than counting up to 10. Although it can take a bit of practice getting used to hexadecimal notation, it quickly becomes second nature, and you'll soon be counting in all sorts of bases besides base 10. You already do, in fact. The 24-hour clock is intrinsically sexagesimal, so you've been marking time in base 60 all this time (pun intended) without even realising it. The only reason it is base 60 is because it is the lowest number that is evenly divisible by 2, 3, 4, 5 and 6. The ancient Sumerians knew this 5,000 years ago. It's also the reason why circles have exactly 360 degrees.
binary code(computer science) A code in which each allowable position has one of two possible states, commonly 0 and 1; the binary number system is one of many binary codes.Source: http://www.answers.com/binary+code?cat=technology