Formatting string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

h
how can I convert a byte array to string, so that the bytes appear in hex format in the string. Can anyone help

John
 
Hi John,

Normaly Jon answers this, I did not see him some days, I try it.

A string is in unicode format, 2Bytes in dotNet programs.

Maybe you do you want to convert an byte array which holds extended ASCII to
a string?
(Which than will be unicode 2Bytes).

In VB there there are two functions for that Chr and ChrW while the last is
advised.

Cor
 
byte[] bs = {0xef, 0xee, 0xaa}
foreach (byte b in bs

System.Console.WriteLine(b.ToString("x"))


Tu-Thac
www.ongtech.co

----- John Sturm wrote: ----

h
how can I convert a byte array to string, so that the bytes appear in hex format in the string. Can anyone help

John
 
John,
In addition to others comments, I would use a StringBuilder to build the
string of hex digits.

Something like:

Dim bytes() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20}

Dim sb As New System.Text.StringBuilder(bytes.Length * 2)
For Each b As Byte In bytes
sb.Append(b.ToString("x2"))
Next
Dim s As String = sb.ToString

Hope this helps
Jay

John Sturm said:
hi
how can I convert a byte array to string, so that the bytes appear in hex
format in the string. Can anyone help?
 
Cor Ligthert said:
Normaly Jon answers this, I did not see him some days, I try it.

Yup - I've been away.
A string is in unicode format, 2Bytes in dotNet programs.

Hang on though - that's not what he's asking for. He's basically asking
for BitConverter.ToString(byte[]).

He wants to *format* a byte array, not *decode* it.
 
Hi Jon,
Hang on though - that's not what he's asking for. He's basically asking
for BitConverter.ToString(byte[]).

He wants to *format* a byte array, not *decode* it.

I knew also that that could be the question, however really sure was I not
from that because "appear" is not "showed". (The old charactersstrings
where in one byte hex format). I take always the answer on what most people
fits first. It was a try what would be the answer to get that sure.


Cor
 
Back
Top