"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
John Vottero wrote:
>"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk. ..
>> public static string ToHex(byte[] ba)
{
StringBuilder sb = new StringBuilder(2 * ba.Length);
for(int i = 0; i < ba.Length; i++)
{
sb.Append(ba[i].ToString("X2"));
}
return sb.ToString();
}
and then call it with:
string resultstring = ToHex(yourencoding.GetBytes(inputstring));
You don't need ToHex, use System.BitConverter.ToString().
Can BitConverter.ToString omit the hyphen ?
No, but how about BitConverter.ToString(array).Replace('-', '');
or even (if you want it to look like "0x12, 0x23, 0x45"):
"0x" + BitConverter.ToString(array).Replace("-", ", 0x");
In either case, this doesn't seem to be what the OP wants. The OP has "hex
numbers" (i am GUESSING in a byte[] array) that he wants converted into what
looks like ASCII characters (i.e. if he has a value 0x78 he wants it
converted to "x")
But this is a guess. For all I know, he has a STRING containing a formatted
"0x49, 0x41 ..." that he wants to convert. He never said. The OP just said
"bunch of hex numbers converted to a string. Of course, if it's a byte
array of only ascii chars, he could do something like:
// The bytes. To use the string constructor, NULL TERMINATE THIS ARRAY or
use the start,count overload
// String constructor with sbyte only accepts ASCII characters. to use
Unicode, use the normal char[] ctor.
sbyte[] theArray = { 0x49, 0x20, 0x61, 0x6d, 0x20, 0x61, 0x20, 0x64, 0x6f,
0x64, 0x6f };
string theString;
unsafe
{
fixed (sbyte* sbArray = &theArray[0])
{
theString = new string(sbArray, 0, theArray.Length);
}
}
Console.WriteLine(theString);
But <shrugI don't know what exactly is desired...
--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?