Mixing text and binary data in the same file.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kai Bohli

    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.EventArg s e)
    {
    Assembly assem = this.GetType(). Assembly;
    Stream F9Stream =
    assem.GetManife stResourceStrea m(this.GetType( ),"Resources.Sv Post.Foretak9.b mp");
    PrintDialog pd = new PrintDialog();
    pd.PrinterSetti ngs = new PrinterSettings ();
    if (DialogResult.O K == pd.ShowDialog(t his))
    {
    MemoryStream memStrm = new MemoryStream();
    StreamWriter sw = new StreamWriter(me mStrm);
    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(pictur eBox1.Image);
    //bw.Write(Foreta k9);
    //bw.Write()
    sw.WriteLine("\ x02L");
    sw.WriteLine("D 11");
    sw.WriteLine("P G");
    sw.WriteLine("p G");
    sw.WriteLine("S G");
    sw.WriteLine("A 2");
    sw.WriteLine("1 Y1100005890137F oretak9");
    sw.WriteLine("Q 0001");
    sw.WriteLine("E ");
    sw.WriteLine("" );
    sw.Flush();
    memStrm.Positio n = 0;
    LabelPrintHelpe r.SendDocToPrin ter(pd.PrinterS ettings.Printer Name,memStrm);
    sw.Close();
    }
    }

    </snip>
    Best wishes
    Kai Bohli
    kaiboeNO_SPAM@o nline.no
    Norway
  • John Wood

    #2
    Re: Mixing text and binary data in the same file.

    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.Unicod e, Encoding.BigEnd ianUnicode, 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" <kaiboe@online. nospam> wrote in message
    news:1ha1e0pt26 t2ldv94b6fmfh5p 28ro0pjln@4ax.c om...[color=blue]
    > 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[/color]
    load the graphics from[color=blue]
    > resource and send it directly to the printerport along with "printer[/color]
    instructions".[color=blue]
    >
    > The problem is that the printer instruction have to be "plain text" while[/color]
    the image has to be[color=blue]
    > 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[/color]
    BinaryWriter everythings is[color=blue]
    > binary. Any help are greatly appreciated.
    >
    > TIA
    > Kai Bohli
    > Norway
    >
    > Code below:
    > <snip>
    > private void btnInitDatamax_ Click(object sender, System.EventArg s e)
    > {
    > Assembly assem = this.GetType(). Assembly;
    > Stream F9Stream =
    >[/color]
    assem.GetManife stResourceStrea m(this.GetType( ),"Resources.Sv Post.Foretak9.b m
    p");[color=blue]
    > PrintDialog pd = new PrintDialog();
    > pd.PrinterSetti ngs = new PrinterSettings ();
    > if (DialogResult.O K == pd.ShowDialog(t his))
    > {
    > MemoryStream memStrm = new MemoryStream();
    > StreamWriter sw = new StreamWriter(me mStrm);
    > 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(pictur eBox1.Image);
    > //bw.Write(Foreta k9);
    > //bw.Write()
    > sw.WriteLine("\ x02L");
    > sw.WriteLine("D 11");
    > sw.WriteLine("P G");
    > sw.WriteLine("p G");
    > sw.WriteLine("S G");
    > sw.WriteLine("A 2");
    > sw.WriteLine("1 Y1100005890137F oretak9");
    > sw.WriteLine("Q 0001");
    > sw.WriteLine("E ");
    > sw.WriteLine("" );
    > sw.Flush();
    > memStrm.Positio n = 0;
    > LabelPrintHelpe r.SendDocToPrin ter(pd.PrinterS ettings.Printer Name,memStrm);
    > sw.Close();
    > }
    > }
    >
    > </snip>
    > Best wishes
    > Kai Bohli
    > kaiboeNO_SPAM@o nline.no
    > Norway[/color]


    Comment

    • John Wood

      #3
      Re: Mixing text and binary data in the same file.

      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" <kaiboe@online. nospam> wrote in message
      news:1ha1e0pt26 t2ldv94b6fmfh5p 28ro0pjln@4ax.c om...[color=blue]
      > 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[/color]
      load the graphics from[color=blue]
      > resource and send it directly to the printerport along with "printer[/color]
      instructions".[color=blue]
      >
      > The problem is that the printer instruction have to be "plain text" while[/color]
      the image has to be[color=blue]
      > 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[/color]
      BinaryWriter everythings is[color=blue]
      > binary. Any help are greatly appreciated.
      >
      > TIA
      > Kai Bohli
      > Norway
      >
      > Code below:
      > <snip>
      > private void btnInitDatamax_ Click(object sender, System.EventArg s e)
      > {
      > Assembly assem = this.GetType(). Assembly;
      > Stream F9Stream =
      >[/color]
      assem.GetManife stResourceStrea m(this.GetType( ),"Resources.Sv Post.Foretak9.b m
      p");[color=blue]
      > PrintDialog pd = new PrintDialog();
      > pd.PrinterSetti ngs = new PrinterSettings ();
      > if (DialogResult.O K == pd.ShowDialog(t his))
      > {
      > MemoryStream memStrm = new MemoryStream();
      > StreamWriter sw = new StreamWriter(me mStrm);
      > 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(pictur eBox1.Image);
      > //bw.Write(Foreta k9);
      > //bw.Write()
      > sw.WriteLine("\ x02L");
      > sw.WriteLine("D 11");
      > sw.WriteLine("P G");
      > sw.WriteLine("p G");
      > sw.WriteLine("S G");
      > sw.WriteLine("A 2");
      > sw.WriteLine("1 Y1100005890137F oretak9");
      > sw.WriteLine("Q 0001");
      > sw.WriteLine("E ");
      > sw.WriteLine("" );
      > sw.Flush();
      > memStrm.Positio n = 0;
      > LabelPrintHelpe r.SendDocToPrin ter(pd.PrinterS ettings.Printer Name,memStrm);
      > sw.Close();
      > }
      > }
      >
      > </snip>
      > Best wishes
      > Kai Bohli
      > kaiboeNO_SPAM@o nline.no
      > Norway[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Mixing text and binary data in the same file.

        John Wood <j@ro.com> wrote:[color=blue]
        > 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.[/color]

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

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

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Kai Bohli

          #5
          Re: Mixing text and binary data in the same file.

          Hi John !

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

          ASCIIEncoding AE = new ASCIIEncoding() ;
          byte [] InitArray =
          AE.GetBytes("\x 02n\n\x02M1891\ n\x02O0220\n\x0 2V0\n\x02SG\n\x 02d\n\x02c0757\ n\x02D\n");
          bw.Write(InitAr ray);

          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:
          [color=blue]
          >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.[/color]

          Best wishes
          Kai Bohli
          kaiboeNO_SPAM@o nline.no
          Norway

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Mixing text and binary data in the same file.

            Kai Bohli <kaiboe@online. nospam> wrote:[color=blue]
            > Thanks for your excellent reply. It works great using the BinaryWriter like this:
            >
            > ASCIIEncoding AE = new ASCIIEncoding() ;[/color]

            I'd suggest using just Encoding.ASCII rather than creating a new
            encoding instance.
            [color=blue]
            > byte [] InitArray =
            > AE.GetBytes("\x 02n\n\x02M1891\ n\x02O0220\n\x0 2V0\n\x02SG\n\x 02d\n\x02
            > c0757\n\x02D\n" );
            > bw.Write(InitAr ray);
            >
            > 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 ?[/color]

            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 - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Kai Bohli

              #7
              Re: Mixing text and binary data in the same file.

              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] <skeet@pobox.co m> wrote:
              [color=blue]
              >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
              >'-'.[/color]

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


              Best wishes
              Kai Bohli
              kaiboeNO_SPAM@o nline.no
              Norway

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Mixing text and binary data in the same file.

                Kai Bohli <kaiboe@online. nospam> wrote:[color=blue]
                > Thanks for your reply too. I'll try your suggestions.
                >
                > On Tue, 29 Jun 2004 11:08:04 +0100, Jon Skeet [C# MVP] <skeet@pobox.co m> wrote:
                >[color=green]
                > >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
                > >'-'.[/color]
                >
                > I was expection "Control-D". The printer need this value to be HEX value 02[/color]

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

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • John Wood

                  #9
                  Re: Mixing text and binary data in the same file.

                  If the printer expects a hex ascii value of 02, you should just use
                  binaryWriter.Wr ite(byte) to output that. You should only really use
                  Encoding.ASCII for writing the text part, just continue to use
                  binaryWriter.Wr ite overloads for the binary data (which control codes are,
                  effectively).

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

                  "Kai Bohli" <kaiboe@online. nospam> wrote in message
                  news:3nj2e0t6ht q682b0qv2sf24av 88sc6ih93@4ax.c om...[color=blue]
                  > 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] <skeet@pobox.co m>[/color]
                  wrote:[color=blue]
                  >[color=green]
                  > >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
                  > >'-'.[/color]
                  >
                  > I was expection "Control-D". The printer need this value to be HEX value[/color]
                  02[color=blue]
                  >
                  >
                  > Best wishes
                  > Kai Bohli
                  > kaiboeNO_SPAM@o nline.no
                  > Norway[/color]


                  Comment

                  Working...