Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


i am developing an application in which user will input a hexnumber and an address, application have override hexnumber at that address. One major issue i am facing that since user is giving value from console it is in the form of string, i want to use same value as no convertion is required ie if user gives 0x9A567BFF it will be in the form of string but i want to the same as hex in application. Please help.
Posted

1 solution

StringToHexConvert.java

/**
* Convert a String to Hexa decimal form.
*/
import java.io.*;

class StringToHexConvert {

String stringToHex(String str) {
char[] chars = str.toCharArray();
StringBuffer strBuffer = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
strBuffer.append(Integer.toHexString((int) chars[i]));
}
return strBuffer.toString();
}

public static void main(String[] args) throws IOException {
System.out.print("Enter the String : ");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String inputString = br.readLine();
StringToHexConvert obj = new StringToHexConvert();
String hexString = obj.stringToHex(inputString);
System.out.println("String in hexa form : " + hexString);
}
}
Output of the program:

Enter the String: mahendra
String in hexa form: 6d6168656e647261
 
Share this answer
 
Comments
vikasdogra 6-Sep-11 3:21am    
Hi praveen,
This is the only problem my input will be a hexnumber ie ox1A4F5BCC BUT BY default it will be considered as a string i want to use same number ie ox1A4F5BCC as a hex......i dont want to convert.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900