Post: [help] String/text to hex
11-14-2015, 11:22 PM #1
HeroWin
Java Developer
(adsbygoogle = window.adsbygoogle || []).push({}); I've been trying all day testing snippets of other people code to convert the entered string calue to hex "0x__________"

ive assigned the code to perform its actions within a button and output it to a text field...

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
String value = jTextField2.getText();

jTextField3.setText(value);
JOptionPane.showMessageDialog(null,"Hello, you are about to convert \n"
+ "this line of string values to hexadecimal\n"
+ ""
+ "this vlaue is " + value);
11-14-2015, 11:33 PM #2
gopro_2027
Vault dweller
I am not sure exactly how hexadecimal works but I think it is just the number value of each char in the string. like each char has a certain number but I am probably wrong. If this were right htough you could do it like
String ret = "";
for (int i = 0; i < str.length(); i++)
ret = ret + ((int)str.charAt(i));
11-15-2015, 12:49 AM #3
Specter
Pro Memer
Originally posted by HeroWin View Post
I've been trying all day testing snippets of other people code to convert the entered string calue to hex "0x__________"

ive assigned the code to perform its actions within a button and output it to a text field...

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
String value = jTextField2.getText();

jTextField3.setText(value);
JOptionPane.showMessageDialog(null,"Hello, you are about to convert \n"
+ "this line of string values to hexadecimal\n"
+ ""
+ "this vlaue is " + value);




Do you want like numbers entered into the textbox to be converted to hex or a string converted to hex thru ASCII? I'm assuming you want to convert a string like "Hello World" to hex, so I'll take you through the steps you need.

First you need to convert the string into ascii values, what I'd do is parse each character of the string one at a time using a loop, and convert the char to an int datatype which would leave you with it's ASCII value, example;

    
for(int i = 0; i < str.length(); i++)
{
char selection = str.charAt(i);
int asciiVal = (int)selection;
}


Now that you have this you can start to do stuff with it, possibly in that for loop you can call a function I'll provide below and add it to a string or something.

I wrote this function a long time ago, basically hex is base 16 so you have 0-9 then 10 => A, 11 => B, 12 => C, 13 => D, 14 => E, 15 => F. The function first checks if the number initiated by an argument (x) "result" is above 0 and loops the code inside it if the condition that "x > 0" retains true (as we modify result later on). Then we'll check the result of the number % 16 (modulus, otherwise known as remainder of divided by). If it's above 9, we'll substitute it with letters A-F for 9-16 (not including) respectively. If not, it'll just keep the number. Then it will add either the letter or number to "hexbuffer", a string. Finally before conditionally looping again, it will take "result" and divide it by 16, and reiterate the above process if the result is not 0.

I believe it's similar to a process used in converting decimal to binary using a "double dabble" system, or at least that's what I've heard it called. You're basically just taking the number, checking it's modulus, using that value, then dividing it by 16 and repeating the process until you hit 0. The function will finally take the string "hexbuffer", turn it into a stringbuffer, reverse it (as you're finding it in reverse so you have to reverse the order you found the values to get the actual string), and converting it to a string again before returning it.

    
public static String dec2hex(int x)
{
int result = x;
int i = 0;

String hexBuffer = "";

while(result > 0) {
if(result % 16 > 9) {
switch((result % 16)) {
case 10:
hexBuffer += "A";
break;
case 11:
hexBuffer += "B";
break;
case 12:
hexBuffer += "C";
break;
case 13:
hexBuffer += "D";
break;
case 14:
hexBuffer += "E";
break;
case 15:
hexBuffer += "F";
break;
}
} else {
hexBuffer += (result % 16);
}
result /= 16;
i++;
}
hexBuffer = new StringBuilder(hexBuffer).reverse().toString();
return hexBuffer;
}


The reason I wrote an essay on explaining what it does is because it's better to understand what the code is doing and how the function works rather than just what it does :p

Originally posted by pro View Post
I am not sure exactly how hexadecimal works but I think it is just the number value of each char in the string. like each char has a certain number but I am probably wrong. If this were right htough you could do it like
String ret = "";
for (int i = 0; i < str.length(); i++)
ret = ret + ((int)str.charAt(i));


That'll get you the ASCII value of each character of the string, which is the first part of converting, but you still need to convert that to hex.
Last edited by Specter ; 11-15-2015 at 12:55 AM.

The following 3 users say thank you to Specter for this useful post:

Bitwise, DaenerysTargy

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo