By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

Changing ASCII values in C#

P: n/a


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[i];
int newValue = aValue - 127;
byte aNewByte = (byte) newValue;

decodedArray.SetValue(aNewByte,i)
;
//int value = ByteArray[i];
//int newValue = value - 127;
//ByteArray.SetValue(newValue,ByteArray[i
]);

}

Console.WriteLine("Count {0},value
{1}",i,ByteArray[i]);
Console.WriteLine("Count {0},value
{1}",i,decodedArray[i]);
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();

//
}
}
--
Jeffrey Spoon

May 24 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Jeffrey Spoon wrote:

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
May 24 '06 #2

P: n/a
In message <e#**************@TK2MSFTNGP05.phx.gbl>, Andrew Faust
<ae****@aradyme.com> writes

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

--
Jeffrey Spoon

May 24 '06 #3

P: n/a
Jeffrey Spoon <Je**********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 24 '06 #4

P: n/a
In message <MP************************@msnews.microsoft.com >, Jon Skeet
<?@pobox.com.invalid> writes
Jeffrey Spoon <Je**********@hotmail.com> wrote:
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.

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[i];
int newValue = aValue - 127;
byte aNewByte = (byte) newValue;

decodedArray.SetValue(aNewByte,i);
}

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

}//while

fs.Close();//close reader

ASCIIEncoding ascii = new ASCIIEncoding();
String decoded = ascii.GetString(decodedArray);
Console.WriteLine(decoded);
--
Jeffrey Spoon

May 25 '06 #5

P: n/a
Jeffrey Spoon <Je**********@hotmail.com> wrote:
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[i] = (byte) data[i]-127;
}

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 25 '06 #6

P: n/a
In message <MP************************@msnews.microsoft.com >, Jon Skeet
<?@pobox.com.invalid> writes

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


--
Jeffrey Spoon

May 25 '06 #7

P: n/a
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" <Je**********@hotmail.com> wrote in message
news:Ny**************@nowhere.nnn...
In message <MP************************@msnews.microsoft.com >, Jon Skeet
<?@pobox.com.invalid> writes

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


--
Jeffrey Spoon

May 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.