473,386 Members | 1,702 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,386 software developers and data experts.

How do I create a one line text file with control codes? e.g.: 144 = 0x90 and 147 = 0x93?

How do I create a one line text file with these control codes? e.g.: 144 =
0x90 and 147 = 0x93?

I am trying to create a one line text file with these characters all one one
row with no spaces.

1. 144 = 0x90
2. 147 = 0x93
3. STX = (^B = 2 = 0x2)
4. NUL = (^@ = 0 = 0x0)
5. DC4 = (^T = 20 = 0x14)
6. SOH = (^A = 1 = 0x1)
7. GS (^] = 29 = 0x1d)
8. RS = (^^ = 30 = 0x1e
I tried StreamWriter, BinaryWriter... but it can't seem to handle the 147 =
0x93 without putting a strange 'A' character.
This is for use with a legacy program and that 'A' character can not be in
the text file.

I am using TextPad 4 to view the control characters.
The code below is close, but for 144 and 147 the strange 'A' character
exists prior to the Control code.

// BUG?: Do not specify encoding 'System.Text.Encoding.UTF8' else 147 will
not work: (even though documentation says UTF8 is default).
StreamWriter sw = new StreamWriter(@"C:\Text2.txt", false);

sw.WriteLine("Encoding: {0}", sw.Encoding.ToString());

sw.Write( (char) 144 );
sw.Write( (char) 147 );
sw.Write( (char) 2 );
sw.Write( (char) 0 );
sw.Write( (char) 20 );
sw.Write( (char) 1 );
sw.Write( (char) 29 );
sw.Write( (char) 30 );

sw.Close();
Nov 16 '05 #1
13 6063
Just curious: if you don't want codepage-translation, why do you use a
TextWriter class?
I think you should use a FileStream.

Niki

"Dan V." <da**@yah.com> wrote in
news:O%****************@tk2msftngp13.phx.gbl...
How do I create a one line text file with these control codes? e.g.: 144 =
0x90 and 147 = 0x93?

I am trying to create a one line text file with these characters all one one row with no spaces.

1. 144 = 0x90
2. 147 = 0x93
3. STX = (^B = 2 = 0x2)
4. NUL = (^@ = 0 = 0x0)
5. DC4 = (^T = 20 = 0x14)
6. SOH = (^A = 1 = 0x1)
7. GS (^] = 29 = 0x1d)
8. RS = (^^ = 30 = 0x1e
I tried StreamWriter, BinaryWriter... but it can't seem to handle the 147 = 0x93 without putting a strange 'A' character.
This is for use with a legacy program and that 'A' character can not be in
the text file.

I am using TextPad 4 to view the control characters.
The code below is close, but for 144 and 147 the strange 'A' character
exists prior to the Control code.

// BUG?: Do not specify encoding 'System.Text.Encoding.UTF8' else 147 will
not work: (even though documentation says UTF8 is default).
StreamWriter sw = new StreamWriter(@"C:\Text2.txt", false);

sw.WriteLine("Encoding: {0}", sw.Encoding.ToString());

sw.Write( (char) 144 );
sw.Write( (char) 147 );
sw.Write( (char) 2 );
sw.Write( (char) 0 );
sw.Write( (char) 20 );
sw.Write( (char) 1 );
sw.Write( (char) 29 );
sw.Write( (char) 30 );

sw.Close();

Nov 16 '05 #2
What is codepage-translation?

Tried FileStream in this way with similar results (I must be missing
something - though I am still using StreamWriter...)

// Create a 'FileStream' object.

FileStream myFileStream = new FileStream(@"C:\Text3.txt", FileMode.Create,
FileAccess.Write);

// Create a 'StreamWriter' to write the data into the file.

StreamWriter myStreamWriter = new StreamWriter(myFileStream,
System.Text.Encoding.UTF8);
myStreamWriter.WriteLine("Encoding: {0}",
myStreamWriter.Encoding.ToString());

myStreamWriter.Write( (char) 144 );

myStreamWriter.Write( (char) 147 );

myStreamWriter.Write( (char) 2 );

myStreamWriter.Write( (char) 0 );

myStreamWriter.Write( (char) 20 );

myStreamWriter.Write( (char) 1 );

myStreamWriter.Write( (char) 29 );

myStreamWriter.Write( (char) 30 );
// Update the 'StreamWriter'.

myStreamWriter.Flush();

// Close the 'StreamWriter' and FileStream.

myStreamWriter.Close();

myFileStream.Close();


"Niki Estner" <ni*********@cube.net> wrote in message
news:uq**************@TK2MSFTNGP12.phx.gbl...
Just curious: if you don't want codepage-translation, why do you use a
TextWriter class?
I think you should use a FileStream.

Niki

"Dan V." <da**@yah.com> wrote in
news:O%****************@tk2msftngp13.phx.gbl...
How do I create a one line text file with these control codes? e.g.: 144 = 0x90 and 147 = 0x93?

I am trying to create a one line text file with these characters all one one
row with no spaces.

1. 144 = 0x90
2. 147 = 0x93
3. STX = (^B = 2 = 0x2)
4. NUL = (^@ = 0 = 0x0)
5. DC4 = (^T = 20 = 0x14)
6. SOH = (^A = 1 = 0x1)
7. GS (^] = 29 = 0x1d)
8. RS = (^^ = 30 = 0x1e
I tried StreamWriter, BinaryWriter... but it can't seem to handle the 147 =
0x93 without putting a strange 'A' character.
This is for use with a legacy program and that 'A' character can not be

in the text file.

I am using TextPad 4 to view the control characters.
The code below is close, but for 144 and 147 the strange 'A' character
exists prior to the Control code.

// BUG?: Do not specify encoding 'System.Text.Encoding.UTF8' else 147 will not work: (even though documentation says UTF8 is default).
StreamWriter sw = new StreamWriter(@"C:\Text2.txt", false);

sw.WriteLine("Encoding: {0}", sw.Encoding.ToString());

sw.Write( (char) 144 );
sw.Write( (char) 147 );
sw.Write( (char) 2 );
sw.Write( (char) 0 );
sw.Write( (char) 20 );
sw.Write( (char) 1 );
sw.Write( (char) 29 );
sw.Write( (char) 30 );

sw.Close();


Nov 16 '05 #3
On Wed, 12 May 2004 17:07:12 -0400, "Dan V." <da**@yah.com> wrote:
<snip>

For straight byte reading/writing, use the FileStream class. Ex:

FileStream fs = new FileStream(@"C:\Text3.txt", FileMode.Create,
FileAccess.Write);

fs.WriteByte(144);
fs.WriteByte(147);
fs.WriteByte(2);
fs.WriteByte(0);
fs.WriteByte(20);
fs.WriteByte(1);
fs.WriteByte(29);
fs.WriteByte(30);
fs.Close();
Nov 16 '05 #4
Thank you it worked beautifully.

How would I write a string using WriteByte?
Like:

fs.WriteByte(0);
fs.Write(@"c:\TextToAdd.txt"); // want to add a control char before and
after this string, but this does not work, not sure how to use all
parameters

fs.WriteByte(29);

"Austin Ehlers" <th**************@hotmail.com> wrote in message
news:as********************************@4ax.com...
On Wed, 12 May 2004 17:07:12 -0400, "Dan V." <da**@yah.com> wrote:
<snip>

For straight byte reading/writing, use the FileStream class. Ex:

FileStream fs = new FileStream(@"C:\Text3.txt", FileMode.Create,
FileAccess.Write);

fs.WriteByte(144);
fs.WriteByte(147);
fs.WriteByte(2);
fs.WriteByte(0);
fs.WriteByte(20);
fs.WriteByte(1);
fs.WriteByte(29);
fs.WriteByte(30);
fs.Close();

Nov 16 '05 #5
Dan V. <da**@yah.com> wrote:
Thank you it worked beautifully.

How would I write a string using WriteByte?
Like:

fs.WriteByte(0);
fs.Write(@"c:\TextToAdd.txt"); // want to add a control char before and
after this string, but this does not work, not sure how to use all
parameters

fs.WriteByte(29);


Rather than using WriteByte, you'd probably just use Write that takes a
byte array, after converting the string to a byte array using the
appropriate Encoding.

See http://www.pobox.com/~skeet/csharp/unicode.html for more info about
encodings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Thanks again, I read your article and this one:
http://www.joelonsoftware.com/articles/Unicode.html

Very helpful.

This code was what I needed.
s = (@"c:\TextToAdd.txt");
fs.Write(System.Text.Encoding.UTF8.GetBytes(s), 0, s.Length);

This page got me on the write track also:

http://www.ondotnet.com/pub/a/dotnet...rpckbk_chap01/


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dan V. <da**@yah.com> wrote:
Thank you it worked beautifully.

How would I write a string using WriteByte?
Like:

fs.WriteByte(0);
fs.Write(@"c:\TextToAdd.txt"); // want to add a control char before and after this string, but this does not work, not sure how to use all
parameters

fs.WriteByte(29);


Rather than using WriteByte, you'd probably just use Write that takes a
byte array, after converting the string to a byte array using the
appropriate Encoding.

See http://www.pobox.com/~skeet/csharp/unicode.html for more info about
encodings.

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

Nov 16 '05 #7
Dan V. <da**@yah.com> wrote:
Thanks again, I read your article and this one:
http://www.joelonsoftware.com/articles/Unicode.html

Very helpful.
Goodo.
This code was what I needed.
s = (@"c:\TextToAdd.txt");

fs.Write(System.Text.Encoding.UTF8.GetBytes(s), 0, s.Length);
That's not quite the code you need. You'll end up writing s.Length
bytes, even though GetBytes may well have returned more bytes than
that.

Use

byte[] bytes = Encoding.UTF8.GetBytes(s);
fs.Write(bytes, 0, bytes.Length);
This page got me on the write track also:

http://www.ondotnet.com/pub/a/dotnet...rpckbk_chap01/


Can't say I like that page much, given the code it espouses. There's no
need to create a new instance of UnicodeEncoding etc each time. Why
bother with a whole extra method in the first place when you can just
do:

string x = Encoding.Unicode.GetString(bytes);

rather than

string x = FromUnicodeByteArray(bytes);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
You must be thinking - "did he really understand those articles..."
Is this the point then?

string.Length may not equal bytes.length because:
1) some characters may not equal exactly one byte (even though in my case
it did)
2) some characters (if I used non 'English' ones may be more than one
byte) - I am recalling that in UTF8 ANSI and ASCII, they use only one byte
only for most 'English' characters and 2 or more bytes for the rest...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dan V. <da**@yah.com> wrote:
Thanks again, I read your article and this one:
http://www.joelonsoftware.com/articles/Unicode.html

