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 :)

``````import java.util.*;
import java.io.*;

public class project {

public static void main(String[] args)	{

Scanner in = new Scanner (System.in);
int x;
System.out.print("\nEnter the integer :");
x = in.nextInt();
System.out.print("\nThe conversion of integer in hex is: "+ (Integer.toHexString(x)));
}

}``````

Code doesnot compile:

``````import java.util.*;
import java.io.*;

public class project {

public static void main(String[] args)
{
int x2;/*** I tried using it with String as well ***/
Scanner in = new Scanner (System.in);
int x;
System.out.print("\nEnter the integer :");
x = in.nextInt();
System.out.print("\nThe conversion of integer in hex is: "+(Integer.toHexString(x)));

/******Integer.toHexString(x) gives Hex String of x ***/

x2 = (Integer.toHexString(x))/100;

System.out.print("\nCalculation does occur:"+x2);

}

}``````

Error:
Output window:
W:\project.java:16: operator / cannot be applied to java.lang.String,int
x2 = (Integer.toHexString(x))/100;
^
1 error

operator / cannot be applied to java.lang.String,int

## All 4 Replies

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.