• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading integers from raw text files

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
This is my 1st post so please be gentle
I'm currently trying to read in a raw text file of ints in an android application and running into all sorts of issues. Currently I have the code -



This works great, except that it reads the the ascii code of the int instead of the value, eg. 2 in my file turns into 50. I've tried different ways of reading the file such as using dataInputStreams and bufferedInputSteeams but these don't seem to compile at all so I think I'm barking up the wrong tree with that approach. As far as I can see I have 2 options, store the ints in a different format like a database (seems like overkill), convert the ascii codes to there represented values in some way (should work if I knew how).
Does anyone have any ideas? I've chased my tail all over the net with this one.

Thanks
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The InputStream.read() method reads a single byte. If you created a text file with a "2" in the first position, that "2" is an ASCII character, and "2" in ASCII is really 0x32 in hex, and 0x32 in hex is 50 as an integer.

If you want to read ASCII (or UTF-8) characters out of a file you must use a text input method, not a binary input method. Try the Reader.read() methods or BufferedReader.readline().
reply
    Bookmark Topic Watch Topic
  • New Topic