Convert Byte() <-> String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Altman

    Convert Byte() <-> String

    Hi all,

    I'm looking for the fastest way to convert an array of bytes to String. I
    also need to convert a String back to its original Byte() representation.
    Convert.ToBase6 4String and Convert.FromBas e64String seem like the closest
    thing I can find to what I'm looking for baked into the base class library.

    Can anyone suggest a better way to do this?

    TIA - Bob

  • kimiraikkonen

    #2
    Re: Convert Byte() &lt;-&gt; String

    On Jun 22, 8:05 pm, "Bob Altman" <r...@nospam.no spamwrote:
    Hi all,
    >
    I'm looking for the fastest way to convert an array of bytes to String. I
    also need to convert a String back to its original Byte() representation.
    Convert.ToBase6 4String and Convert.FromBas e64String seem like the closest
    thing I can find to what I'm looking for baked into the base class library.
    >
    Can anyone suggest a better way to do this?
    >
    TIA - Bob
    Hi,
    Would you consider using BitConverter class:


    Converts base data types to an array of bytes, and an array of bytes to base data types.


    Hope this helps,

    Onur Güzel

    Comment

    • Teemu

      #3
      Re: Convert Byte() &lt;-&gt; String


      "Bob Altman" <rda@nospam.nos pamkirjoitti viestissä
      news:OErH5oI1IH A.3756@TK2MSFTN GP04.phx.gbl...
      Hi all,
      >
      I'm looking for the fastest way to convert an array of bytes to String. I
      also need to convert a String back to its original Byte() representation.
      Convert.ToBase6 4String and Convert.FromBas e64String seem like the closest
      thing I can find to what I'm looking for baked into the base class
      library.
      >
      Can anyone suggest a better way to do this?
      This might be what you are looking for:

      Dim UTF8Converter As New System.Text.UTF 8Encoding
      Dim OriginalString = "This is a test."
      Dim Bytes As Byte() = UTF8Converter.G etBytes(Origina lString)

      MsgBox(UTF8Conv erter.GetString (Bytes))

      There are other encodings as well.

      -Teemu

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Convert Byte() &lt;-&gt; String

        "Bob Altman" <rda@nospam.nos pamschrieb:
        I'm looking for the fastest way to convert an array of bytes to String. I
        also need to convert a String back to its original Byte() representation.
        Convert.ToBase6 4String and Convert.FromBas e64String seem like the closest
        thing I can find to what I'm looking for baked into the base class
        library.
        In addition to the above methods, take a look at
        'System.Text.En coding.GetStrin g' and 'System.Text.En coding.GetBytes '.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Steven Cheng [MSFT]

          #5
          RE: Convert Byte() &lt;-&gt; String

          Hi Bob,

          I think Base64 encoding is the common and sophisciated approach. Also, for
          built-in text binary converting, you can have a look at the
          System.Text.Enc oding namespace has other members suggested. There contains
          many encoding types(mainly used for converting Text characters to binary
          encoding stream). You can use those unicode encoding for your scenario.
          e.g.

          =============== ======
          Dim bytes() As Byte

          bytes = System.Text.Enc oding.UTF8.GetB ytes(StringText )
          =============== ======

          Utf8 encoding is efficient for compression since it use different length
          binary format for different characters. this helps when you want to get
          compressed size of the encoded binary.

          Sincerely,

          Steven Cheng

          Microsoft MSDN Online Support Lead


          Delighting our customers is our #1 priority. We welcome your comments and
          suggestions about how we can improve the support we provide to you. Please
          feel free to let my manager know what you think of the level of service
          provided. You can send feedback directly to my manager at:
          msdnmg@microsof t.com.

          =============== =============== =============== =====
          Get notification to my posts through email? Please refer to
          Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

          ications.

          Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
          where an initial response from the community or a Microsoft Support
          Engineer within 1 business day is acceptable. Please note that each follow
          up response may take approximately 2 business days as the support
          professional working with you may need further investigation to reach the
          most efficient resolution. The offering is not appropriate for situations
          that require urgent, real-time or phone-based interactions or complex
          project analysis and dump analysis issues. Issues of this nature are best
          handled working with a dedicated Microsoft Support Engineer by contacting
          Microsoft Customer Support Services (CSS) at
          http://msdn.microsoft.com/subscripti...t/default.aspx.
          =============== =============== =============== =====
          This posting is provided "AS IS" with no warranties, and confers no rights.



          --------------------
          >From: "Bob Altman" <rda@nospam.nos pam>
          >Subject: Convert Byte() <-String
          >Date: Sun, 22 Jun 2008 10:05:48 -0700
          >Hi all,
          >
          >I'm looking for the fastest way to convert an array of bytes to String. I
          >also need to convert a String back to its original Byte() representation.
          >Convert.ToBase 64String and Convert.FromBas e64String seem like the closest
          >thing I can find to what I'm looking for baked into the base class library.
          >
          >Can anyone suggest a better way to do this?
          >
          >TIA - Bob
          >
          >

          Comment

          • Bob Altman

            #6
            Re: Convert Byte() &lt;-&gt; String

            Also, for
            built-in text binary converting, you can have a look at the
            System.Text.Enc oding namespace has other members suggested.
            I thought about using System.Text.Enc oding, but the problem with that is
            that the Byte() data I am trying to convert to a String can contain any
            arbitrary data. Most encodings (such as UTF-8) are only valid for a subset
            of possible byte values or combinations of values.

            I wasn't aware of the BitConverter class that Onur pointed out. The major
            problem with that class is that it doesn't seem to provide symmetrical
            encoding and decoding between strings and byte arrays. In other words, it
            can convert a byte array to a string of hex digits separated by dashes, but
            a quick look at the docs didn't reveal a way to convert the string back to a
            byte array. Also, while the string created by BitConverter has the
            advantage (in some applications) of being human-readable, it's twice as long
            as Base64 string representation.

            Comment

            • Steven Cheng [MSFT]

              #7
              Re: Convert Byte() &lt;-&gt; String

              Thanks for your reply Bob,

              Yes, if the data is pure binary with all kinds for values, those dedicated
              binary/text encoding schemas (such as Base64 or Hex) are perferred. So far
              I think the base64 encoding is the proper one here. The
              BitConvertor.To String method just help produce the hex encoding like
              output, it will be effecient on processing time since it will always use
              two character to represent each byte, however, it will consume much more
              space.

              Sincerely,

              Steven Cheng

              Microsoft MSDN Online Support Lead


              Delighting our customers is our #1 priority. We welcome your comments and
              suggestions about how we can improve the support we provide to you. Please
              feel free to let my manager know what you think of the level of service
              provided. You can send feedback directly to my manager at:
              msdnmg@microsof t.com.

              =============== =============== =============== =====
              Get notification to my posts through email? Please refer to
              Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

              ications.
              =============== =============== =============== =====
              This posting is provided "AS IS" with no warranties, and confers no rights.

              --------------------
              >From: "Bob Altman" <rda@nospam.nos pam>
              >Subject: Re: Convert Byte() <-String
              >Date: Mon, 23 Jun 2008 07:53:52 -0700
              >
              >Also, for
              >built-in text binary converting, you can have a look at the
              >System.Text.En coding namespace has other members suggested.
              >
              >I thought about using System.Text.Enc oding, but the problem with that is
              >that the Byte() data I am trying to convert to a String can contain any
              >arbitrary data. Most encodings (such as UTF-8) are only valid for a
              subset
              >of possible byte values or combinations of values.
              >
              >I wasn't aware of the BitConverter class that Onur pointed out. The major
              >problem with that class is that it doesn't seem to provide symmetrical
              >encoding and decoding between strings and byte arrays. In other words, it
              >can convert a byte array to a string of hex digits separated by dashes,
              but
              >a quick look at the docs didn't reveal a way to convert the string back to
              a
              >byte array. Also, while the string created by BitConverter has the
              >advantage (in some applications) of being human-readable, it's twice as
              long
              >as Base64 string representation.
              >
              >

              Comment

              Working...