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

Writing extended ascii characters to text file.

P: n/a
JSM
Hi,

I am just trying to port an existing simple encryption routine to C#. this
routine simply adds/substracts 10 ascii characters to each character in a
text file (except quotes). The routine for decrypting the file works fine
however when I encrypt the file, several characters are corrupted. when I
looked into it they are always extended ascii characters (eg "x" which is
ascii character 120 gets translated to ascii character 130 which is part of
the extended ascii range of characters). I am assuming this has something to
do with the Encoding which I used to read/write the text files but I just
can't seem to get it to work. I have tried specifying all different types of
encoding (Unicode, Ascii, UTF7/8 without success).

I can't change the encryption method because this needs to work with
existing installations of my application.

Below are the two routines. Any ideas ? I thought this would be a very
simple task but alas I was wrong!

Cheers,

John
--------------------------------------

public void DecryptFile(string SourceFile,string DestFile)
{
string line;
StreamReader sr=new StreamReader(SourceFile,Encoding.Default);
StreamWriter sw=new StreamWriter(DestFile,false,Encoding.Default);

while ((line=sr.ReadLine())!=null)
{
string newline="";

for (int x=0;x<line.Length;x++)
{
if ((line.Substring(x,1)==qte) ||
(line.Substring(x,1)==Convert.ToChar(44).ToString( )))
newline +=line.Substring(x,1);
else
{
newline +=
Convert.ToChar((int)Encoding.Default.GetBytes(line .Substring(x,1))[0]-10).To
String();
}
}
sw.WriteLine(newline);
}

sr.Close();
sw.Close();
}
public void EncryptFile(string SourceFile,string DestFile)
{
string line;
StreamReader sr=new StreamReader(SourceFile,Encoding.Default);
StreamWriter sw=new StreamWriter(DestFile,false, Encoding.Default);

while ((line=sr.ReadLine())!=null)
{
string newline="";
for (int x=0;x<line.Length;x++)
{
if ((line.Substring(x,1)==qte) ||
(line.Substring(x,1)==Convert.ToChar(44).ToString( )))
newline +=line.Substring(x,1);
else
{
newline +=
Convert.ToChar((int)Encoding.Default.GetBytes(line .Substring(x,1))[0]+10).To
String();
}
}

sw.WriteLine(newline);
}

sr.Close();
sw.Close();
}
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi JSM,

First of all, remember that all strings in .NET are internally Unicode
strings, so in order to get real ASCII codes you should use the GetBytes
method of an Encoding instance configured for the ASCII encoding (as far as
I remember there is a static instance accessible as Encoding.Ascii). Now
that you have the bytes with the ASCII codes, you do the encryption and
again, you've got bytes, not characters. Therefore, it seems natural to
write out the resultant bytes in a binary mode where the issue of character
encoding simply is out of the picture.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"JSM" <js*@spam.pacific.net.au> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
Hi,

I am just trying to port an existing simple encryption routine to C#. this
routine simply adds/substracts 10 ascii characters to each character in a
text file (except quotes). The routine for decrypting the file works fine
however when I encrypt the file, several characters are corrupted. when I
looked into it they are always extended ascii characters (eg "x" which is
ascii character 120 gets translated to ascii character 130 which is part
of
the extended ascii range of characters). I am assuming this has something
to
do with the Encoding which I used to read/write the text files but I just
can't seem to get it to work. I have tried specifying all different types
of
encoding (Unicode, Ascii, UTF7/8 without success).

I can't change the encryption method because this needs to work with
existing installations of my application.

Below are the two routines. Any ideas ? I thought this would be a very
simple task but alas I was wrong!

Cheers,

John
--------------------------------------

public void DecryptFile(string SourceFile,string DestFile)
{
string line;
StreamReader sr=new StreamReader(SourceFile,Encoding.Default);
StreamWriter sw=new StreamWriter(DestFile,false,Encoding.Default);

while ((line=sr.ReadLine())!=null)
{
string newline="";

for (int x=0;x<line.Length;x++)
{
if ((line.Substring(x,1)==qte) ||
(line.Substring(x,1)==Convert.ToChar(44).ToString( )))
newline +=line.Substring(x,1);
else
{
newline +=
Convert.ToChar((int)Encoding.Default.GetBytes(line .Substring(x,1))[0]-10).To
String();
}
}
sw.WriteLine(newline);
}

sr.Close();
sw.Close();
}
public void EncryptFile(string SourceFile,string DestFile)
{
string line;
StreamReader sr=new StreamReader(SourceFile,Encoding.Default);
StreamWriter sw=new StreamWriter(DestFile,false, Encoding.Default);

while ((line=sr.ReadLine())!=null)
{
string newline="";
for (int x=0;x<line.Length;x++)
{
if ((line.Substring(x,1)==qte) ||
(line.Substring(x,1)==Convert.ToChar(44).ToString( )))
newline +=line.Substring(x,1);
else
{
newline +=
Convert.ToChar((int)Encoding.Default.GetBytes(line .Substring(x,1))[0]+10).To
String();
}
}

sw.WriteLine(newline);
}

sr.Close();
sw.Close();
}


