Integer to hexdecimal Coversion!!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

Integer to hexdecimal Coversion!!

 
0
  #1
Jul 3rd, 2008
hi ,

i have a simple problem here, that is how can i convert an Decimal integer into Hexadecimal integer and not in Hexadecimal string. I have wrote the following code and managed to get the output from the program. But for a certain reason i cant use it again with the integers, this is because the output hex value is actually a hex string. For my project i have to do calculations using hex values. So plz let me know if there is a class which converts decimal integer into Hexadecimal integer , or any other method which i can use :)
Thanks in Advance.
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class project {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. Scanner in = new Scanner (System.in);
  9. int x;
  10. System.out.print("\nEnter the integer :");
  11. x = in.nextInt();
  12. System.out.print("\nThe conversion of integer in hex is: "+ (Integer.toHexString(x)));
  13. }
  14.  
  15. }
Code doesnot compile:
  1.  
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. public class project {
  6.  
  7. public static void main(String[] args)
  8. {
  9. int x2;/*** I tried using it with String as well ***/
  10. Scanner in = new Scanner (System.in);
  11. int x;
  12. System.out.print("\nEnter the integer :");
  13. x = in.nextInt();
  14. System.out.print("\nThe conversion of integer in hex is: "+(Integer.toHexString(x)));
  15.  
  16. /******Integer.toHexString(x) gives Hex String of x ***/
  17.  
  18. x2 = (Integer.toHexString(x))/100;
  19.  
  20. System.out.print("\nCalculation does occur:"+x2);
  21.  
  22.  
  23. }
  24.  
  25. }
Error:
Output window:
W:\project.java:16: operator / cannot be applied to java.lang.String,int
x2 = (Integer.toHexString(x))/100;
^
1 error

TaskView:
operator / cannot be applied to java.lang.String,int
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,506
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Integer to hexdecimal Coversion!!

 
0
  #2
Jul 3rd, 2008
Use Integer.parseInt(java.lang.String,int) with a radix of 16 if you need to convert the hex string back to an int.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

Re: Integer to hexdecimal Coversion!!

 
0
  #3
Jul 6th, 2008
I managed to split up a 24 bit number by using signed and unsigned shifting of numbers. But i got a question from that part too. Is it true that if we shift a number by 32 bits , all data associated to the number is gone?
Forexample :
00FFFF(in hex)
if i shift it to left by 24 bits and then i shift it back to right by 24 bits the answer i will get is 0000FF.

Sytax used:
int x1;
x=input;
x1 = x<<24;
x1=x1>>>24;
end;

Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,506
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Integer to hexdecimal Coversion!!

 
0
  #4
Jul 7th, 2008
Why not find out for yourself? Run those lines (well, check your syntax before you do, because it's incorrect and "end;" isn't a valid statement) with System.out.println() calls after the operations and see what you get.

Your own computer will give you answers to questions like this much quicker and more accurately than anyone on the net.
Last edited by Ezzaral; Jul 7th, 2008 at 12:21 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

Re: Integer to hexdecimal Coversion!!

 
0
  #5
Jul 7th, 2008
Originally Posted by Ezzaral View Post
Why not find out for yourself? Run those lines (well, check your syntax before you do, because it's incorrect and "end;" isn't a valid statement) with System.out.println() calls after the operations and see what you get.

Your own computer will give you answers to questions like this much quicker and more accurately than anyone on the net.
That was just a pseudo code :p, thanks anyway
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC