468,512 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

String to Byte Array

Tom
Stupid Question: Have a string ("ABC"); need to convert that to a byte array
(Dim x(3) as Byte). Do I have to do this manually or is there a .NET
command to do it automatically?

Tom
Nov 20 '05 #1
5 3406
"Tom" <to*@nospam.com> schrieb
Stupid Question: Have a string ("ABC"); need to convert that to a
byte array (Dim x(3) as Byte). Do I have to do this manually or is
there a .NET command to do it automatically?


dim x as byte()

x = System.text.encoding.unicode.getbytes("ABC")

converts to a Unicode encoded byte array.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
Tom
Armin: Cool, thanks! However, I had to use
System.Text.Encoding.ASCII.GetBytes to convert it properly - otherwise, I
was getting a NULL (chr(0)) between each character.

Also, I see that System.Text.Encoding.ASCII.GetChars converts it back to a
string value.

Tom

"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Tom" <to*@nospam.com> schrieb
Stupid Question: Have a string ("ABC"); need to convert that to a
byte array (Dim x(3) as Byte). Do I have to do this manually or is
there a .NET command to do it automatically?


dim x as byte()

x = System.text.encoding.unicode.getbytes("ABC")

converts to a Unicode encoded byte array.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
* "Tom" <to*@nospam.com> scripsit:
Also, I see that System.Text.Encoding.ASCII.GetChars converts it back to a
string value.


If you want a string, you can use 'GetString' instead of 'GetChars'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
"Tom" <to*@nospam.com> schrieb
Armin: Cool, thanks! However, I had to use
System.Text.Encoding.ASCII.GetBytes to convert it properly -
otherwise, I was getting a NULL (chr(0)) between each character.
That's what Unicode encoding means: 2 bytes per character.

ASCII: You know that ASCII is 7 bits only, i.e. no values > 127 possible?
Also, I see that System.Text.Encoding.ASCII.GetChars converts it back
to a string value.


Why not GetString instead of GetChars?

--
Armin

Nov 20 '05 #5
Tom,
Remember that ASCII only supports characters 0 to 127, if you want
characters 0 to 255 then you may want to consider using Encoding.Default
instead, which is set to your current Windows Code Page.

There is also Encoding.UTF7 & Encoding UTF8 which will give 1 byte per char,
however they do interesting things with characters 128 to 255...

For a series of links on Unicode, Ascii, Ansi & various encodings used see:
http://www.yoda.arachsys.com/csharp/unicode.html

Hope this helps
Jay

"Tom" <to*@nospam.com> wrote in message
news:eO*************@tk2msftngp13.phx.gbl...
Armin: Cool, thanks! However, I had to use
System.Text.Encoding.ASCII.GetBytes to convert it properly - otherwise, I
was getting a NULL (chr(0)) between each character.

Also, I see that System.Text.Encoding.ASCII.GetChars converts it back to a
string value.

Tom

"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Tom" <to*@nospam.com> schrieb
Stupid Question: Have a string ("ABC"); need to convert that to a
byte array (Dim x(3) as Byte). Do I have to do this manually or is
there a .NET command to do it automatically?


dim x as byte()

x = System.text.encoding.unicode.getbytes("ABC")

converts to a Unicode encoded byte array.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Simon Schaap | last post: by
11 posts views Thread by Dan C | last post: by
6 posts views Thread by Ricardo Quintanilla | last post: by
4 posts views Thread by ThunderMusic | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.