Problem reading in ascii

I am being given an ascii file that has been bitshifted by 1 place. I am to simply read in each character and then divide its value by 2 and output the character. This all works fine except for the odd few character.

try

{

BufferedReader br =new BufferedReader(new FileReader("C:\\bitshift.txt" ));

int rd_line;

while ((rd_line = br.read()) != -1)

{

//rd_line = (rd_line%255)/2;

rd_line = rd_line/2;

System.out.println((char)rd_line +"-" + rd_line);

}

}

catch( IOException e)

{

}

I have read some topics on this and think a possible reason is that the buffered reader think it is unicode and not ascii. Is this correct or am I way off

Regards

[1222 byte] By [jayclarkea] at [2007-10-2 10:33:56]
# 1
Yes, it is Unicode. Why it doesn't work depends on what "doesn't work" means.
CeciNEstPasUnProgrammeura at 2007-7-13 2:19:56 > top of Java-index,Java Essentials,Java Programming...
# 2

Doesn't work means the following. This is an output of my results.

Note: all lower case characters are fine its just upper case.

The '?' is meant to be 'A' and seeing as I am outputting half of what i read 4109, I must have read the number (4109*2). I have checked on the system that is supplying me with the file to read from and that is passing me the correct value of ascii 130.

?-4109

If I am being passed Ascii, is there a way to read in ascii rather that unicode?

Regards

jayclarkea at 2007-7-13 2:19:56 > top of Java-index,Java Essentials,Java Programming...
# 3

The following creates an adapted InputStream and shifts the byte behind the scenes.

import java.io.*;

class ShiftedInputStream extends InputStream

{

ShiftedInputStream(InputStream source)

{

source_ = source;

}

public int read() throws IOException

{

int bite = source_.read();

if (bite != -1)

bite >>>= 1;

return bite;

}

private InputStream source_;

}

public class Fred21

{

public static void main(String[] args)

{

try

{

BufferedReader br = new BufferedReader( new InputStreamReader(new ShiftedInputStream(new FileInputStream( "C:\\bitshift.txt" )), "ASCII"));

String line = null;

while ((line = br.readLine()) != null)

{

System.out.println(line);

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

sabre150a at 2007-7-13 2:19:56 > top of Java-index,Java Essentials,Java Programming...
# 4
4109 is 'A'? Since when?
CeciNEstPasUnProgrammeura at 2007-7-13 2:19:56 > top of Java-index,Java Essentials,Java Programming...
# 5
Thats what im saying, 4109 is not A. 130 is A. But when I am reading in 130, and then divide it by 2 I get 4109 for some reason and a ?
jayclarkea at 2007-7-13 2:19:56 > top of Java-index,Java Essentials,Java Programming...
# 6
> Thats what im saying, 4109 is not A. 130 is A. But> when I am reading in 130, and then divide it by 2 I> get 4109 for some reason and a ?Ignore your original code! It will not work!
sabre150a at 2007-7-13 2:19:56 > top of Java-index,Java Essentials,Java Programming...
# 7

Thanks very much. Could you explain what the following does. I understand it shift the bits but not sure how?

> class ShiftedInputStream extends InputStream

> {

>ShiftedInputStream(InputStream source)

>{

> source_ = source;

>}

>

>public int read() throws IOException

>{

> int bite = source_.read();

> if (bite != -1)

> bite >>>= 1;

> return bite;

>}

>

>private InputStream source_;

> }

Regards

jayclarkea at 2007-7-13 2:19:56 > top of Java-index,Java Essentials,Java Programming...
# 8

> Thanks very much. Could you explain what the

> following does. I understand it shift the bits but

> not sure how?

It reads a byte from the original stream and if it is not the end of file (-1) then it divides the value by 2 (using a single shift right) before returning it!

sabre150a at 2007-7-13 2:19:56 > top of Java-index,Java Essentials,Java Programming...