Re: Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
- From: Daniel Pitts <newsgroup.spamfilter@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 06 Oct 2008 13:13:13 -0700
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/>
.
- References:
- Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
- From: Robert Blass
- Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
- Prev by Date: Re: Better way of handling error codes with messages
- Next by Date: Re: Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
- Previous by thread: Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
- Next by thread: Re: Attn:Mark Space... My Ascii 2 Hex back 2 Ascii program.
- Index(es):
Relevant Pages
|