aspose file tools*
The moose likes Beginning Java and the fly likes Problem encoding chars Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem encoding chars" Watch "Problem encoding chars" New topic
Author

Problem encoding chars

Andrew Marshall
Greenhorn

Joined: Jul 09, 2003
Posts: 11
Cowpokes and caballeros,
YES, this is a class assignment, but your responses will not improve my grade - only my personal understanding of Java.
I wrote a program that is supposed to encode/decode files according to my own encoding scheme. I decided to convert each character's ASCII code to Hex, reverse the Hex digits, and then convert the Hex back to a char. For example - A (65 ASCII) -> 41 (in Hex) -> 14 (reversed Hex) -> � (20 ASCII).
This scheme works perfectly EXCEPT that it seems to spaz when reading characters with ASCII codes from 128 through 159 (80 through 9F in Hex). I'm reading files into a char array using a BufferedReader's read(char[], int, int) method. Any and all help is appreciated.
Andrew
Here's the code without comments and with compacted formatting:
import java.io.*;
public class P5 {
public static void main(String[] args) {
P5 myApp = new P5();
myApp.run(args[0]); }
public void run(String filename) {
char[] originalFile = null;
char[] newFile = null;
//read the file
{//Set up the arrays to be the correct length
File theFile = new File(filename);
originalFile = new char[(int)theFile.length()];
newFile = new char[(int)theFile.length()]; }
{//Perform the actual read
BufferedReader fileInput = null;
try {
fileInput = new BufferedReader(new FileReader(filename));
fileInput.read(originalFile, 0, originalFile.length); }
catch (EOFException eof) {
/*we want this to happen!*/ }
catch (Exception e) {
System.out.println(e.getMessage()); } }
//en/decrypt
for (int i = 0; i < originalFile.length; i++) {
int sixteensPlace = 0, onesPlace = 0;
sixteensPlace = ( (int)originalFile[i] / 16 );
onesPlace = ( (int)originalFile[i] % 16 );
newFile[i] = (char)( (onesPlace * 16) + sixteensPlace );
//the next line is for debugging
System.out.println(originalFile[i] + "=" + (int)originalFile[i] + "=" + sixteensPlace + "." + onesPlace + " -> " + (int)newFile[i] + "=" + newFile[i]); }
//write the new file
{BufferedWriter fileOutput = null;
try {
fileOutput = new BufferedWriter(new FileWriter(filename + "_enc"));
fileOutput.write(newFile, 0, newFile.length);
fileOutput.close(); }
catch (Exception e) {
System.out.println(e.getMessage()); }
}
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


This scheme works perfectly EXCEPT that it seems to spaz when reading characters with ASCII codes from 128 through 159 (80 through 9F in Hex).

Can you give us a slightly more technical description of "spaz"? What goes wrong, exactly?


[Jess in Action][AskingGoodQuestions]
Andrew Marshall
Greenhorn

Joined: Jul 09, 2003
Posts: 11

My bad!
Here is key output from the following new line of debugging code:
System.out.println(i + ", " + originalFile[i] + ", " + (byte)originalFile[i] + ", " + (int)originalFile[i]);
120, x, 120, 120
121, y, 121, 121
122, z, 122, 122
123, {, 123, 123
124, |, 124, 124
125, }, 125, 125
126, ~, 126, 126
127, ⌂, 127, 127
128, �, -84, 8364
129, ?, -3, 65533
130, �, 26, 8218
131, �, -110, 402
132, �, 30, 8222
133, �, 38, 8230
134, �, 32, 8224
135, �, 33, 8225
136, �, -58, 710
137, �, 48, 8240
138, �, 96, 352
139, �, 57, 8249
140, �, 82, 338
141, ?, -3, 65533
142, �, 125, 381
143, ?, -3, 65533
144, ?, -3, 65533
145, �, 24, 8216
146, �, 25, 8217
147, �, 28, 8220
148, �, 29, 8221
149, �, 34, 8226
150, �, 19, 8211
151, �, 20, 8212
152, �, -36, 732
153, �, 34, 8482
154, �, 97, 353
155, �, 58, 8250
156, �, 83, 339
157, ?, -3, 65533
158, ₧, 126, 382
159, �, 120, 376
160, �, -96, 160
161, �, -95, 161
162, �, -94, 162
163, �, -93, 163
164, �, -92, 164
165, �, -91, 165
166, �, -90, 166
167, �, -89, 167
168, �, -88, 168
169, ⌐, -87, 169
Notice how the (byte) and (int) values seem "spazzy" from ASCII 128 through 159?
Does it help to clarify that I simply want to read bytes from any file, perform math (bit swapping) on the bytes, and then write the bytes to a new file?
Confused Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem encoding chars