Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"Ascii2Hex2Ascii" Compiler says "java.lang.NumberFormatException", What does this mean?

12 views
Skip to first unread message

Robert Blass

unread,
Oct 9, 2008, 10:07:21 AM10/9/08
to
I am getting some pretty weird 'messages' perhaps errors? When I Run
my program. It compiles with no errors or messages.
Could someone tell me what to do, or more import explain what that
message is trying to tell me?

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);
}
}
}
}

Joshua Cranmer

unread,
Oct 9, 2008, 10:27:52 AM10/9/08
to
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.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Eric Sosman

unread,
Oct 9, 2008, 10:31:56 AM10/9/08
to
Robert Blass wrote:
> I am getting some pretty weird 'messages' perhaps errors? When I Run
> my program. It compiles with no errors or messages.
> Could someone tell me what to do, or more import explain what that
> message is trying to tell me?
>
> 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)
> [...]

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.

--
Eric....@sun.com

Mark Space

unread,
Oct 9, 2008, 12:34:32 PM10/9/08
to
Robert Blass wrote:
> I am getting some pretty weird 'messages' perhaps errors? When I Run

> java.lang.NumberFormatException: For input string: "0041004100410071"

> String hexVal="00"+Integer.toHexString( ch );

And here is where those extra 0's are coming from.

Lew

unread,
Oct 9, 2008, 3:25:35 PM10/9/08
to
Robert Blass wrote:
...

>         last for-loop to convert BACK from HEX to ASCII
...

> //Program to Convert ASCII STRING to HEX then Back to ASCII STRING
...
>       String asciiString = (br.readLine());
...
>         //CONVERT BACK TO ASCII
...

>       System.out.println("Converted BACK to ASCII String ="+charVal);

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

unread,
Oct 9, 2008, 5:33:09 PM10/9/08
to
On Thu, 09 Oct 2008 10:27:52 -0400, Joshua Cranmer
<Pidg...@verizon.invalid> sayd the following:

>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..

Robert Blass

unread,
Oct 9, 2008, 5:34:49 PM10/9/08
to
On Thu, 9 Oct 2008 12:25:35 -0700 (PDT), Lew <l...@lewscanon.com> sayd
the following:

If "A" is ascii code 65 then what would I be calling it instead of
Ascii code #'s?


Lew

unread,
Oct 9, 2008, 5:32:51 PM10/9/08
to
On Oct 9, 5:33 pm, Robert Blass <bl...@messenger.xcx> wrote:
> On Thu, 09 Oct 2008 10:27:52 -0400, Joshua Cranmer
> <Pidgeo...@verizon.invalid> sayd the following:

Note the key part of the advice: "read the documentation for
Integer.parseInt".

--
Lew

John W Kennedy

unread,
Oct 9, 2008, 6:31:01 PM10/9/08
to

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"

Roedy Green

unread,
Oct 9, 2008, 8:22:34 PM10/9/08
to
On Thu, 09 Oct 2008 10:07:21 -0400, Robert Blass <bl...@messenger.xcx>
wrote, quoted or indirectly quoted someone who said :

>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

Lew

unread,
Oct 9, 2008, 8:39:10 PM10/9/08
to
Lew sayd

>>> You have no ASCII strings in this code, so you shouldn't use that as a
>>> name or comment. It's misleading.

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

Robert Blass

unread,
Oct 10, 2008, 12:41:34 PM10/10/08
to
On Thu, 09 Oct 2008 09:34:32 -0700, Mark Space
<mark...@sbcglobal.net> sayd the following:


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?

Mark Space

unread,
Oct 10, 2008, 1:23:09 PM10/10/08
to
Robert Blass wrote:

> 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.

0 new messages