469,117 Members | 1,293 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,117 developers. It's quick & easy.

Convert string to a STRING Byte array

1
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
OR
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..
thanks

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

Example:

Expand|Select|Wrap|Line Numbers
  1.         byte[] mystringbytearray;
  2.         mystringbytearray = System.Text.Encoding.ASCII.GetBytes("gfhg");
  3.  
joedeene
Nov 15 '08 #3
balabaster
797 Expert 512MB
VB:
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)
C#
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
Plater
7,872 Expert 4TB
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
Nov 17 '08 #5
balabaster
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.