• 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

Problem encoding chars

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()); }
}
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Andrew Marshall
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic