Convert Int to 2 Byte Array C# .Net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • carlospedr@gmail.com

    Convert Int to 2 Byte Array C# .Net

    I'm sending ESC sequences to a printer, one of these sequences include
    a parameter that is "2 byte integer", in order to send it to the
    printer I have to convert an int to 2 byte integer, how do I do this?
    Thank's for the help,
    Carlos Pedro

  • Andrew Robinson

    #2
    Re: Convert Int to 2 Byte Array C# .Net

    Int16 myTwoByteIntege r = 123;

    --

    Andrew Robinson


    <carlospedr@gma il.comwrote in message
    news:1154450375 .426478.322130@ h48g2000cwc.goo glegroups.com.. .
    I'm sending ESC sequences to a printer, one of these sequences include
    a parameter that is "2 byte integer", in order to send it to the
    printer I have to convert an int to 2 byte integer, how do I do this?
    Thank's for the help,
    Carlos Pedro
    >

    Comment

    • Adam Clauss

      #3
      Re: Convert Int to 2 Byte Array C# .Net

      Use a short? (System.Int16)

      --
      Adam Clauss

      <carlospedr@gma il.comwrote in message
      news:1154450375 .426478.322130@ h48g2000cwc.goo glegroups.com.. .
      I'm sending ESC sequences to a printer, one of these sequences include
      a parameter that is "2 byte integer", in order to send it to the
      printer I have to convert an int to 2 byte integer, how do I do this?
      Thank's for the help,
      Carlos Pedro
      >

      Comment

      • carlospedr@gmail.com

        #4
        Re: Convert Int to 2 Byte Array C# .Net

        Thank you very much, you should've called me stupid...

        I wanted a 2 Byte Array not a 2 Byte int, however... the
        BitConverter.Ge tBytes(i) function does it...

        Thank you...
        Andrew Robinson wrote:
        Int16 myTwoByteIntege r = 123;
        >
        --
        >
        Andrew Robinson
        >
        >
        <carlospedr@gma il.comwrote in message
        news:1154450375 .426478.322130@ h48g2000cwc.goo glegroups.com.. .
        I'm sending ESC sequences to a printer, one of these sequences include
        a parameter that is "2 byte integer", in order to send it to the
        printer I have to convert an int to 2 byte integer, how do I do this?
        Thank's for the help,
        Carlos Pedro

        Comment

        • Ignacio Machin \( .NET/ C# MVP \)

          #5
          Re: Convert Int to 2 Byte Array C# .Net

          Hi,

          You can use BitConverter.Ge tBytes( yourInt )

          How are you sending it? maybe you do not need to separate it in bytes


          --
          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation

          <carlospedr@gma il.comwrote in message
          news:1154450375 .426478.322130@ h48g2000cwc.goo glegroups.com.. .
          I'm sending ESC sequences to a printer, one of these sequences include
          a parameter that is "2 byte integer", in order to send it to the
          printer I have to convert an int to 2 byte integer, how do I do this?
          Thank's for the help,
          Carlos Pedro
          >

          Comment

          • Göran Andersson

            #6
            Re: Convert Int to 2 Byte Array C# .Net

            You mean that you need to split a 16 bit number into two bytes?

            ushort num = 42;
            byte hi = (byte)(num >8);
            byte lo = (byte)(num & 255);

            carlospedr@gmai l.com wrote:
            I'm sending ESC sequences to a printer, one of these sequences include
            a parameter that is "2 byte integer", in order to send it to the
            printer I have to convert an int to 2 byte integer, how do I do this?
            Thank's for the help,
            Carlos Pedro
            >

            Comment

            • carlospedr@gmail.com

              #7
              Re: Convert Int to 2 Byte Array C# .Net

              Yes, but I've sorted it out, there is a a function in the .Net
              Framework that does that.

              Thank's any way.

              Göran Andersson wrote:
              You mean that you need to split a 16 bit number into two bytes?
              >
              ushort num = 42;
              byte hi = (byte)(num >8);
              byte lo = (byte)(num & 255);
              >
              carlospedr@gmai l.com wrote:
              I'm sending ESC sequences to a printer, one of these sequences include
              a parameter that is "2 byte integer", in order to send it to the
              printer I have to convert an int to 2 byte integer, how do I do this?
              Thank's for the help,
              Carlos Pedro

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Convert Int to 2 Byte Array C# .Net

                <carlospedr@gma il.comwrote:
                Yes, but I've sorted it out, there is a a function in the .Net
                Framework that does that.
                Have a look at BitConverter.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                Working...