Re: Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.



Robert Blass wrote:
I've completed a working program. It takes console input of an ascii
string, converts it to it's Hex value and then converts the Hex value
back to the Ascii string value.

import java.io.*;
import java.lang.*;
//Convert ASCII to HEX Then Convert BACK to string value
class Hex2Ascii2Hex
{
public static void main(String[] args) throws Exception
{
// system.in reader (the input from console)
InputStreamReader inputStream = new InputStreamReader(System.in);
// buffer the console reader
BufferedReader br = new BufferedReader(inputStream);
{
// output the question.
System.out.print("Enter STRING : ");
// read console intput one line (BR.readLine) and store as
String
String asciiString = (br.readLine());
// Covert to HEX
for(int i = 0; i<asciiString.length(); i++)
{
System.out.println();
int ch=(int)asciiString.charAt( i );
String hexVal="00"+Integer.toHexString( ch );
System.out.print(i + "HEX VALUE IS :"+hexVal); // String to Hex

//Convert back to Ascii(String) value
int intVal = Integer.parseInt(hexVal, 16);
char charVal = (char) intVal;
System.out.print("...ASCII(String) Value : "+charVal);
}
}
}
}

Do you, or anyone else, see room for improvement??

I plan on adding functionality to make all the input from a file and
all output to a file. I may need console output to give display of
steps.

Thanks.

The big thing I can see is that you shouldn't use the word "Ascii", when what you're really doing is looking at the UTF-16 values, not Ascii.

I would also replace your hexVal creation with something that will pad zeros instead of just adding two zeros before it.


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
.



Relevant Pages

  • Re: healp reading / writing binary strings.
    ... Robert Klemme wrote: ... > And how can I write the same type of string from say an ... to the ascii table as well? ... for hex, does the hex value I supply correspond to the ascii table as ...
    (comp.lang.ruby)
  • Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
    ... It takes console input of an ascii ... converts it to it's Hex value and then converts the Hex value ... back to the Ascii string value. ...
    (comp.lang.java.help)
  • Re: Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
    ... // buffer the console reader ... BufferedReader br = new BufferedReader( ... Just making a point that all numbers are hex, decimal and binary and internal format doesn't really matter. ... public static void main(String[] args) throws Exception ...
    (comp.lang.java.help)
  • Re: Creating a unique number from a text string
    ... For example you could turn each character's ASCII ... code into a hex number: ... You can also look at something called Base-64, ... designed to turn any string into "easy" characters. ...
    (comp.databases.ms-access)
  • Re: How to construct a string with bunch of hex numbers?
    ... to construct a string with those hex numbers? ... No. I'd like to have a string like "xyzabc" and each ASCII char corresponds ... to a hex number. ...
    (microsoft.public.dotnet.languages.csharp)