How to convert byte() to string, and from string back to byte()

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

    How to convert byte() to string, and from string back to byte()

    I'm writing an app in vb.net 1.1 and need to convert a byte array into a
    string, and then from a string back to a byte array.

    for example

    Private mByte() as New Byte(4){11,22,3 3,44}

    Now how do I convert it to:
    dim myStr as string = "11,22,33,4 4"

    and then back to the byte array?

    Thanks.



    --
    moondaddy@nospa m.nospam


  • Cor Ligthert [MVP]

    #2
    Re: How to convert byte() to string, and from string back to byte()

    Moondaddy,

    Have a look at the encode functions GetString and GetBytes.





    I hope this helps,

    Cor


    Comment

    • Peter Huang [MSFT]

      #3
      RE: How to convert byte() to string, and from string back to byte()

      Hi

      I thin you may need to use the code similar with below.
      Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button3.Click
      Dim mByte() As Byte = New Byte(3) {11, 22, 33, 44}
      Dim strs(3) As String
      For i As Integer = 0 To 3
      strs(i) = mByte(i).ToStri ng()
      Next
      For Each s As String In strs
      Debug.WriteLine (s)
      Next
      Dim b As Byte = Convert.ToByte( strs(0))
      End Sub

      If you wants to transfer the data as a string type, I think you may try to
      use the Base64encoding.
      Dim ss As String = Convert.ToBase6 4String(mByte)
      MsgBox(ss)
      Dim mb() As Byte = Convert.FromBas e64String(ss)


      Best regards,

      Peter Huang
      Microsoft Online Partner Support

      Get Secure! - www.microsoft.com/security
      This posting is provided "AS IS" with no warranties, and confers no rights.

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: How to convert byte() to string, and from string back to byte()

        "moondaddy" <moondaddy@nosp am.nospam> schrieb:[color=blue]
        > I'm writing an app in vb.net 1.1 and need to convert a byte array into a
        > string, and then from a string back to a byte array.[/color]

        'System.Text.En coding.<encodin g>.{GetBytes, GetString}'.

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

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: How to convert byte() to string, and from string back to byte()

          moondaddy,
          In addition to the other comments:

          Does your byte array contain Char codes, such as ASCII, ANSI, or Unicode
          characters? In which case you can use System.Text.Enc oding.GetBytes &
          Encoding.GetStr ing to convert to & from a string (as Cor & Herfried
          suggested).

          Of if you simply want a comma delimited list of numbers from the Byte Array
          I would create a couple of helper functions (As Peter suggested).

          Something like:

          Private Shared Function ArrayToString(B yVal bytes() As Byte, Optional
          ByVal format As String = Nothing) As String
          If bytes.Length = 0 Then Return String.Empty
          Dim sb As New System.Text.Str ingBuilder(byte s.Length * 4)
          For Each b As Byte In bytes
          sb.Append(b.ToS tring(format))
          sb.Append(","c)
          Next
          sb.Length -= 1
          Return sb.ToString()
          End Function

          Private Shared Function StringToArray(B yVal s As String, Optional ByVal
          style As System.Globaliz ation.NumberSty les = Nothing) As Byte()
          If s.Length = 0 Then Return New Byte() {}
          Dim values() As String = s.Split(","c)
          Dim bytes(values.Le ngth - 1) As Byte
          For index As Integer = 0 To values.Length - 1
          bytes(index) = Byte.Parse(valu es(index), style)
          Next
          Return bytes
          End Function

          Public Shared Sub Main()

          Dim mByte() As Byte = New Byte() {11, 22, 33, 44}
          Dim myStr As String

          'myStr = ArrayToString(m Byte, "x2")
          myStr = ArrayToString(m Byte)

          Debug.WriteLine (myStr)
          mByte = Nothing
          'mByte = StringToArray(m yStr,
          Globalization.N umberStyles.All owHexSpecifier)
          mByte = StringToArray(m yStr)

          End Sub

          Hope this helps
          Jay

          "moondaddy" <moondaddy@nosp am.nospam> wrote in message
          news:%23xvfI9Uo FHA.2916@TK2MSF TNGP14.phx.gbl. ..
          | I'm writing an app in vb.net 1.1 and need to convert a byte array into a
          | string, and then from a string back to a byte array.
          |
          | for example
          |
          | Private mByte() as New Byte(4){11,22,3 3,44}
          |
          | Now how do I convert it to:
          | dim myStr as string = "11,22,33,4 4"
          |
          | and then back to the byte array?
          |
          | Thanks.
          |
          |
          |
          | --
          | moondaddy@nospa m.nospam
          |
          |


          Comment

          • moondaddy

            #6
            Re: How to convert byte() to string, and from string back to byte()

            Thanks. I found i need to use this one since the array comes from a
            differenct type of encoding used in an encryption function and I found that
            I use the other methods above it actually chances the data so it can't be
            restored.

            Thanks to ALL.

            --
            moondaddy@nospa m.nospam
            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
            news:%23E6OwUao FHA.3068@TK2MSF TNGP15.phx.gbl. ..[color=blue]
            > moondaddy,
            > In addition to the other comments:
            >
            > Does your byte array contain Char codes, such as ASCII, ANSI, or Unicode
            > characters? In which case you can use System.Text.Enc oding.GetBytes &
            > Encoding.GetStr ing to convert to & from a string (as Cor & Herfried
            > suggested).
            >
            > Of if you simply want a comma delimited list of numbers from the Byte
            > Array
            > I would create a couple of helper functions (As Peter suggested).
            >
            > Something like:
            >
            > Private Shared Function ArrayToString(B yVal bytes() As Byte, Optional
            > ByVal format As String = Nothing) As String
            > If bytes.Length = 0 Then Return String.Empty
            > Dim sb As New System.Text.Str ingBuilder(byte s.Length * 4)
            > For Each b As Byte In bytes
            > sb.Append(b.ToS tring(format))
            > sb.Append(","c)
            > Next
            > sb.Length -= 1
            > Return sb.ToString()
            > End Function
            >
            > Private Shared Function StringToArray(B yVal s As String, Optional ByVal
            > style As System.Globaliz ation.NumberSty les = Nothing) As Byte()
            > If s.Length = 0 Then Return New Byte() {}
            > Dim values() As String = s.Split(","c)
            > Dim bytes(values.Le ngth - 1) As Byte
            > For index As Integer = 0 To values.Length - 1
            > bytes(index) = Byte.Parse(valu es(index), style)
            > Next
            > Return bytes
            > End Function
            >
            > Public Shared Sub Main()
            >
            > Dim mByte() As Byte = New Byte() {11, 22, 33, 44}
            > Dim myStr As String
            >
            > 'myStr = ArrayToString(m Byte, "x2")
            > myStr = ArrayToString(m Byte)
            >
            > Debug.WriteLine (myStr)
            > mByte = Nothing
            > 'mByte = StringToArray(m yStr,
            > Globalization.N umberStyles.All owHexSpecifier)
            > mByte = StringToArray(m yStr)
            >
            > End Sub
            >
            > Hope this helps
            > Jay
            >
            > "moondaddy" <moondaddy@nosp am.nospam> wrote in message
            > news:%23xvfI9Uo FHA.2916@TK2MSF TNGP14.phx.gbl. ..
            > | I'm writing an app in vb.net 1.1 and need to convert a byte array into a
            > | string, and then from a string back to a byte array.
            > |
            > | for example
            > |
            > | Private mByte() as New Byte(4){11,22,3 3,44}
            > |
            > | Now how do I convert it to:
            > | dim myStr as string = "11,22,33,4 4"
            > |
            > | and then back to the byte array?
            > |
            > | Thanks.
            > |
            > |
            > |
            > | --
            > | moondaddy@nospa m.nospam
            > |
            > |
            >
            >[/color]


            Comment

            • Peter Huang [MSFT]

              #7
              Re: How to convert byte() to string, and from string back to byte()

              Hi

              Thanks for your confirm.

              Best regards,

              Peter Huang
              Microsoft Online Partner Support

              Get Secure! - www.microsoft.com/security
              This posting is provided "AS IS" with no warranties, and confers no rights.

              Comment

              Working...