Nov 16 '05 #2

P: n/a
JSM <js*@spam.pacific.net.au> wrote:
I am just trying to port an existing simple encryption routine to C#. this
routine simply adds/substracts 10 ascii characters to each character in a
text file (except quotes). The routine for decrypting the file works fine
however when I encrypt the file, several characters are corrupted. when I
looked into it they are always extended ascii characters (eg "x" which is
ascii character 120 gets translated to ascii character 130 which is part of
the extended ascii range of characters). I am assuming this has something to
do with the Encoding which I used to read/write the text files but I just
can't seem to get it to work. I have tried specifying all different types of
encoding (Unicode, Ascii, UTF7/8 without success).


See http://www.pobox.com/~skeet/csharp/unicode.html to explain a few of
your problems - for a start, look at what the page says about "extended
ASCII"...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
JSM
Thanks for your reply Dimitry.

I am still relatively new to C# - would you have an example which
demonstrates what you had said?

Cheers,

John

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:OE**************@TK2MSFTNGP09.phx.gbl...
Hi JSM,

First of all, remember that all strings in .NET are internally Unicode
strings, so in order to get real ASCII codes you should use the GetBytes
method of an Encoding instance configured for the ASCII encoding (as far as I remember there is a static instance accessible as Encoding.Ascii). Now
that you have the bytes with the ASCII codes, you do the encryption and
again, you've got bytes, not characters. Therefore, it seems natural to
write out the resultant bytes in a binary mode where the issue of character encoding simply is out of the picture.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"JSM" <js*@spam.pacific.net.au> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
Hi,

I am just trying to port an existing simple encryption routine to C#. this routine simply adds/substracts 10 ascii characters to each character in a text file (except quotes). The routine for decrypting the file works fine however when I encrypt the file, several characters are corrupted. when I looked into it they are always extended ascii characters (eg "x" which is ascii character 120 gets translated to ascii character 130 which is part
of
the extended ascii range of characters). I am assuming this has something to
do with the Encoding which I used to read/write the text files but I just can't seem to get it to work. I have tried specifying all different types of
encoding (Unicode, Ascii, UTF7/8 without success).

I can't change the encryption method because this needs to work with
existing installations of my application.

Below are the two routines. Any ideas ? I thought this would be a very
simple task but alas I was wrong!

Cheers,

John
--------------------------------------

public void DecryptFile(string SourceFile,string DestFile)
{
string line;
StreamReader sr=new StreamReader(SourceFile,Encoding.Default);
StreamWriter sw=new StreamWriter(DestFile,false,Encoding.Default);

while ((line=sr.ReadLine())!=null)
{
string newline="";

for (int x=0;x<line.Length;x++)
{
if ((line.Substring(x,1)==qte) ||
(line.Substring(x,1)==Convert.ToChar(44).ToString( )))
newline +=line.Substring(x,1);
else
{
newline +=
Convert.ToChar((int)Encoding.Default.GetBytes(line .Substring(x,1))[0]-10).To String();
}
}
sw.WriteLine(newline);
}

sr.Close();
sw.Close();
}
public void EncryptFile(string SourceFile,string DestFile)
{
string line;
StreamReader sr=new StreamReader(SourceFile,Encoding.Default);
StreamWriter sw=new StreamWriter(DestFile,false, Encoding.Default);

while ((line=sr.ReadLine())!=null)
{
string newline="";
for (int x=0;x<line.Length;x++)
{
if ((line.Substring(x,1)==qte) ||
(line.Substring(x,1)==Convert.ToChar(44).ToString( )))
newline +=line.Substring(x,1);
else
{
newline +=
Convert.ToChar((int)Encoding.Default.GetBytes(line .Substring(x,1))[0]+10).To String();
}
}

sw.WriteLine(newline);
}

sr.Close();
sw.Close();
}

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.