Results 1 to 10 of 10
  1. #1
    nighteagle2 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Converting Number to Base 8

    Hey, so now im having trouble with the second part of my assignment, converting a number to Base 8. I already wrote a program that converts the number to binary.

    My Professors asking me to produce a string of the Base 8 equivalent of the original number and assign it to the instance variable of octal number.

    I can't figure out how to convert a number into an Octal without doing
    Java Code:
    String octalStr = Integer.toOctalString(numVal)
    My teacher wants us to actually convert it.

    Also how do i assign the original number to the instance variable of the octal number

    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Converting Number to Base 8

    How would you do this on paper, without a computer?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Converting Number to Base 8

    wrote a program that converts the number to binary.
    Generalize that program by changing the 2 to a variable and then call it with an 8 to get an octal String.

  4. #4
    nighteagle2 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Converting Number to Base 8

    Aite thanks man. One last question. So i have this code

    Java Code:
    public boolean checkOctal(String octalStr, int numVal)
    	{
    		
    		int p = 1;
    		int octalCheck = 0;
    		
    		for(int a = 64; a>=1 && p <= 3; a /= 8)
    		{	
    			if (octalStr.charAt(p) > 0)
    			{
    				octalCheck += (a * (octalStr.charAt(p)));
    			}
    			p++;	
    		}
    		if (octalCheck == numVal)
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    i can't figure out how to get this line to work
    octalCheck += (a * (octalStr.charAt(p)));

    When i try converting it to an int, It doesn't equal the value at the position on the string. But if i leave it as it is, it doesn't add up correctly

    Thanks

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Converting Number to Base 8

    i can't figure out how to get this line to work
    octalCheck += (a * (octalStr.charAt(p)));
    Please explain what that line is supposed to do?

    To see what the code is doing, break it up into simple, single statements and print out the results after each statement.

    When i try converting it to an int, It doesn't equal the value at the position on the string. But if i leave it as it is, it doesn't add up correctly
    Add some printlns that show what the code is doing. Copy the output and add comments to it describing what you want it to do.

  6. #6
    nighteagle2 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Converting Number to Base 8

    ok so octalStr is the base8 number, limited to 4 digits. I want it to multiple the second number by 64, third by 8, and fourth by 1 and add it all up.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Converting Number to Base 8

    What is printed out when you separate the statement into parts and print out everything for each statement?

  8. #8
    nighteagle2 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Converting Number to Base 8

    when i print (octalStr.charAt(p)), it'll show the right number.

    For example the base 8 of 34 is 0042. When i print
    Java Code:
    (octalStr.charAt(1))
    it'll print 0. which is correct
    But when i do:
    Java Code:
     octalCheck += (a * octalStr.charAt(1) );
    it'll print 3072

  9. #9
    nighteagle2 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Converting Number to Base 8

    a is 64 btw sorry

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Converting Number to Base 8

    Your approach is way off.
    Look at the conversion using base 10 since it will be easier to see:
    Find the value for "123"

    Set the intVal to 0

    Get the next digit: 1
    To add this value the current value in intVal you need to shift the intVal left 1 position (mult by 10)
    then add in the digit
    continue loop to the end of the string.

    As you get each digit, that means the the current number must be shifted left 1 digit (mult by 10)

    The result in base 10 display would be 123. 3 positions, 1 shifted left 2 positions, the 2 shifted one position, the 3 no shift

Similar Threads

  1. Replies: 7
    Last Post: 01-16-2012, 03:37 AM
  2. Replies: 2
    Last Post: 11-27-2011, 05:11 PM
  3. Replies: 1
    Last Post: 10-28-2011, 08:18 AM
  4. Converting whole number into decimal
    By jim01 in forum New To Java
    Replies: 2
    Last Post: 09-23-2010, 07:58 PM
  5. A Number Converting Program!
    By WastedxYears in forum New To Java
    Replies: 2
    Last Post: 01-09-2010, 12:47 AM

Posting Permissions

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