Conversion of byte[]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Bean

    #1

    Conversion of byte[]

    I have several data files containing ASCII strings, int, long and byte data
    types and need to store the data in variables of the appropriate type. From
    previous posts, there seem to be several methods for converting byte[] to
    strings.

    What is the best way to convert a byte[] to a long or int?

    Thanks,
    Tom


  • Pieter

    #2
    Re: Conversion of byte[]

    Tom,

    I don't see the point in converting an array (multiple values) to a basic
    data type (1 value).

    Pieter

    "Tom Bean" <tbean@t-a-c.com> wrote in message
    news:eCuC6%231S DHA.1948@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > I have several data files containing ASCII strings, int, long and byte[/color]
    data[color=blue]
    > types and need to store the data in variables of the appropriate type.[/color]
    From[color=blue]
    > previous posts, there seem to be several methods for converting byte[] to
    > strings.
    >
    > What is the best way to convert a byte[] to a long or int?
    >
    > Thanks,
    > Tom
    >
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: Conversion of byte[]

      Pieter <Pieter@N.O.S.P .4.M> wrote:[color=blue]
      > I don't see the point in converting an array (multiple values) to a basic
      > data type (1 value).[/color]

      As Tom posted, the original file *contains* int, long and byte data.
      Clearly it has to be represented in bytes, as that's the only way that
      anything can be represented in files, fundamentally.

      BitConverter is almost certainly the way to go here.

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

      • Pieter

        #4
        Re: Conversion of byte[]

        Oooops! I didn't thought about that, just tried to help.

        "Jon Skeet" <skeet@pobox.co m> wrote in message
        news:MPG.197f0d 1d35f9103d98a10 0@news.microsof t.com...[color=blue]
        > Pieter <Pieter@N.O.S.P .4.M> wrote:[color=green]
        > > I don't see the point in converting an array (multiple values) to a[/color][/color]
        basic[color=blue][color=green]
        > > data type (1 value).[/color]
        >
        > As Tom posted, the original file *contains* int, long and byte data.
        > Clearly it has to be represented in bytes, as that's the only way that
        > anything can be represented in files, fundamentally.
        >
        > BitConverter is almost certainly the way to go here.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet/
        > If replying to the group, please do not mail me too[/color]


        Comment

        • Tom Bean

          #5
          Re: Conversion of byte[]

          I checked the documentation on System.BitConve rter.ToString and unless I'm
          misreading it, it appears to convert the byte[] to a hex-string instead of a
          normal string of characters. If that is correct, it isn't what I need. Is
          there a preferred method for converting byte[] to string?

          Also, once I have converted the byte[] to the variables it contains, I need
          to convert them back to a byte[] for updating the file.
          System.BitConve rter.GetBytes seems to be able to convert from most numeric
          data types to byte[]. What is the best method to convert string back to
          ASCII in the byte[]?

          Thanks,
          Tom

          "Tom Bean" <tbean@t-a-c.com> wrote in message
          news:eCuC6%231S DHA.1948@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > I have several data files containing ASCII strings, int, long and byte[/color]
          data[color=blue]
          > types and need to store the data in variables of the appropriate type.[/color]
          From[color=blue]
          > previous posts, there seem to be several methods for converting byte[] to
          > strings.
          >
          > What is the best way to convert a byte[] to a long or int?
          >
          > Thanks,
          > Tom
          >
          >[/color]


          Comment

          • Tom Bean

            #6
            Re: Conversion of byte[]

            Jon,

            Thanks for your help.

            Another question: My byte[] from the file contains arrays of bytes, shorts,
            ints, etc. The files were created in C++ where it is a simple matter to use
            pointers to move through the file's array of bytes and copy/convert the
            data.

            What is the best method of copying these embedded arrays into other arrays?

            Thanks,
            Tom

            "Jon Skeet" <skeet@pobox.co m> wrote in message
            news:MPG.197f74 5b26a8493c98a11 5@news.microsof t.com...[color=blue]
            > Tom Bean <tbean@t-a-c.com> wrote:[color=green]
            > > I checked the documentation on System.BitConve rter.ToString and unless[/color][/color]
            I'm[color=blue][color=green]
            > > misreading it, it appears to convert the byte[] to a hex-string instead[/color][/color]
            of a[color=blue][color=green]
            > > normal string of characters. If that is correct, it isn't what I need.[/color][/color]
            Is[color=blue][color=green]
            > > there a preferred method for converting byte[] to string?[/color]
            >
            > Yes. Use System.Text.Enc oding.
            > See http://www.pobox.com/~skeet/csharp/unicode.html for more details.
            >[color=green]
            > > Also, once I have converted the byte[] to the variables it contains, I[/color][/color]
            need[color=blue][color=green]
            > > to convert them back to a byte[] for updating the file.
            > > System.BitConve rter.GetBytes seems to be able to convert from most[/color][/color]
            numeric[color=blue][color=green]
            > > data types to byte[]. What is the best method to convert string back to
            > > ASCII in the byte[]?[/color]
            >
            > Again, System.Text.Enc oding.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet/
            > If replying to the group, please do not mail me too[/color]


            Comment

            • Jon Skeet

              #7
              Re: Conversion of byte[]

              Tom Bean <tbean@t-a-c.com> wrote:[color=blue]
              > Another question: My byte[] from the file contains arrays of bytes, shorts,
              > ints, etc. The files were created in C++ where it is a simple matter to use
              > pointers to move through the file's array of bytes and copy/convert the
              > data.
              >
              > What is the best method of copying these embedded arrays into other arrays?[/color]

              You generally don't need to - just keep an index of where you are, and
              give the index to BitConverter.

              Alternatively, often BinaryReader will do what you want.

              If you need to copy arrays around, just create a new array of the right
              size and then use Array.Copy.

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

              • Tom Bean

                #8
                Re: Conversion of byte[]

                Jon,

                I encountered a problem trying to copy the byte[] to a short[]. Array.Copy
                expects the source and destination to be the same size. Is there any
                alternative besides using BitConverter to convert each pair of bytes to a
                short?

                Thanks,
                Tom

                "Jon Skeet" <skeet@pobox.co m> wrote in message
                news:MPG.197f82 caf9ef214998a11 b@news.microsof t.com...[color=blue]
                > Tom Bean <tbean@t-a-c.com> wrote:[color=green]
                > > Another question: My byte[] from the file contains arrays of bytes,[/color][/color]
                shorts,[color=blue][color=green]
                > > ints, etc. The files were created in C++ where it is a simple matter to[/color][/color]
                use[color=blue][color=green]
                > > pointers to move through the file's array of bytes and copy/convert the
                > > data.
                > >
                > > What is the best method of copying these embedded arrays into other[/color][/color]
                arrays?[color=blue]
                >
                > You generally don't need to - just keep an index of where you are, and
                > give the index to BitConverter.
                >
                > Alternatively, often BinaryReader will do what you want.
                >
                > If you need to copy arrays around, just create a new array of the right
                > size and then use Array.Copy.
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet/
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jon Skeet

                  #9
                  Re: Conversion of byte[]

                  Tom Bean <tbean@t-a-c.com> wrote:[color=blue]
                  > I encountered a problem trying to copy the byte[] to a short[]. Array.Copy
                  > expects the source and destination to be the same size. Is there any
                  > alternative besides using BitConverter to convert each pair of bytes to a
                  > short?[/color]

                  You can use Buffer.BlockCop y - hopefully that should do what you want.

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

                  Working...