Convert string to a STRING Byte array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • da1978
    New Member
    • Nov 2008
    • 1

    Convert string to a STRING Byte array

    Hi experts,

    I need to convert a string or a Byte array to a string byte array.

    Its relatively easy to convert a string to an char array or a byte array but not a
    Code:
    STRING byte array. i.e. 
                Dim Array() As Char
                Dim strwork As String = "76A3kj9d6"
                Array = strwork.ToCharArray
    OR
    Code:
        Dim s As String = "76A3kj9d6"
            MessageBox.Show("Base String:  " & s)
            Dim b() As Byte = System.Text.UTF8Encoding.UTF8.GetBytes(s)
            MessageBox.Show("Converted from byte array to string:  " & System.Text.UTF8Encoding.UTF8.GetString(b))
    Please help..
    thanks

    <edit by mod: use [CODE] tags, not [I] tags>
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    question moved to .NET movie.

    Comment

    • joedeene
      Contributor
      • Jul 2008
      • 579

      #3
      I've never heard of a "string byte array". You can, however, get the bytes of a string and then place them in a byte array.

      Example:

      Code:
              byte[] mystringbytearray;
              mystringbytearray = System.Text.Encoding.ASCII.GetBytes("gfhg");
      joedeene

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        VB:
        Code:
        Dim Converter As Encoding = Encoding.ASCII
        Dim InputString As String = "Hello World"
        Dim ByteResult() As Byte = Encoding.ASCII.GetBytes(InputString)
        Dim StringResult As String = Converter.GetString(ByteResult)
        Console.WriteLine(StringResult)
        C#
        Code:
        Encoding Converter = Encoding.ASCII;
        string InputString = "Hello World";
        byte[] ByteResult = Encoding.ASCII.GetBytes(InputString);
        string StringResult = Converter.GetString(ByteResult);
        Console.WriteLine(StringResult);

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I think perhaps the op wants a "hex string" representation of their string.

          For instance if one had:
          "A BALL"
          You would then have a hex string of:
          "41 20 42 41 4C 4C"

          This can be achieved by typecasting a byte or a char to an integer and then using the .ToString() overload to print out 2digit hex values

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            That'll teach me to answer posts when I'm falling asleep :oP

            Comment

            Working...