Closed Thread
Results 1 to 4 of 4

Thread: Binary, Octal & Hex Conversions

  1. #1
    lor's Avatar
    lor
    lor is offline Programming Goddess
    Join Date
    Jun 2009
    Posts
    895
    Blog Entries
    4
    Rep Power
    16

    Binary, Octal & Hex Conversions

    In this tutorial I will be showing you how to translate different types of numbers into... well, other numbers and such. I will also be teaching you some of the discrete mathematics involved with computing.

    Basics:

    Natural Numbers:
    1, 2, 3, 4, so on, these are basically referred to as "counting numbers".

    Integers:
    -3, -2, -1, 0, 1, 2, so on, negative and positive numbers.

    Rational Numbers:
    Rational numbers are numbers which can be expressed as x/y where x and y are integers and y is not equal to 0. e.g. 1/4, 1/6, 12/1, etc.

    Irrational Numbers:
    Numbers which can't be expressed as x/y where x and y are integers and y is not equal to 0. A common example is pi. (The latest I heard about pi is that some University students got the biggest, fastest computer in the world, calculated pi and they recorded about 1 trillion numbers or so, no one has actually worked out if pi has a repetitive system, for all we know pi could start repeating at 1 trillion and 1, we'll have to wait and see!)

    Decimal Number System:
    Each digit has a place value that depends on its position relative to the decimal point. Decimal number systems have 10 as its base. Example:

    333.310

    The first 3 (to the left) has a value of 300, the next, value of 30, the next, a value of 3, and the next, a value of 3/10.

    Expanded Form:
    A random number, 2461.98510 =
    2x10³ + 4x10² + 6x10¹ + 1x100 + 9x10-¹ + 8x10-² + 5x10-³
    20,000 + 4,000 + 600 + 10 + 9 + 0.8 + 0.05
    = 24,619.85

    *The reason why we multiply it by 10 is because a natural number has a base of 10.
    *Any number raised to the power 0 is equal to 1, e.g. 10° = 1, 1° = 1, 3° = 1, y° = 1, so on and so forth.

    Counting in Binary:
    Binary has 2 different numbers therefore it has a base of 2. Be careful when writing natural numbers/binary/octal/hex as "101" could easily be mistaken for "one hundred and one" instead of the binary representation, thus why we add a small "2" down the bottom right corner.
    Anyway, let's get to the counting.

    Decimal: Binary:
    0 0
    1 1
    2 10
    3 11
    4 100
    5 101
    6 110
    7 111
    8 1000
    9 1001
    10 1010
    11 1011
    12 1100
    13 1101
    14 1110
    15 1111
    16 10000

    If you haven’t noticed, there is an algorithm hidden within binary. If 0 is appended to its binary representation the integer is multiplied by 2. If 1 is appended to its binary representation the integer is multiplied by 2 and 1 added.
    Each time a zero is added onto a decimal, the size of the binary number increases 10 times. E.g. 1, 10, 100, 1000, etc. Each time a zero is added onto a binary number, the size of the decimal number doubles.

    Converting Binary to Decimal:
    To convert binary to a decimal number we use the expanding technique using base 2.

    1101.012
    = 1x2³ + 1x2² + 0x2¹ + 1x2° + 0x2-¹ + 1x2-²
    = 8 + 4 + 0 + 1 + 0 + 0.25
    = 13.2510

    Converting Decimal to Binary:
    We basically divide by 2 and record the remainder. Once we have reached “0” we stop. We then read upwards, giving us our answer.

    25
    12 1
    6 0
    3 0
    1 1
    0 1

    2510 = 11001²

    If we have a number after the decimal point, multiply by 2 instead of dividing by 2.

    0.32
    0 64
    1 28
    0 56
    1 12
    0 24
    0 48
    0 96

    0.3210 = 0.0101000²

    *In order to maintain the level of accuracy, binary numbers need three places for each place in the decimal number, plus 1.

    Binary, Octal And Hexadecimal:

    Dec Bin Oct Hex
    0 0 0 0
    1 1 1 1
    2 10 2 2
    3 11 3 3
    4 100 4 4
    5 101 5 5
    6 110 6 6
    7 111 7 7
    8 1000 10 8
    9 1001 11 9
    10 1010 12 A
    11 1011 13 B
    12 1100 14 C
    13 1101 15 D
    14 1110 16 E
    15 1111 17 F
    16 10000 20 10

    ...and so on. I’ll give you another example in Hex as it’s a bit more confusing.

    0 10 20... 90 A0 B0 C0... F0 100 110
    1 11 21 91 A1 F1 101 111
    2 12 22 92 A2 F2 102 112
    3 13 23 ... ... F3 103 ...
    4 14 24 F4 104
    5 15 25 F5 105
    6 16 26 F6 106
    7 17 27 F7 107
    8 18 28 F8 108
    9 19 29 F9 109
    A 1A 2A FA 10A
    B 1B 2B FB 10B
    C 1C 2C FC 10C
    D 1D 2D FD 10D
    E 1E 2E FE 10E
    F 1F 2F 9F AF FF 10F 11F

    Alright, conversions.

    Octal to Binary:
    Octal: Binary:
    0 0
    1 1
    2 10
    3 11
    4 100
    5 101
    6 110
    7 111
    10 1000

    When converting octal to binary we group the binary in groups of 3. For example:

    6 0 1 3
    110 000 001 011

    Binary to Octal:
    Octal Binary:
    0 0
    1 1
    2 10
    3 11
    4 100
    5 101
    6 110
    7 111
    10 1000

    We divide the binary into groups of 3, basically the same as converting octal to binary but the other way round.

    1 101 001 011 . 011 100
    1 5 1 3 . 3 4

    Hexadecimal to Binary:
    Hex: Binary:
    0 0
    1 1
    2 10
    3 11
    4 100
    5 101
    6 110
    7 111
    8 1000
    9 1001
    A 1010
    B 1011
    C 1100
    D 1101
    E 1110
    F 1111

    Keep the binary in groups of four when converting.

    7 0 A 3
    111 0000 1010 0011

    Binary to Hex:
    Hex: Binary:
    0 0
    1 1
    2 10
    3 11
    4 100
    5 101
    6 110
    7 111
    8 1000
    9 1001
    A 1010
    B 1011
    C 1100
    D 1101
    E 1110
    F 1111

    Same rule applies when converting binary to hex as goes for hex to binary, groups of four.

    1 0110 0011 1101 . 0001 1001 11
    1 6 3 D . 1 9 C

    Octal to Decimal:
    Note that octal has 8 as its base.

    374.28 = 3x8² + 7x8¹ + 4x8° + 2x8-¹
    = 192 + 56 + 4 + 2/8
    = 52.2510

    Hexadecimal to Decimal:
    Note that hex has 16 as its base.

    Hex: Decimal:
    0 0
    1 1
    2 2
    3 3
    4 4
    5 5
    6 6
    7 7
    8 8
    9 9
    A 10
    B 11
    C 12
    D 13
    E 14
    F 15

    E9C.816 = 14x16² + 9x16¹ + 12x16° + 8x16-¹
    = 7358 + 144 + 12 + 8/16
    = 3740.510

    Conversion from Decimal to Octal and Hexadecimal:
    Divide by 8/16 and record the remainder from the bottom upwards.

    8 1459
    182 3
    22 6
    2 6
    0 2

    = 26638

    16 1459
    91 3 3
    5 11 B
    0 5 5 5

    = 5B316

    Thanks for reading. If there are any questions be free to ask but know that I'm not a binary expert. If there are any mistakes please let me know.
    Author - lor.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: Binary, Octal & Hex Conversions

    01010110011001010111001001111001001000000110001101 10111101101101011100000110110001100101011101000110 01010010000100100000010001110110111101101111011001 00001000000111010001110101011101000110111101110010 01101001011000010110110000101100001000000010101101 1100100110010101110000

  4. #3
    lor's Avatar
    lor
    lor is offline Programming Goddess
    Join Date
    Jun 2009
    Posts
    895
    Blog Entries
    4
    Rep Power
    16

    Re: Binary, Octal & Hex Conversions

    Thank you.

  5. #4
    exicute's Avatar
    exicute is offline Programming Expert
    Join Date
    Jan 2010
    Location
    Ohio
    Posts
    398
    Rep Power
    10

    Re: Binary, Octal & Hex Conversions

    Good Tutorial, I never took the time to learn binary, but this gives the basic idea in a structured manner. +rep

    My Tutorials|Build A Computer|Cat 5E|

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Confused on explicit and implicit conversions
    By demo53 in forum C# Programming
    Replies: 1
    Last Post: 04-03-2011, 04:53 PM
  2. Hexa convert to binary. sum all binary numbers.
    By iera_yahaya in forum C and C++
    Replies: 5
    Last Post: 02-02-2010, 05:58 AM
  3. Data type conversions?
    By Macoder in forum C and C++
    Replies: 6
    Last Post: 10-10-2009, 04:09 PM
  4. octal
    By jamesw in forum General Programming
    Replies: 3
    Last Post: 11-02-2008, 11:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts