471,893 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,893 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. STRING byte array. i.e. 
  2.             Dim Array() As Char
  3.             Dim strwork As String = "76A3kj9d6"
  4.             Array = strwork.ToCharArray
Expand|Select|Wrap|Line Numbers
  1.     Dim s As String = "76A3kj9d6"
  2.         MessageBox.Show("Base String:  " & s)
  3.         Dim b() As Byte = System.Text.UTF8Encoding.UTF8.GetBytes(s)
  4.         MessageBox.Show("Converted from byte array to string:  " & System.Text.UTF8Encoding.UTF8.GetString(b))
Please help..

<edit by mod: use [code] tags, not [i] tags>
Nov 14 '08 #1
5 4614
8,127 Expert 4TB
question moved to .NET movie.
Nov 15 '08 #2
583 512MB
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.


Expand|Select|Wrap|Line Numbers
  1.         byte[] mystringbytearray;
  2.         mystringbytearray = System.Text.Encoding.ASCII.GetBytes("gfhg");
Nov 15 '08 #3
797 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. Dim Converter As Encoding = Encoding.ASCII
  2. Dim InputString As String = "Hello World"
  3. Dim ByteResult() As Byte = Encoding.ASCII.GetBytes(InputString)
  4. Dim StringResult As String = Converter.GetString(ByteResult)
  5. Console.WriteLine(StringResult)
Expand|Select|Wrap|Line Numbers
  1. Encoding Converter = Encoding.ASCII;
  2. string InputString = "Hello World";
  3. byte[] ByteResult = Encoding.ASCII.GetBytes(InputString);
  4. string StringResult = Converter.GetString(ByteResult);
  5. Console.WriteLine(StringResult);
Nov 17 '08 #4
7,872 Expert 4TB
I think perhaps the op wants a "hex string" representation of their string.

For instance if one had:
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
Nov 17 '08 #5
797 Expert 512MB
That'll teach me to answer posts when I'm falling asleep :oP
Nov 17 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Swarup | last post: by
6 posts views Thread by Ricardo Quintanilla | last post: by
15 posts views Thread by Kueishiong Tu | last post: by
6 posts views Thread by Allan Ebdrup | last post: by
6 posts views Thread by Bob Altman | last post: by
12 posts views Thread by Peter | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.