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

Mixing text and binary data in the same file.

Hi all !

I've come across a huge problem (for me at least).

I'm trying to send some initial graphics to a labelprinter. To do this, I load the graphics from
resource and send it directly to the printerport along with "printer instructions".

The problem is that the printer instruction have to be "plain text" while the image has to be
binary. Something like this:

....
....
02D
02ICPCOD

<Binary data goes here>

More plain text goes here
.....
.....

If I use StreamWriter, the everything is plain text, and if I use BinaryWriter everythings is
binary. Any help are greatly appreciated.

TIA
Kai Bohli
Norway

Code below:
<snip>
private void btnInitDatamax_Click(object sender, System.EventArgs e)
{
Assembly assem = this.GetType().Assembly;
Stream F9Stream =
assem.GetManifestResourceStream(this.GetType(),"Re sources.SvPost.Foretak9.bmp");
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
MemoryStream memStrm = new MemoryStream();
StreamWriter sw = new StreamWriter(memStrm);
sw.WriteLine("\x02n");
sw.WriteLine("\x02M1891");
sw.WriteLine("\x02O0220");
sw.WriteLine("\x02O0220");
sw.WriteLine("\x02SG");
sw.WriteLine("\x02d");
sw.WriteLine("\x02c0757");
sw.WriteLine("\x02D");
sw.WriteLine("\x02ICPForetak9");
sw.WriteLine("");
sw.Flush();
// append the image stream
long length = F9Stream.Length;
byte [] bytes = new byte[length];
//sw.Write(bytes,0,(int)length);
//bw.Write(pictureBox1.Image);
//bw.Write(Foretak9);
//bw.Write()
sw.WriteLine("\x02L");
sw.WriteLine("D11");
sw.WriteLine("PG");
sw.WriteLine("pG");
sw.WriteLine("SG");
sw.WriteLine("A2");
sw.WriteLine("1Y1100005890137Foretak9");
sw.WriteLine("Q0001");
sw.WriteLine("E");
sw.WriteLine("");
sw.Flush();
memStrm.Position = 0;
LabelPrintHelper.SendDocToPrinter(pd.PrinterSettin gs.PrinterName,memStrm);
sw.Close();
}
}

</snip>
Best wishes
Kai Bohli
ka***********@online.no
Norway
Nov 16 '05 #1
8 3452
You can use BinaryWriter, and an Encoding to convert text to a byte array,
and output the byte array using one of the BinaryWriter's Write overloads.

You need to find out which encoding the device uses. You have choices of
Encoding.ASCII, Encoding.Unicode, Encoding.BigEndianUnicode, Encoding.UTF7
or Encoding.UTF8.

All these encoding objects provide a GetBytes method that convert a text
string to a byte array representation that can then be fed into the strean
using the BinaryWriter.

Hope that helps.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Kai Bohli" <ka****@online.nospam> wrote in message
news:1h********************************@4ax.com...
Hi all !

I've come across a huge problem (for me at least).

I'm trying to send some initial graphics to a labelprinter. To do this, I load the graphics from resource and send it directly to the printerport along with "printer instructions".
The problem is that the printer instruction have to be "plain text" while the image has to be binary. Something like this:

...
...
02D
02ICPCOD

<Binary data goes here>

More plain text goes here
....
....

If I use StreamWriter, the everything is plain text, and if I use BinaryWriter everythings is binary. Any help are greatly appreciated.

TIA
Kai Bohli
Norway

