473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Writing extended ascii characters to text file.

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
3 24465
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: barthome1 | last post by:
Hello, My company collects data from non-US sources. We are starting projects where this data will be output in an XML document and passed around to our applications and third party tools. ...
1
by: | last post by:
Hey all, Quick question...been bugging me for some time, really. I have a console app, it does some things, and I want to save an array of text to a text file. The text consists of ASCII and...
13
by: bgbauer70 | last post by:
My appologies if this ends up being a duplicate post. For some reason the first post never showed up. I've tried about 300 iterrations of this same ability, and none of them seem to work in...
4
by: wob | last post by:
Many thanks for those who responded to my question of "putting greek char into C string". In searching for an solution, I noticed that there are more than one version of "Extended ASCII...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
2
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
30
by: mellyshum123 | last post by:
I'm wanting to write an int to a file, and so far I have written: const char buff; int num = 256 //for the sake of the example sprintf(buff, "%d", num); Now my question is what to do next. I...
4
by: =?Utf-8?B?Um9zaGFuIFIuRA==?= | last post by:
Hi All, I am new to C# programming; I am developing an application for recording audio data using voice modem. I am using HyperTerminal to manually process audio data. The modem when configured...
13
by: ramif | last post by:
Is there a way to print extended ASCII in C?? I tried to code something, but it only displays strange symbols. here is my code: main() { char chr = 177; //stores the extended ASCII...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.