Mixing text and binary data in the same file.

K

Kai Bohli

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(),"Resources.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.PrinterSettings.PrinterName,memStrm);
sw.Close();
}
}

</snip>
Best wishes
Kai Bohli
(e-mail address removed)
Norway
 
J

John Wood

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.
 
J

John Wood

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.
 
J

Jon Skeet [C# MVP]

John Wood said:
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.
 
K

Kai Bohli

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


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
(e-mail address removed)
Norway
 
J

Jon Skeet [C# MVP]

Kai Bohli said:
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.
 
K

Kai Bohli

Hi Jon

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

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
(e-mail address removed)
Norway
 
J

Jon Skeet [C# MVP]

Kai Bohli said:
Thanks for your reply too. I'll try your suggestions.



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.
 
J

John Wood

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top