Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Taking any file and reading it into ascii code

Status
Not open for further replies.

Kitamora

Programmer
Dec 2, 2004
8
US
I'm trying to take any file, text, jpg, doc, whatever and taking what is in the file and read it in as Ascii code, count the number of codes and do arithmetic compression on the whole thing. My biggest problem is reading everything into an ascii code, I can read it as bytes, but I'm stuck as to how to read things in as ascii, or even convert once they are read. I would really prefer to read them in as ascii as the over head the other way would could be huge.

Thank you for any assistance.

Kitamora
 
I thought if you read in a byte[], then each character in the file is an 8-bit unsigned integer. If that's not what you mean by ASCII code, what do you mean?

Bob Rashkin
rrashkin@csc.com
 
but I'm stuck as to how to read things in as ascii
ASCII is only a series of bytes... Nothing more nothing less. If you mean to read them as chars...

What you might want to look into is:

I thought if you read in a byte[], then each character in the file is an 8-bit unsigned integer.
Sorry for being pedantic, but a byte is a byte. It is meaningless untill the user gives it an intrepretation.
 
Thanks I'll take a look at CharArrayReader, I guess I should have explained a bit more, I need to turn those bytes into the 0 - 255 decimal value that are equivalent to ascii. Basically any bite, gets turn into ascii, and then I'm to count how many times it appears. So I can use arithmetic compression. I got the byte I just can't figure out how to make it display as an ascii value of 0 - 255.

Thanks,

Kitamora
 
oops I mean chars, not bytes. Now I know that won't work. Okay it's been a long day and it's not even noon yet. Any help, would be still appreciated as CharArrayReader isn't exactly what I'm looking for, though I think I can use it to read in the file with it, I just still have the problem of converting them to ascii 0 - 255, unless I'm over looking something really simple, and someone needs to hit me over the head with it.

Thanks,

Kitamora
 
[bad joke]
Do like that general of Little Big Horn
[\bad joke]

Code:
byte readByte;
char asciiChar = (char) readByte;

[\code]

Cheers.

Dian
 
Thank you Dian,

I'll try that instead of the asciireader I found, that is a lot more simple.


Kit
 
Thank you all for the help, I finally figured out my own problem. ;-) Thougyh you all did help me go in the right direction, I was over looking something that was very very simple.

All I needed to do was set watever variable I was using to grab the buffered reader, was turn it, into an int, and then send it to an int array, this gives you the decimal vale for ascii.

For anyone who has the same problem as I did, and is over looking doing this the easy way, here you go:

BufferedReader in = new BufferedReader(new FileReader("C:\\test.txt" ) );

for(int i=0; i<buf.length; i++){
int data [ ] = new int[buf.length];
data = (int)in.read();
System.outprintln(data);


Kitamora
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top