If you need more information then please ask.
Here is the source code with comments.
/* I get the following error or message when the program goes
into the
last for-loop to convert BACK from HEX to ASCII
it seems to only happen when the Length goes over 4 or
higher?
java.lang.NumberFormatException: For input string: "0041004100410071"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Hex2Ascii2HexTEST.main(Hex2Ascii2HexTEST.java:35)
*/
import java.io.*;
import java.lang.*;
//Program to Convert ASCII STRING to HEX then Back to ASCII STRING
class Hex2Ascii2HexTEST
{
public static void main(String[] args) throws Exception
{
String hexString ="";
// 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
System.out.println("ORIGINAL ascii String is ="+asciiString);
for(int i = 0; i<asciiString.length(); i++)
{
int ch=(int)asciiString.charAt( i );
String hexVal="00"+Integer.toHexString( ch );
System.out.println("HEX VALUE ="+hexVal); // String to Hex
hexString = hexString + hexVal;
}
//System.out.print();
System.out.println();
System.out.println("HEX STRING ="+hexString);
//CONVERT BACK TO ASCII
for(int ii = 0; ii<asciiString.length(); ii++)
{
//Convert back to Ascii(String) value
int intVal = Integer.parseInt(hexString, 16);
char charVal = (char) intVal;
System.out.println("Converted BACK to ASCII String ="+charVal);
}
}
}
}
If you read the documentation for Integer.parseInt, you'll note that
overflowing the input will throw the exception. Your input string is
equal to 56 bits, far more than the 31 bits an int can hold.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
So you're trying to convert "0041004100410071" to an int value.
The largest value an int can store is 2147483647. Line those two
numbers up and study them; do you see a difficulty? So does Java.
> java.lang.NumberFormatException: For input string: "0041004100410071"
> String hexVal="00"+Integer.toHexString( ch );
And here is where those extra 0's are coming from.
Why do you insist on calling "ASCII" that which is not ASCII?
You have no ASCII strings in this code, so you shouldn't use that as a
name or comment. It's misleading.
--
Lew
>Robert Blass wrote:
>> java.lang.NumberFormatException: For input string: "0041004100410071"
>> at java.lang.NumberFormatException.forInputString(Unknown Source)
>> at java.lang.Integer.parseInt(Unknown Source)
>> at Hex2Ascii2HexTEST.main(Hex2Ascii2HexTEST.java:35)
>
>If you read the documentation for Integer.parseInt, you'll note that
>overflowing the input will throw the exception. Your input string is
>equal to 56 bits, far more than the 31 bits an int can hold.
But I've not been making INT bigger, I was simply making a Char bigger
by...
string = string + stringvalue(int)
etc..
If "A" is ascii code 65 then what would I be calling it instead of
Ascii code #'s?
Note the key part of the advice: "read the documentation for
Integer.parseInt".
--
Lew
Java uses Unicode throughout, specifically, UTF-16. If you really want
to use ASCII, you'll have to use byte instead of char and byte[] instead
of String.
--
John W. Kennedy
"Give up vows and dogmas, and fixed things, and you may grow like
That. ...you may come to think a blow bad, because it hurts, and not
because it humiliates. You may come to think murder wrong, because it
is violent, and not because it is unjust."
-- G. K. Chesterton. "The Ball and the Cross"
>java.lang.NumberFormatException: For input string: "0041004100410071"
see
http://mindprod.com/jgloss/runerrormessages.html#NUMBERFORMATEXCEPTION
--
Roedy Green Canadian Mind Products
http://mindprod.com/politics/harper.html
Anyone but Harper for Prime Minister of Canada
Robert Blass wrote:
>> If "A" is ascii [sic] code 65 then what would I be calling it instead of
>> Ascii [sic] code #'s?
If "A" is also Unicode UTF-8, what would you call it?
As it happens, "A" is a String and is not ASCII, nor is the char value 'A'
ASCII. Its value is (char) 65, not (byte) 65. It does not have an ASCII
value at all.
Your premise is inaccurate: in Java, neither "A" nor 'A' is encoded in ASCII.
Even if they were, your conclusion does not follow, as 'A' has the value 65
in several encodings.
John W Kennedy wrote:
> Java uses Unicode throughout, specifically, UTF-16. If you really want
> to use ASCII, you'll have to use byte instead of char and byte[] instead
> of String.
GIYF, Robert.
java.sun.com has some material on encodings also.
--
Lew
for(int ii = 0; ii<asciiString.length()+3; ii++)
{
//Convert back to Ascii(String) value
int intVal = Integer.parseInt(hexString, 16);
char charVal = (char) intVal;
System.out.println("Converted BACK to ASCII String ="+charVal);
}
I'm simply trying to take that long string of hex values and
converting those back to a strings.
Converted string "00410041" convert to int of (string of first 4
digits of the beginning of the hexstring.
Repeat until end of string.length
I have to go 4 at a time since it's exceeded some method I guess?
I hope that made sense?
> for(int ii = 0; ii<asciiString.length()+3; ii++)
> {
> //Convert back to Ascii(String) value
> int intVal = Integer.parseInt(hexString, 16);
> char charVal = (char) intVal;
> System.out.println("Converted BACK to ASCII String ="+charVal);
> }
>
> I'm simply trying to take that long string of hex values and
> converting those back to a strings.
This can't work, not the way you're trying to get output. Go one at a
time, not four.
>
> Converted string "00410041" convert to int of (string of first 4
> digits of the beginning of the hexstring.
I'm curious: what do you expect this "00410041" to convert too? Do it
by hand please, one step at a time, including the int, the char and what
the char will represent. This is really important.
>
> Repeat until end of string.length
>
> I have to go 4 at a time since it's exceeded some method I guess?
Um, no. How's your assembly language? Are you in a formal course of
study (college)? Which one?
This is kinda important too.