Adblock breaks this site

Tutorial: Manipulating Binary

Discussion in 'Programming General' started by Swan, Feb 10, 2008.

  1. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Tutorial: Manipulating Binary

    Well, many of you may want to know why the hell you would want to manipulate binary. I'll give you a few examples of why you would want to, before I start. But before I go in to the examples. I am going to say this flat: There will be little percentage of the needed code provided during the course of the tutorial. The program source can be downloaded from the link at the bottom of the tutorial.

    Consider games. Their maps are stored in seemingly "encrypted" files that you cannot open and understand whatsoever, yet you can get programs to view these maps and models with ease. This is most commonly because the software uses binary. The mapping system will modify the bytes in the file, rather than the text. You can format these bytes to how you want them to be, you can include your own method of storing your own data.

    Also, consider commercial (and sometimes uncommercial) software. They will commonly use custom files (i.e. The Elder Scrolls series uses .esp for their plugins, .ess for their save files etc). This also uses byte manipulation, hence binary manipulation is necessary.

    This tutorial will teach you how to convert from the decimal number system to the binary number system, and vice versa using Java. The same concept can however be applied to languages such as C++ and C#.

    Binary is all ones and zeros right? Well, as you should know, 1 is true and 0 is false. So consider the following table:

    Code:
                +  +  +  + + + +  = Decimal
             128|64|32|16|8|4|2|1 = 255
               0| 0| 1| 1|1|1|0|0 = 60
               0| 1| 1| 1|1|0|0|0 = 120
               1| 1| 1| 1|0|0|0|0 = 240
    
    As you can see, the number 60 (of which the binary equiv. is 111100) is made up of 32 + 16 + 8 + 4. Likewise, 120 is made up of 64 + 32 + 16 + 8, and 240 is made up of 128 + 64 + 32 + 16. This is the basis of binary manipulation. Knowing how this works means you know how to successfully convert Binary in to decimal, and so on.

    But we want to write ourselves a program to do this for us, so we have no need to do the sometimes trivial math.

    To do this, the concept may not be thought of at first, but is extremely simple. The method is extremely easy to understand in my opinion, just look at it logically.

    This method only has one argument, which is the decimal value you wish to convert. There will be two variables, a character array, and a string. Name them what you will.

    Now, on to concept. You want to check the remainder of a devision by two using the modulus operator. The operand will obviously be the decimal argument. The result yielded will either be 1 or 0, being the remainder of the division. Now add this to your string, divide the operand (Decimal value) by two and do it again until it is no longer larger than 0. Incase you're wondering how to do this, I'll spoil you a little bit:
    Code:
    while(Decimal > 0)
    If you decided to demo this piece of code, you will be delighted to see a bunch of 1's and 0's are output. There is only one problem, they are the wrong way around. say you wanted the binary value for 12, this _SHOULD_ be 1100, but instead you will be given 0011! There is an easy way to solve this little issue though, simply reverse the string. This is where we use our character array.

    In case you haven't already guessed it, you are going to convert the resultant string from the previous operations in to the array. To do this, simply use the String.toCharArray() method. Now, for the "hard" part (note the sarcasm). I prefer to use a while loop and two int variables, but reversal may be achieved using any loop variation. Here is a spoiler in case you're stuck:

    _WARNING: SPOILER_
    Code:
            int i = 0;
            int l = bArray.length - 1;
            while(i < l) {
                char t;
                t = bArray[l];
                bArray[l] = bArray[i];
                bArray[i] = t;
                
                i++;
                l--;
            }
    (where bArray is the character array you are using)

    Now all you need to do is redefine your string as a new string using that character array, and return the value. Congratulations! You have successfully converted Decimal to Binary!

    Now, here is your chance to shine: I'm going to give you a little project. Create a program that converts binary to decimal. Sure, you can write your own formats using Decimal to binary, but you can't read them unless you can go the other way, can you? The Microsoft .Net platform provides a simple function to do this for you. This is why I used Java, because I prefer for, while learning, you should know how everything ticks before using premade methods.

    Now I will explain some interesting things about binary operations.

    If you want to double your binary number, all you have to do is move it one place to the left. This can be shown in the table above, but for means of convenience, I'll display it here:
    Code:
                +  +  +  + + + +  = Decimal
             128|64|32|16|8|4|2|1 = 255
               0| 0| 1| 1|1|1|0|0 = 60
               0| 1| 1| 1|1|0|0|0 = 120
               1| 1| 1| 1|0|0|0|0 = 240
    
    Likewise, to half a binary number, you can simply move it one place to the right, as shown on the table. These are where the bitwise operators come in useful.

    If you want to move your desired binary number to the left or right, you can use the "shifting" operators to do this. example:
    Code:
    System.out.println(10 << 1);
    this moves the binary number 1010 one space to the left, making it 10100. this doubles the number, as this table will show:

    Code:
    		16 8 4 2 1
    		   1 0 1 0 = 8 + 2 = 10
    		 1 0 1 0 0 = 16 + 4 = 20
    
    Likewise, you may use the right shifting operator to half the number:
    Code:
    System.out.println(10 >> 1);
    this turns the binary number 1010 in to 101 (moves it one "decimal" place to the right), in effect making it the decimal 5, as shown:
    Code:
    		8 4 2 1
    		1 0 1 0 = 8 + 2 = 10
    		  1 0 1 = 4 + 1 = 5
    
    There are a few other bitwise operators (&, |, ^ and so on) that you will want to study on your own.

    The entire source can be downloaded from http://67.207.132.247/dave/bitwisetutorial/Main.java

    credits go to http://www.geekpedia.com/ for the original C# source, as I knew how binary numbers worked, I just didn't know the method used to convert them. I take absolutely no credit for this method.
     
  2. Olan14

    Olan14 Forum Addict

    Joined:
    Jan 26, 2007
    Posts:
    581
    Referrals:
    0
    Sythe Gold:
    0
    Tutorial: Manipulating Binary

    It was fair, but I suggest you write your own material.
     
  3. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Tutorial: Manipulating Binary

    This is my own material, bar from the code - which I converted from C# and indeed, gave full credit to the website I got it from.
     
< Macroers prog need help! | Is it possible to make an IRC server out of visual basic? >


 
 
Adblock breaks this site