Very helpful.


Goodo.
This code was what I needed.
s = (@"c:\TextToAdd.txt");

fs.Write(System.Text.Encoding.UTF8.GetBytes(s), 0, s.Length);


That's not quite the code you need. You'll end up writing s.Length
bytes, even though GetBytes may well have returned more bytes than
that.

Use

byte[] bytes = Encoding.UTF8.GetBytes(s);
fs.Write(bytes, 0, bytes.Length);
This page got me on the write track also:

http://www.ondotnet.com/pub/a/dotnet...rpckbk_chap01/


Can't say I like that page much, given the code it espouses. There's no
need to create a new instance of UnicodeEncoding etc each time. Why
bother with a whole extra method in the first place when you can just
do:

string x = Encoding.Unicode.GetString(bytes);

rather than

string x = FromUnicodeByteArray(bytes);

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

Nov 16 '05 #9
Dan V. <da**@yah.com> wrote:
You must be thinking - "did he really understand those articles..."
Only a *little* bit :)
Is this the point then?

string.Length may not equal bytes.length because:
1) some characters may not equal exactly one byte (even though in my case
it did)
Indeed. It will depend on the encoding and the characters being
encoded. For instance, using Encoding.Unicode you will always get twice
as many bytes as characters. Using Encoding.ASCII you'll always get
exactly the same number of bytes as characters (but you can only
properly encode characters 0-127). Using Encoding.UTF8 you'll get a
variable number depending on the character - ASCII values still end up
as one byte, but the number of bytes grows depending on the characters.
In fact, UTF-8 is even more complicated, because surrogate pairs should
be encoded into a single 6-byte UTF-8 sequence, rather than two 4-byte
sequences. Nasty stuff!
2) some characters (if I used non 'English' ones may be more than one
byte) - I am recalling that in UTF8 ANSI and ASCII, they use only one byte
only for most 'English' characters and 2 or more bytes for the rest...


