Results 1 to 10 of 10

Thread: noob coding question     submit to reddit submit to twitter

  1. #1
    Hydra
    Join Date
    Apr 2006
    Posts
    105
    BG Level
    3

    noob coding question

    ok so i got this assignment where i have to convert bases to numbers and im really lost and could use some help, not gonna ask for the code cause thats stupid. just would like someone to explain hexadecimal stuff to me please. here is what it says.


    "We are used to base 10 numbers where each column is a power of 10 and has a range of 0 to 9. In computers we also commonly use other bases like base 2, 8 and 16. In general each column of a number is a power of the base with a range of 0 to the (base – 1). So in base 8 the range of digits in a column is 0 to 7 and a 10 represents 8. Base 16 the range of digits in a column is 0 to F, 0, 1, 2, … 9, A, B, C, D, E, F and a 10 base 16 is 16 base 10."

    and it says to "Write a program to convert a positive whole base 10 number from 0 to 255 to a base from 2 to 16. The program will ask for a number then the base to convert it to. Then it will output the original base 10 number in the new base. For example: the number 2 is entered to be converted to base 2, and the output says “2 base 10 is 10 base 2”.
    Then the program asks if the user wants to covert another number
    "
    sorry for the noob question

  2. #2
    Pay No Attention to the Man Behind the Curtain
    Join Date
    Jan 1970
    Posts
    3,486
    BG Level
    7
    FFXIV Character
    Ragns Meuhie
    FFXIV Server
    Gilgamesh
    FFXI Server
    Bahamut
    Blog Entries
    171

    Knowing your conversion table pretty much give you then answer:
    Code:
    Hex   Bin      Dec
    0     0000     0
    1     0001     1
    2     0010     2
    3     0011     3
    4     0100     4
    5     0101     5
    6     0110     6
    7     0111     7
    8     1000     8
    9     1001     9
    A     1010     10
    B     1011     11
    C     1100     12
    D     1101     13
    E     1110 	 14
    F     1111 	 15
    Then depending on the programming language you use you convert the data using bit shifting, modulo or whatever your learned in your class.

  3. #3
    Sea Torques
    Join Date
    Nov 2005
    Posts
    641
    BG Level
    5
    FFXI Server
    Asura

    there are 10 types of people.
    Those who understand binary and those that don't.

    edit: btw, this seems more like a math question than a programming one, once you know the formula you could convert it into any base you would want really. (I had a similar assignment in college as well)

  4. #4
    Blackice
    Guest

    Quote Originally Posted by SassiAsura
    there are 10 types of people.
    Those who understand binary and those that don't.
    That gave me a nice chuckle, lol.

  5. #5
    Sea Torques
    Join Date
    Jan 2005
    Posts
    535
    BG Level
    5

    Hopefully when you were taught how to convert numbers to different bases, you worked out an example that looks something like this.

    First, lets work in base 10. Take the number 3299. This is easily represented like this:

    Code:
    3 x 10e3 + 2 x 10e2 + 9 x 10e1 + 9 * 10e0
       3000  +    200   +    90    +  9
                        3299

    Now lets take the same number and represent it in base 6 (hey, nobody EVER uses base 6 but the same process applies to any number base)

    First, lets see what the different "multiples" of 6 are in base 10.
    Code:
    6e0   6e1  6e2  6e3  6e4
      1    6    36  216  1296
    The number we want to convert is 3299.

    Code:
    3299 - 2 * 6e4 = 3299 - 2 * 1296 = 3299 - 2592 = 707 remainder
    707 - 3 * 6e3 = 707 - 3 * 216 = 707 - 648 = 59 remainder
    59 - 1 * 6e3 = 59 - 1 * 36 = 59 - 36 = 23 remainder
    23 - 3 * 6e1 = 23 - 3 * 6 = 23 - 18 = 5 remainder
    5
    ----------
    result = 23135 (base 6)
    Maybe this is just more confusing, but this is how I originally learned the long way to convert different number bases.

    Edit: Because your input is limited to the range 0 - 255, your math will be MUCH simpler.

  6. #6
    Sea Torques
    Join Date
    Nov 2005
    Posts
    641
    BG Level
    5
    FFXI Server
    Asura

    I had lots of fun in that math class, we also used something which translates... to arrays in English I believe. In French it was matrices. Of course, since I haven't applied any of that knowledge in a few years, I'd need a little bit of time to get back into it.

  7. #7
    Hydra
    Join Date
    Apr 2006
    Posts
    105
    BG Level
    3

    yeah i get all of that but i have to make a program for bases 2-16 for numbers 0-255 and thats kind of where im getting lost, ive asked my roommates and they still dont understand it. so far i have (pitiful) and BTW this is C#. and my first programming class
    Code:
    using System; // the System name space
    
    public class MainClass
    {
    
        // Main method where the program starts
        public static void Main(string[] Args) 
        {
                    int n; //number
    		int b; //base
    
    		Console.WriteLine("Enter a number from 0 to 255: ");
    		n = Convert.ToInt32(Console.ReadLine());
    
    		Console.Write("Enter a base from 2-16: ");
    		b = Convert.ToInt32(Console.ReadLine());
            
        } // end Main method end of program
        
    }  // end class MainClass (change the name)
    some ideas i have are:

    Get a number N between 0 and 255 inclusive.
    Get a base B between 2 and 16 inclusive.

    While the number is > 0
    Get a digit using N % B
    Send the digit to a switch statement to convert it to a string, C
    Put the character C on the FRONT of the string S ( S = C + S; )
    Divide the number by the base
    End conversion

    Write out the original number N, the base and the string S

    Ask the user if they want to convert another number, if yes then repeat

     Get A number
    Do
    Prompt for a number in the correct range
    Read the number
    While not legal

     The Switch on the digit
    10 -> “A”, 11 -> “B”, 12 -> “C”, 13 -> “D” ,14 -> “E”, 15 -> “F”
    default: C = Convert.ToString(Digit);

  8. #8
    Cerberus
    Join Date
    Jun 2006
    Posts
    432
    BG Level
    4

    ok, well i don't know how much this will help you, but i had to do basically the same thing for my java class near the start of the year.

    C# and Java have a sort of simliar syntax, here is the code (ignore the fact that there is no main method, we put it into a graphical interface which called all of the things needed.

    Code:
    import java.lang.StringBuffer;
    public class Numconverter
    {
        public String bin = "";
        public String hex = "";
        public String oct = "";
        public StringBuffer kb;
        public double dnum, bnum, hnum, onum;
        public void setDecimal(double number)
        {
            dnum = number;     
        }
        public void setOct(String number)
        {
            oct = number;
            kb = new StringBuffer(oct);
            int length = kb.length();
            for(length = length; length != 0; length--)
            {
                char num = kb.charAt(length-1);
                if(num != '0' && num != '1' && num != '2' && num != '3' && num != '4' && num != '5' && num != '6' && num != '7')
                {
                    oct = "ERROR";
                }
            }
        }
        public void setHex(String number)
        {
            hex = number;
            kb = new StringBuffer(hex);
            int length = kb.length();
            for(length = length; length != 0; length--)
            {
                char num = kb.charAt(length-1);
                if(num != '0' && num != '1' && num != '2' && num != '3' && num !='4' && num !='5' && num !='6' && num !='7' && num !='8' && num !='9' && num !='A' && num !='B' && num !='C' && num != 'D' && num !='E' && num !='F') 
                {
                    hex = "ERROR";
                }
            }
        }
        public void setBin(String number)
        {
            //long bnum2 = (long)number;
            //bin = Long.toString(bnum2);
            bin = number;
            kb = new StringBuffer(bin);
            int length = kb.length();
            for(length = length; length != 0; length--)
            {
                char num = kb.charAt(length-1);
                if(num != '1' && num != '0')
                {
                    bin = "ERROR";
                }
            }
        
            
        }
        
        public String getDecfromBin()
        {
            double z = 0;
            int c = 0;
            int m = 0;
            int x = 1;
            int length = kb.length();
            int[] thearray = new int[length];
            if(bin != "ERROR")
            {
                for(x = 0; x != length; x++)
                {
                    String norg = String.valueOf(kb.charAt(x));
                    int go = Integer.valueOf(norg);
                   
                    thearray[x] = go;           
                }
         
                for(z = length; z != 0; z--)
                {
                    int a = thearray[m];
                    double pow = Math.pow(2.0, z-1);
                    double b = a*pow;
                    c += b;
                    m++;
                }
            }
            else
                {
                    return(bin);
                }
                bin = Integer.toString(c);
            return(bin);
        }
        public String getBinaryfromDec()
        {
            bnum = dnum;
            int g = (int)dnum;
            int y = 0;
            int[] binnum = new int[g];
            for(int x = 1; bnum != 0; x++)
            {   
                binnum[x-1] = (int)(bnum % 2);
                if((int)bnum/2 != 0)
                {
                bnum = bnum/2;
                }
                else
                {
                    bnum=0;
                }
                y = y + 1;
            } 
            
            for(int counter = y; counter != 0; counter--)
            {
                bin += binnum[counter-1];
            }
            return(bin);
        }
        
        public String getHexfromDec()
        {
            hnum = dnum;
            int g = (int)dnum;
            int y = 0;
            int[] hexnum = new int[g];
            for(int x = 1; hnum != 0; x++)
            {
                hexnum[x-1] = (int)(hnum%16);
                if((int)hnum/16 != 0)
                {
                    hnum = hnum/16;
                }
                else
                {
                    hnum = 0;
                }
                y = y +1;
            }
            for(int counter = y; counter != 0; counter--)
            {
                if(hexnum[counter-1] == 10)
                {
                    hex += "A";
                }
                else if(hexnum[counter-1] == 11 )
                {
                    hex += "B";
                }
                else if(hexnum[counter-1] == 12 )
                {
                    hex += "C";
                }
                else if(hexnum[counter-1] == 13)
                {
                    hex += "D";
                }
                else if(hexnum[counter-1] == 14)
                {
                    hex += "E";
                }
                else if(hexnum[counter-1] == 15)
                {
                    hex += "F";
                }
                else
                {
                    hex += hexnum[counter-1];
                }
            }
            return(hex);
    }
      
        public String getDecfromHex()
        {
            double z = 0;
            int c = 0;
            int m = 0;
            int x = 1;
            int go = 0;
            int length = kb.length();
            int[] thearray = new int[length];
            if(hex != "ERROR")
            {
                for(x = 0; x != length; x++)
                {
                    char norg = kb.charAt(x);
                    if(norg == 'A')
                    {
                        go = 10;
                    }
                    else if(norg == 'B')
                    {
                        go = 11;
                    }
                    else if(norg == 'C')
                    {
                        go = 12;
                    }
                    else if(norg == 'D')
                    {
                        go = 13;
                    }
                    else if(norg == 'E')
                    {
                        go = 14;
                    }
                    else if(norg == 'F')
                    {
                        go = 15;
                    }
                    else
                    {
                    String norg2 = String.valueOf(kb.charAt(x));
                    go = Integer.valueOf(norg2);
                    }       
                    thearray[x] = go;           
                }
                
                for(z = length; z != 0; z--)
                {
                    int a = thearray[m];
                    double pow = Math.pow(16.0, z-1);
                    double b = a*pow;
                    c += b;
                    m++;
                }
            }
            else
                {
                    return(hex);
                }
                hex = Integer.toString(c);
            return(hex);
        }
        
        public String getOctfromDec()
        {
            onum = dnum;
            int g = (int)dnum;
            int y = 0;
            int[] octnum = new int[g];
            for(int x = 1; onum != 0; x++)
            {
                octnum[x-1] = (int)(onum%8);
                if((int)onum/8 != 0)
                {
                    onum = onum/8;
                }
                else
                {
                    onum = 0;
                }
                y = y +1;
            }
            for(int counter = y; counter != 0; counter--)
            {
              oct += octnum[counter-1];
            }
            return(oct);
        }
        
        public String getDecfromOct()
        {
            double z = 0;
            int c = 0;
            int m = 0;
            int x = 1;
            int length = kb.length();
            int[] thearray = new int[length];
            if(oct != "ERROR")
            {
                for(x = 0; x != length; x++)
                {
                    String norg = String.valueOf(kb.charAt(x));
                    int go = Integer.valueOf(norg);
                   
                    thearray[x] = go;           
                }
         
                for(z = length; z != 0; z--)
                {
                    int a = thearray[m];
                    double pow = Math.pow(8.0, z-1);
                    double b = a*pow;
                    c += b;
                    m++;
                }
            }
            else
                {
                    return(oct);
                }
               oct = Integer.toString(c);
               return(oct);
        }
        
    }
    Ignore how shitty and repetitive the code is please, this was done at the START of the year. Also, it converts it to a string due to the graphical interface used for it.

  9. #9
    Hydra
    Join Date
    Apr 2006
    Posts
    105
    BG Level
    3

    oh wow! thank you so much

  10. #10
    Cerberus
    Join Date
    Jun 2006
    Posts
    432
    BG Level
    4

    keep in mind its java and not c#

Similar Threads

  1. noob memory question
    By melbufrauma in forum Tech
    Replies: 10
    Last Post: 2009-10-22, 17:51