Code below:
<snip>
private void btnInitDatamax_Click(object sender, System.EventArgs e)
{
Assembly assem = this.GetType().Assembly;
Stream F9Stream =
assem.GetManifestResourceStream(this.GetType(),"Re sources.SvPost.Foretak9.bm
p"); PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
MemoryStream memStrm = new MemoryStream();
StreamWriter sw = new StreamWriter(memStrm);
sw.WriteLine("\x02n");
sw.WriteLine("\x02M1891");
sw.WriteLine("\x02O0220");
sw.WriteLine("\x02O0220");
sw.WriteLine("\x02SG");
sw.WriteLine("\x02d");
sw.WriteLine("\x02c0757");
sw.WriteLine("\x02D");
sw.WriteLine("\x02ICPForetak9");
sw.WriteLine("");
sw.Flush();
// append the image stream
long length = F9Stream.Length;
byte [] bytes = new byte[length];
//sw.Write(bytes,0,(int)length);
//bw.Write(pictureBox1.Image);
//bw.Write(Foretak9);
//bw.Write()
sw.WriteLine("\x02L");
sw.WriteLine("D11");
sw.WriteLine("PG");
sw.WriteLine("pG");
sw.WriteLine("SG");
sw.WriteLine("A2");
sw.WriteLine("1Y1100005890137Foretak9");
sw.WriteLine("Q0001");
sw.WriteLine("E");
sw.WriteLine("");
sw.Flush();
memStrm.Position = 0;
LabelPrintHelper.SendDocToPrinter(pd.PrinterSettin gs.PrinterName,memStrm);
sw.Close();
}
}

</snip>
Best wishes
Kai Bohli
ka***********@online.no
Norway

Nov 16 '05 #2
Also keep in mind that BinaryWriter has a Write(string) method, so if you
don't mind using the current encoding, then that might be a simpler option.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"Kai Bohli" <ka****@online.nospam> wrote in message
news:1h********************************@4ax.com...
Hi all !

I've come across a huge problem (for me at least).

I'm trying to send some initial graphics to a labelprinter. To do this, I load the graphics from resource and send it directly to the printerport along with "printer instructions".
The problem is that the printer instruction have to be "plain text" while the image has to be binary. Something like this:

...
...
02D
02ICPCOD

<Binary data goes here>

More plain text goes here
....
....

If I use StreamWriter, the everything is plain text, and if I use BinaryWriter everythings is binary. Any help are greatly appreciated.

TIA
Kai Bohli
Norway

Code below:
<snip>
private void btnInitDatamax_Click(object sender, System.EventArgs e)
{
Assembly assem = this.GetType().Assembly;
Stream F9Stream =
assem.GetManifestResourceStream(this.GetType(),"Re sources.SvPost.Foretak9.bm
p"); PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
MemoryStream memStrm = new MemoryStream();
StreamWriter sw = new StreamWriter(memStrm);
sw.WriteLine("\x02n");
sw.WriteLine("\x02M1891");
sw.WriteLine("\x02O0220");
sw.WriteLine("\x02O0220");
sw.WriteLine("\x02SG");
sw.WriteLine("\x02d");
sw.WriteLine("\x02c0757");
sw.WriteLine("\x02D");
sw.WriteLine("\x02ICPForetak9");
sw.WriteLine("");
sw.Flush();
// append the image stream
long length = F9Stream.Length;
byte [] bytes = new byte[length];
//sw.Write(bytes,0,(int)length);
//bw.Write(pictureBox1.Image);
//bw.Write(Foretak9);
//bw.Write()
sw.WriteLine("\x02L");
sw.WriteLine("D11");
sw.WriteLine("PG");
sw.WriteLine("pG");
sw.WriteLine("SG");
sw.WriteLine("A2");
sw.WriteLine("1Y1100005890137Foretak9");
sw.WriteLine("Q0001");
sw.WriteLine("E");
sw.WriteLine("");
sw.Flush();
memStrm.Position = 0;
LabelPrintHelper.SendDocToPrinter(pd.PrinterSettin gs.PrinterName,memStrm);
sw.Close();
}
}

</snip>
Best wishes
Kai Bohli
ka***********@online.no
Norway

Nov 16 '05 #3
John Wood <j@ro.com> wrote:
Also keep in mind that BinaryWriter has a Write(string) method, so if you
don't mind using the current encoding, then that might be a simpler option.


Note that that prefixes the string with an encoded length, which may
not be what's wanted.

Personally I'd just use BinaryWriter.Write(byte[]), passing in
Encoding.XXX.GetBytes(text) where XXX is the appropriate encoding.

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

Thanks for your excellent reply. It works great using the BinaryWriter like this:

