short[] to byte[] conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alberto Cardoso

    short[] to byte[] conversion

    Is there a direct way to convert a short array to a byte array?
    I dont to use a for and cast every short to a byte.

    I want something like the BitConverter class that accpets a short array as
    argument.

    Bitconverter.Ge tBytes( short[] );

    --
    Alberto Cardoso

  • Ciaran O''Donnell

    #2
    RE: short[] to byte[] conversion

    I dont think there is. You will need to loop through them.

    Ciaran O'Donnell

    "Alberto Cardoso" wrote:
    Is there a direct way to convert a short array to a byte array?
    I dont to use a for and cast every short to a byte.
    >
    I want something like the BitConverter class that accpets a short array as
    argument.
    >
    Bitconverter.Ge tBytes( short[] );
    >
    --
    Alberto Cardoso
    >

    Comment

    • Mattias Sjögren

      #3
      Re: short[] to byte[] conversion

      Alberto,
      >Is there a direct way to convert a short array to a byte array?
      Depends on what kind of behaviour you want.

      If you want to make a straight memory copy so that each short results
      in two bytes, you can use the System.Buffer.B lockCopy method.

      If you want just one byte for each short (assuming the value is in
      range), you basically have to use a loop. Either write an explcit loop
      or, if you're on .NET 2.0, use a method such as
      Array.ConvertAl l<short, byte>().


      Mattias

      --
      Mattias Sjögren [C# MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • Stoitcho Goutsev \(100\)

        #4
        Re: short[] to byte[] conversion

        Alberto,

        There is no direct method call AFAIK, but you can use the Array.ConvertAl l
        method for that


        short[] arrShort = new short[] { 100, 200, 30 };
        byte[] arrByte;

        arrByte = Array.ConvertAl l<short, byte>(arrShort, delegate(short
        item){return (byte)item;});



        --
        HTH
        Stoitcho Goutsev (100)

        "Alberto Cardoso" <AlbertoCardoso @discussions.mi crosoft.comwrot e in
        message news:F96C10F9-45DF-4DB4-AD46-558D830F3937@mi crosoft.com...
        Is there a direct way to convert a short array to a byte array?
        I dont to use a for and cast every short to a byte.
        >
        I want something like the BitConverter class that accpets a short array as
        argument.
        >
        Bitconverter.Ge tBytes( short[] );
        >
        --
        Alberto Cardoso
        >

        Comment

        • Dustin Campbell

          #5
          Re: short[] to byte[] conversion

          There is no direct method call AFAIK, but you can use the
          Array.ConvertAl l method for that
          >
          short[] arrShort = new short[] { 100, 200, 30 };
          byte[] arrByte;
          arrByte = Array.ConvertAl l<short, byte>(arrShort, delegate(short
          item){return (byte)item;});
          Just for the curious, here's what this looks like with lambda expressions
          in C# 3.0:

          short[] arrShort = new short[] { 100, 200, 30 };
          byte[] arrByte = Array.ConvertAl l(arrShort, item =(byte)item);

          Best Regards,
          Dustin Campbell
          Developer Express Inc.


          Comment

          • Jonathan Wood

            #6
            Re: short[] to byte[] conversion

            Mattias,
            If you want to make a straight memory copy so that each short results
            in two bytes, you can use the System.Buffer.B lockCopy method.
            Dang, I'm glad I ran across this post. That's exactly what I was wondering
            about too.

            One thing: Is there any support for copying bytes from an array to a
            variable and the reverse? I realize I could do this by first creating an
            array of the correct type and then assigning an element to a variable. But
            is there by chance any way to do that directly?

            Thanks.

            --
            Jonathan Wood
            SoftCircuits Programming



            Comment

            • Mattias Sjögren

              #7
              Re: short[] to byte[] conversion

              Hi Jonathan,
              >One thing: Is there any support for copying bytes from an array to a
              >variable and the reverse?
              Check out the BitConverter class.



              Mattias

              --
              Mattias Sjögren [C# MVP] mattias @ mvps.org
              http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
              Please reply only to the newsgroup.

              Comment

              • Jonathan Wood

                #8
                Re: short[] to byte[] conversion

                Hi Mattias,

                That looks like it should do the trick, thanks!

                One other question, if I may: what are the ramifications of using routines
                that aren't CLS-compliant? I may want to run this code on a Web server. Is
                that going to be a problem?

                --
                Jonathan Wood
                SoftCircuits Programming



                "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rgwrote in message
                news:eMA5YaqCHH A.4224@TK2MSFTN GP06.phx.gbl...
                Hi Jonathan,
                >
                >>One thing: Is there any support for copying bytes from an array to a
                >>variable and the reverse?
                >
                Check out the BitConverter class.
                >
                >
                >
                Mattias
                >
                --
                Mattias Sjögren [C# MVP] mattias @ mvps.org
                http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                Please reply only to the newsgroup.

                Comment

                • Mattias Sjögren

                  #9
                  Re: short[] to byte[] conversion

                  >One other question, if I may: what are the ramifications of using routines
                  >that aren't CLS-compliant? I may want to run this code on a Web server. Is
                  >that going to be a problem?
                  CLS is all about language interoperabilit y and defining a lowest
                  common set of features that all managed languages should be able to
                  support. It's not related to security. So CLS is really only relevant
                  for people writing components that may be consumed by other languages.
                  If you do that, you may want to restrict your public interfaces to CLS
                  compliant features (or at least provide CLS compiliant alternatives).

                  Consuming non-CLS compilant stuff in your own code is generally not a
                  problem. That's an implementation detail that others don't care about.

                  However, one of the things that aren't CLS compilant are pointer
                  types, and to enable use of pointer types in C# you have to turn on
                  unsafe code. And _that_ may affect your code's ability to execute in
                  partially trusted environments.

                  Does that answer your question?


                  Mattias

                  --
                  Mattias Sjögren [C# MVP] mattias @ mvps.org
                  http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                  Please reply only to the newsgroup.

                  Comment

                  • Jonathan Wood

                    #10
                    Re: short[] to byte[] conversion

                    Hi Mattias,
                    One other question, if I may: what are the ramifications of using
                    routines
                    >>that aren't CLS-compliant? I may want to run this code on a Web server. Is
                    >>that going to be a problem?
                    >
                    CLS is all about language interoperabilit y and defining a lowest
                    common set of features that all managed languages should be able to
                    support. It's not related to security. So CLS is really only relevant
                    for people writing components that may be consumed by other languages.
                    If you do that, you may want to restrict your public interfaces to CLS
                    compliant features (or at least provide CLS compiliant alternatives).
                    >
                    Consuming non-CLS compilant stuff in your own code is generally not a
                    problem. That's an implementation detail that others don't care about.
                    >
                    However, one of the things that aren't CLS compilant are pointer
                    types, and to enable use of pointer types in C# you have to turn on
                    unsafe code. And _that_ may affect your code's ability to execute in
                    partially trusted environments.
                    >
                    Does that answer your question?
                    I think so. Basically, it's only an issue (potentially) if I'm writing
                    components to be used by other .NET languages. And it should have no effect
                    on being used on a Web server. I think I would avoid the pointer types then
                    for Web programming.

                    Thanks!

                    --
                    Jonathan Wood
                    SoftCircuits Programming



                    Comment

                    Working...