"ANSI" isn't a single character set - but UTF8 and ASCII are certainly
one byte per character for all ASCII characters.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
Now I am trying to get rid of that '@' symbol from a string, which I use
previously.
I don't want to write that '@' symbol to the file and WriteByte seems to try
to do this.

s = @"c:\text1.txt"

bytes = Encoding.UTF8.GetBytes(s);

fs.Write(bytes, 0, bytes.Length);
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dan V. <da**@yah.com> wrote:
You must be thinking - "did he really understand those articles..."


Only a *little* bit :)
Is this the point then?

string.Length may not equal bytes.length because:
1) some characters may not equal exactly one byte (even though in my case it did)


Indeed. It will depend on the encoding and the characters being
encoded. For instance, using Encoding.Unicode you will always get twice
as many bytes as characters. Using Encoding.ASCII you'll always get
exactly the same number of bytes as characters (but you can only
properly encode characters 0-127). Using Encoding.UTF8 you'll get a
variable number depending on the character - ASCII values still end up
as one byte, but the number of bytes grows depending on the characters.
In fact, UTF-8 is even more complicated, because surrogate pairs should
be encoded into a single 6-byte UTF-8 sequence, rather than two 4-byte
sequences. Nasty stuff!
2) some characters (if I used non 'English' ones may be more than one
byte) - I am recalling that in UTF8 ANSI and ASCII, they use only one byte only for most 'English' characters and 2 or more bytes for the rest...