ASCIIEncoding AE = new ASCIIEncoding();
byte [] InitArray =
AE.GetBytes("\x02n\n\x02M1891\n\x02O0220\n\x02V0\n \x02SG\n\x02d\n\x02c0757\n\x02D\n");
bw.Write(InitArray);

I have any a small problem with this stuff: "\x02D\n"; which should return ^D but the result is
just "-". The same result goes for "\x02d\n. A shame though, cause this code tells the labelprinter
to expect graphics :)

Any thoughts ?

TIA
On Mon, 28 Jun 2004 22:42:06 -0400, "John Wood" <j@ro.com> wrote:
You can use BinaryWriter, and an Encoding to convert text to a byte array,
and output the byte array using one of the BinaryWriter's Write overloads.


Best wishes
Kai Bohli
ka***********@online.no
Norway
Nov 16 '05 #5
Kai Bohli <ka****@online.nospam> wrote:
Thanks for your excellent reply. It works great using the BinaryWriter like this:

ASCIIEncoding AE = new ASCIIEncoding();
I'd suggest using just Encoding.ASCII rather than creating a new
encoding instance.
byte [] InitArray =
AE.GetBytes("\x02n\n\x02M1891\n\x02O0220\n\x02V0\n \x02SG\n\x02d\n\x02
c0757\n\x02D\n");
bw.Write(InitArray);

I have any a small problem with this stuff: "\x02D\n"; which should
return ^D but the result is just "-". The same result goes for
"\x02d\n. A shame though, cause this code tells the labelprinter to
expect graphics :)

Any thoughts ?


Not sure what you mean exactly by ^D. Do you mean '^' 'D' or "control-
D". If the latter, what character do you expect it to be? "\x02d" is
'-'.

I would actually suggest writing the control codes out as a byte array
- it's not really text at that stage.

--
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
Hi Jon

Thanks for your reply too. I'll try your suggestions.

On Tue, 29 Jun 2004 11:08:04 +0100, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Not sure what you mean exactly by ^D. Do you mean '^' 'D' or "control-
D". If the latter, what character do you expect it to be? "\x02d" is
'-'.


I was expection "Control-D". The printer need this value to be HEX value 02
Best wishes
Kai Bohli
ka***********@online.no
Norway
Nov 16 '05 #7
Kai Bohli <ka****@online.nospam> wrote:
Thanks for your reply too. I'll try your suggestions.

On Tue, 29 Jun 2004 11:08:04 +0100, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Not sure what you mean exactly by ^D. Do you mean '^' 'D' or "control-
D". If the latter, what character do you expect it to be? "\x02d" is
'-'.


I was expection "Control-D". The printer need this value to be HEX value 02


I'd just write the byte 2 then. Alternatively, use \u0002 to get
character 2 in your string.

--
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
If the printer expects a hex ascii value of 02, you should just use
binaryWriter.Write(byte) to output that. You should only really use
Encoding.ASCII for writing the text part, just continue to use
binaryWriter.Write overloads for the binary data (which control codes are,
effectively).

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Kai Bohli" <ka****@online.nospam> wrote in message
news:3n********************************@4ax.com...
Hi Jon

Thanks for your reply too. I'll try your suggestions.

On Tue, 29 Jun 2004 11:08:04 +0100, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Not sure what you mean exactly by ^D. Do you mean '^' 'D' or "control-
D". If the latter, what character do you expect it to be? "\x02d" is
'-'.
I was expection "Control-D". The printer need this value to be HEX value

02

Best wishes
Kai Bohli
ka***********@online.no
Norway

Nov 16 '05 #9

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

Similar topics

0
by: Erik Max Francis | last post by:
Is there any prohibition against mixing different protocols within the same pickle? I don't see anything about this in the Python Library Reference and, after all, the pickle.dump function takes a...
27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
36
by: Wei Su | last post by:
Hi, I have a text file abc.txt and it looks like: 12 34 56 23 45 56 33 56 78 ... .. .. ... .. .. I want to get how many rows totally in the text file, how to do this? Thanks.
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
7
by: Ubantu Rococo | last post by:
Hi all, Sorry for this stupid question, but I am having trouble mixing imagecopy etc. with HTML. What I am trying to do is copy an image, and then obtain co-ordinates from a database which will...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.