473,248 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 3666
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
11
by: Dan C | last post by:
Is there a routine in c# that will transform a string ie"Hello Mom" into a Byte array. I have found char cTmp = pString.ToCharArray(); But I have not been able to figure out how to convert a...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
6
by: moondaddy | last post by:
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,33,44} Now how...
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
5
by: da1978 | last post by:
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 STRING byte array. i.e. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.