"ANSI" isn't a single character set - but UTF8 and ASCII are certainly
one byte per character for all ASCII characters.

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

Nov 16 '05 #11
Never mind,
the problem was else where, WriteByte can handle it.

"Dan V." <da**@yah.com> wrote in message
news:eG**************@TK2MSFTNGP11.phx.gbl...
Now I am trying to get rid of that '@' symbol from a string, which I use
previously.
I don't want to write that '@' symbol to the file and WriteByte seems to try to do this.

s = @"c:\text1.txt"

bytes = Encoding.UTF8.GetBytes(s);

fs.Write(bytes, 0, bytes.Length);
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dan V. <da**@yah.com> wrote:
You must be thinking - "did he really understand those articles..."
Only a *little* bit :)
Is this the point then?

string.Length may not equal bytes.length because:
1) some characters may not equal exactly one byte (even though in my case it did)


Indeed. It will depend on the encoding and the characters being
encoded. For instance, using Encoding.Unicode you will always get twice
as many bytes as characters. Using Encoding.ASCII you'll always get
exactly the same number of bytes as characters (but you can only
properly encode characters 0-127). Using Encoding.UTF8 you'll get a
variable number depending on the character - ASCII values still end up
as one byte, but the number of bytes grows depending on the characters.
In fact, UTF-8 is even more complicated, because surrogate pairs should
be encoded into a single 6-byte UTF-8 sequence, rather than two 4-byte
sequences. Nasty stuff!
2) some characters (if I used non 'English' ones may be more than one byte) - I am recalling that in UTF8 ANSI and ASCII, they use only
one byte only for most 'English' characters and 2 or more bytes for the rest...


"ANSI" isn't a single character set - but UTF8 and ASCII are certainly
one byte per character for all ASCII characters.

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


Nov 16 '05 #12
Dan V. <da**@yah.com> wrote:
Never mind,
the problem was else where, WriteByte can handle it.


That's because the @ isn't in the string at all - it just means it's a
verbatim string literal.

See http://www.pobox.com/~skeet/csharp/f...batim.literals

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
Sorry this was my problem. It works as advertised.

"Dan V." <da**@yah.com> wrote in message
news:eG**************@TK2MSFTNGP11.phx.gbl...
Now I am trying to get rid of that '@' symbol from a string, which I use
previously.
I don't want to write that '@' symbol to the file and WriteByte seems to try to do this.

s = @"c:\text1.txt"

bytes = Encoding.UTF8.GetBytes(s);

fs.Write(bytes, 0, bytes.Length);
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dan V. <da**@yah.com> wrote:
You must be thinking - "did he really understand those articles..."
Only a *little* bit :)
Is this the point then?

string.Length may not equal bytes.length because:
1) some characters may not equal exactly one byte (even though in my case it did)


Indeed. It will depend on the encoding and the characters being
encoded. For instance, using Encoding.Unicode you will always get twice
as many bytes as characters. Using Encoding.ASCII you'll always get
exactly the same number of bytes as characters (but you can only
properly encode characters 0-127). Using Encoding.UTF8 you'll get a
variable number depending on the character - ASCII values still end up
as one byte, but the number of bytes grows depending on the characters.
In fact, UTF-8 is even more complicated, because surrogate pairs should
be encoded into a single 6-byte UTF-8 sequence, rather than two 4-byte
sequences. Nasty stuff!
2) some characters (if I used non 'English' ones may be more than one byte) - I am recalling that in UTF8 ANSI and ASCII, they use only
one byte only for most 'English' characters and 2 or more bytes for the rest...


"ANSI" isn't a single character set - but UTF8 and ASCII are certainly
one byte per character for all ASCII characters.

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


Nov 16 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Viviana Vc | last post by:
How can I programatically do the equivalent of the following: cacls "C:\Program Files\test" /T /G Everyone:f ? Thanks, Viv
7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
8
by: Dong Ge | last post by:
Hi! I am a beginner of C. I want to delete some characters or a whole line in a text file. I have tried the "fprintf", "fputs", "fwrite", but no one of them can run rightly. In the below...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
3
by: Atara | last post by:
I use the following code to load xml file: Dim srcXml As String = pathDataFiles & "test.xml" Dim XmlDoc As New Xml.XmlDocument XmlDoc.Load(srcXml) If my xml file starts with the line - ...
3
by: vtashore | last post by:
I downloaded Steve Leban's RTF2 control and it works as advertised. Good news! After reading reference material on the RTF standard codes, I have been able to write update queries to universally...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
11
by: Mark B | last post by:
I want to display a pre-designed graphical 'performance badge' on certain webpages (round, about 2cm diameter) next to a salesperson's details. I have a function,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.