Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Converting from decimal to hexadecimal. Can't figure out how to output correctly

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Converting from decimal to hexadecimal. Can't figure out how to output correctly

     
    import java.util.Scanner;
     
    public class FindHexString {
    	public static void main(String[] args) {
     
    String hexString = ""; //output a hex string
    int decNumber;		//decimal number to be converted
    int storeInput; 	//copy f input decimal number
    int remainder;
     
    Scanner in = new Scanner(System.in);
     
    //read the decimal number to be converted
    System.out.println("Please enter a positive integer: ");
    decNumber = in.nextInt();
    storeInput = decNumber;		//remember input for late print
     
    //rejecting negative number input
    while(decNumber < 0 ){
    System.out.println("Enter only positive integers please!");
    decNumber = in.nextInt();
    storeInput = decNumber;
     
    }
     
    if(decNumber == 0 ){
    System.out.println("Input decimal number is: " + storeInput);
    System.out.println("Hexadecimal value is: 0");
    return;
    }
     
    //conversion process
    while(decNumber > 0){
    remainder = decNumber % 16;
    decNumber /= 16;
    hexString = remainder + hexString;
    }
     
    if(decNumber = 10){              //this is the part I'm having trouble with
    hexString = "A";
    }
     
    System.out.println("Input decimal number is " + storeInput);
    System.out.println("Hexadecimal value = " + hexString);
     
    	}
    }

    I am trying to figure out how to convert from decimal to hexadecimal without using the java library function. This is just a guess. I could be wrong.
    Please and thank you for your help.


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Converting from decimal to hexadecimal. Can't figure out how to output correctly

    If you just want to print the hexadecimal value corresponding to the decimal then you can use print formats like..
    System.out.printf("%X", integerValue);
    .

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Converting from decimal to hexadecimal. Can't figure out how to output correctly

    Quote Originally Posted by BTroj View Post
    //conversion process
    while(decNumber > 0){
    remainder = decNumber % 16;
    decNumber /= 16;
    hexString = remainder + hexString;
    }
    If you want to do the conversion yourself (no use of standard/predefined methods, for learning/exercise reasons), that code is not sufficient.
    The use of division/modulo operator is ok. But the modulo gives you a value 0...15 and for values in range 10...15 you must translate them to "A"..."F".
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Converting from decimal to hexadecimal. Can't figure out how to output correctly

    In addition, note the bug at:

    if(decNumber = 10){

    You'd want to use the double-equals equality operator. (Summary of Operators (The Java™ Tutorials > Learning the Java Language > Language Basics))

  5. #5
    Junior Member ZaneDarklace's Avatar
    Join Date
    Feb 2014
    Location
    Lawton, OK
    Posts
    15
    My Mood
    Lonely
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Converting from decimal to hexadecimal. Can't figure out how to output correctly

    This is an example of the code that i use to convert decimal numbers to hexadecimal numbers. you will need a for loop too.

    int x = 3901;
    System.out.println( Integer.toHexString(x) );
    Rock On

Similar Threads

  1. convertion hexadecimal to decimal and binary to decimal
    By Md.Ashraful Haque in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 07:30 AM
  2. Decimal to Hexadecimal, Without Java Libraries.
    By ribmant in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2012, 07:28 AM
  3. Converting decimal number to m minutes, n.nn seconds format.
    By coreysizemore in forum Java Theory & Questions
    Replies: 1
    Last Post: January 20th, 2012, 11:37 PM
  4. How do I change my output to 1 decimal place?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: November 3rd, 2011, 09:39 AM
  5. Converting Hex to Decimal
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 10
    Last Post: September 4th, 2011, 04:29 PM