943,949 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1368
  • Java RSS
Jul 3rd, 2008
0

Integer to hexdecimal Coversion!!

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008
Jul 3rd, 2008
0

Re: Integer to hexdecimal Coversion!!

Use Integer.parseInt(java.lang.String,int) with a radix of 16 if you need to convert the hex string back to an int.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 6th, 2008
0

Re: Integer to hexdecimal Coversion!!

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
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008
Jul 7th, 2008
0

Re: Integer to hexdecimal Coversion!!

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 7th, 2008
0

Re: Integer to hexdecimal Coversion!!

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JasperREport not working in .jar file
Next Thread in Java Forum Timeline: How to make a Splash Screen





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC