aspose file tools
The moose likes Java in General and the fly likes Number format exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Number format exception" Watch "Number format exception" New topic
Author

Number format exception

John Gable
Greenhorn

Joined: Apr 12, 2004
Posts: 11
Hey,

I have the following piece of code that i want to convert what i read in from the text file into a binary representation so i can use it to do encryption.

It currently looks like this:



The test file is am using is already in binary.
00001111

The problem I'm having is a number exception error I can read the file in ok to a string - but when i try to convert the string to an integer with base 2 i get the format error.

My eventual aim is to convert text from a file into binary -> pass that into an encryption algorithm and encrypt. i can do the conversion from text to binary no probs - i just cant get past this number exception error.

This is the output from java. (btw it compiles fine)

C:\sdes>java SdesStart e 0000011111 testfile.txt
going to encrypt
100

Exception in thread "main" java.lang.NumberFormatException: For input string: "1
00
"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at SdesStart.readFile(SdesStart.java:119)
at SdesStart.main(SdesStart.java:43)

C:\sdes>
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
It appears from your output that there is a newline character being read in. Try doing this to remove any white space from the beginning or end of the String before you try to convert it to an int.



Also since you're doing encryption you'll probably want to use large numbers. I would recommend using BigInteger.
[ March 25, 2006: Message edited by: Keith Lynn ]
John Gable
Greenhorn

Joined: Apr 12, 2004
Posts: 11
Ahh yep works good now - thanks ..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number format exception
 
Similar Threads
baffling UTF-8 problem
Byte vs Character streams
Reading binary from a file and converting to CHAR
need to generate RSA keys from txt files
Reading image and convert it to Base64 format