J
Jeffrey Spoon
Hello, I'm a bit stuck trying to convert a text file which contains
extended ASCII text and changing the ASCII values so they become
readable. I do this by subtracting 127 from the ASCII value. However, at
the moment I am just getting more gibberish so I'm probably doing
something wrong. I tried using ASCII Encoding before to get the ASCII
values. Although this worked for 0-127 ASCII values, extended ASCII gave
strange values (such as 1992 etc.) so I'm not sure how to adjust for
that. Here is what I have currently:
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//Create a file stream from an existing file.
FileInfo fi=new FileInfo("C:\\Documents and
Settings\\Files\\FILE1");
FileStream fs=fi.OpenRead();
//Read 171 bytes into an array from the
specified file.
int nBytes=10193;
//int nBytes =10193;
byte[] ByteArray=new byte[nBytes];
int nBytesRead=fs.Read(ByteArray, 0, nBytes);
Console.WriteLine("{0} bytes have been read from
the specified file.", nBytesRead.ToString());
byte[] decodedArray=new Byte[nBytes];
//look at bytes
int i =0;
while(i < ByteArray.Length){
int aValue2;
byte a = (byte)ByteArray.GetValue(i);
aValue2 = (int) a;
//if(ByteArray.GetValue(i) > 127){
if(aValue2 > 127){
int aValue = ByteArray;
int newValue = aValue - 127;
byte aNewByte = (byte) newValue;
decodedArray.SetValue(aNewByte,i)
;
//int value = ByteArray;
//int newValue = value - 127;
//ByteArray.SetValue(newValue,ByteArray[i
]);
}
Console.WriteLine("Count {0},value
{1}",i,ByteArray);
Console.WriteLine("Count {0},value
{1}",i,decodedArray);
i++;
}//while
fs.Close();//close reader
//wait for keypress
Console.Write("Press any <RETURN> to exit");
Console.Read();
//write
FileInfo fi2 = new FileInfo("C:\\Documents and
Settings\\Desktop\\Files\\newfile.dat");
FileStream fs2 =new
FileStream("newfile.dat",FileMode.Create);
//fs2.Write(ByteArray,0,nBytes);
fs2.Write(decodedArray,0,nBytes);
Console.WriteLine("bytes have been written from
the specified file.");
fs2.Close();
//
}
}
extended ASCII text and changing the ASCII values so they become
readable. I do this by subtracting 127 from the ASCII value. However, at
the moment I am just getting more gibberish so I'm probably doing
something wrong. I tried using ASCII Encoding before to get the ASCII
values. Although this worked for 0-127 ASCII values, extended ASCII gave
strange values (such as 1992 etc.) so I'm not sure how to adjust for
that. Here is what I have currently:
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//Create a file stream from an existing file.
FileInfo fi=new FileInfo("C:\\Documents and
Settings\\Files\\FILE1");
FileStream fs=fi.OpenRead();
//Read 171 bytes into an array from the
specified file.
int nBytes=10193;
//int nBytes =10193;
byte[] ByteArray=new byte[nBytes];
int nBytesRead=fs.Read(ByteArray, 0, nBytes);
Console.WriteLine("{0} bytes have been read from
the specified file.", nBytesRead.ToString());
byte[] decodedArray=new Byte[nBytes];
//look at bytes
int i =0;
while(i < ByteArray.Length){
int aValue2;
byte a = (byte)ByteArray.GetValue(i);
aValue2 = (int) a;
//if(ByteArray.GetValue(i) > 127){
if(aValue2 > 127){
int aValue = ByteArray;
int newValue = aValue - 127;
byte aNewByte = (byte) newValue;
decodedArray.SetValue(aNewByte,i)
;
//int value = ByteArray;
//int newValue = value - 127;
//ByteArray.SetValue(newValue,ByteArray[i
]);
}
Console.WriteLine("Count {0},value
{1}",i,ByteArray);
Console.WriteLine("Count {0},value
{1}",i,decodedArray);
i++;
}//while
fs.Close();//close reader
//wait for keypress
Console.Write("Press any <RETURN> to exit");
Console.Read();
//write
FileInfo fi2 = new FileInfo("C:\\Documents and
Settings\\Desktop\\Files\\newfile.dat");
FileStream fs2 =new
FileStream("newfile.dat",FileMode.Create);
//fs2.Write(ByteArray,0,nBytes);
fs2.Write(decodedArray,0,nBytes);
Console.WriteLine("bytes have been written from
the specified file.");
fs2.Close();
//
}
}