Changing ASCII values in C#

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();

//
}
}
 
A

Andrew Faust

Jeffrey said:
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:

The so called extended ascii characters don't have a fixed visual
representation. What they are depends on the code page used to interpret
them. For example, a value may be the Pi symbol in one code page, but a
o with an accent in another code page.

The ascii encoding won't work as these aren't really ascii characters
(ascii is only 0 - 127). Try encoding it with Encoding.Default, as that
will allow the character to display using whatever the system default
encoding is (in US with Windows XP it's either Latin-1 or Latin-2, can't
remember which.)

Andrew Faust
 
J

Jeffrey Spoon

Andrew Faust said:
The so called extended ascii characters don't have a fixed visual
representation. What they are depends on the code page used to
interpret them. For example, a value may be the Pi symbol in one code
page, but a o with an accent in another code page.

The ascii encoding won't work as these aren't really ascii characters
(ascii is only 0 - 127). Try encoding it with Encoding.Default, as that
will allow the character to display using whatever the system default
encoding is (in US with Windows XP it's either Latin-1 or Latin-2,
can't remember which.)

Andrew Faust

I should have explained that the ASCII is originally displayable ASCII
(i.e. 0-127) and then has had it's ASCII value added to by 127, so then
it becomes extended ASCII and thus unreadable, in order to obfuscate it.
So now I'm trying to reverse it. I probably gave the impression I just
wanted displayable text, initially.

Anyway, I'll try the default encoding and see what values it gives me.

Thanks
 
J

Jon Skeet [C# MVP]

Jeffrey Spoon said:
I should have explained that the ASCII is originally displayable ASCII
(i.e. 0-127) and then has had it's ASCII value added to by 127, so then
it becomes extended ASCII and thus unreadable, in order to obfuscate it.
So now I'm trying to reverse it. I probably gave the impression I just
wanted displayable text, initially.

If you're just trying to reverse that then take the original byte
array, subtract 127 from each byte value, then use
Encoding.ASCII.GetString.
 
J

Jeffrey Spoon

Jon Skeet said:
If you're just trying to reverse that then take the original byte
array, subtract 127 from each byte value, then use
Encoding.ASCII.GetString.
Thanks Jon, I tried this but got more unprintable stuff. I just want to
check that I am changing the values properly, below.

Also, when I am reading the bytes from the text file do I need to use
ASCII encoding or can I just read the raw bytes?


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(aValue2 > 127){
int aValue = ByteArray;
int newValue = aValue - 127;
byte aNewByte = (byte) newValue;

decodedArray.SetValue(aNewByte,i);


}

Console.WriteLine("Count {0},value {1}",i,ByteArray);
Console.WriteLine("Count {0},value {1}",i,decodedArray);
i++;

}//while



fs.Close();//close reader

ASCIIEncoding ascii = new ASCIIEncoding();
String decoded = ascii.GetString(decodedArray);
Console.WriteLine(decoded);
 
J

Jon Skeet [C# MVP]

Jeffrey Spoon said:
Thanks Jon, I tried this but got more unprintable stuff. I just want to
check that I am changing the values properly, below.

Also, when I am reading the bytes from the text file do I need to use
ASCII encoding or can I just read the raw bytes?

It's better (and easier) to use Encoding.ASCII.

If you're still getting unprintable characters, it sounds like either
there were some unprintable (or non-ASCII) characters to start with, or
the method of obfuscation isn't exactly as you'd thought, or the data
has been corrupted on its way to you.

I can't immediately see anything wrong with your code (other than that
you won't be closing the file if an exception is thrown) but it's a bit
tortuous. Try this (untested):

byte[] data;
using (Stream x = [however you open the file])
{
// See http://www.pobox.com/~skeet/csharp/readbinary.html
data = ReadFully (stream, 10193);
}

for (int i=0; i < data.Length; i++)
{
data = (byte) data-127;
}

string decoded = Encoding.ASCII.GetString(data);
 
J

Jeffrey Spoon

Jon Skeet said:
It's better (and easier) to use Encoding.ASCII.

If you're still getting unprintable characters, it sounds like either
there were some unprintable (or non-ASCII) characters to start with, or
the method of obfuscation isn't exactly as you'd thought, or the data
has been corrupted on its way to you.

Yeah, I'm not sure if the obfuscation is dependent on one type of
Extended ASCII table or something. The results I'm getting now are
basically the same as what I was getting in the first place.
I can't immediately see anything wrong with your code (other than that
you won't be closing the file if an exception is thrown) but it's a bit
tortuous. Try this (untested):

Yes, I thought you'd said tortuous at first, although that's just as
apt. Not quite up to speed with C# syntax, that's my excuse.

I will try this code, thanks again.
byte[] data;
using (Stream x = [however you open the file])

This is how I open the file, not sure if it makes any difference. I'm
sure I don't need to use FileInfo, but anyway...

//Create a file stream from an existing file.
FileInfo fi=new FileInfo("C:\\mydir\\thefile");
FileStream fs=fi.OpenRead();
 
K

Kevin Spencer

Hi Jeffrey,

I believe that Jon's use of the "using" statement was related to his
statement that your app doesn't close the file if an exception occurs. The
"using" statement block ensures that the resource being "used" is disposed,
even if an exception occurs. It is similar to using a try/catch/finally
block in which the finally block closes the file, but is a nice, neat,
easy-to-write and easy-to-read format.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

This is, by definition, not that.

Jeffrey Spoon said:
Jon Skeet said:
It's better (and easier) to use Encoding.ASCII.

If you're still getting unprintable characters, it sounds like either
there were some unprintable (or non-ASCII) characters to start with, or
the method of obfuscation isn't exactly as you'd thought, or the data
has been corrupted on its way to you.

Yeah, I'm not sure if the obfuscation is dependent on one type of Extended
ASCII table or something. The results I'm getting now are basically the
same as what I was getting in the first place.
I can't immediately see anything wrong with your code (other than that
you won't be closing the file if an exception is thrown) but it's a bit
tortuous. Try this (untested):

Yes, I thought you'd said tortuous at first, although that's just as apt.
Not quite up to speed with C# syntax, that's my excuse.

I will try this code, thanks again.
byte[] data;
using (Stream x = [however you open the file])

This is how I open the file, not sure if it makes any difference. I'm sure
I don't need to use FileInfo, but anyway...

//Create a file stream from an existing file.
FileInfo fi=new FileInfo("C:\\mydir\\thefile");
FileStream fs=fi.OpenRead();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top