How to construct a string with bunch of hex numbers?

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

Guest

If there's bunch of hex numbers: 0xA6, 0xD9, 0x00, 0xAA, 0x00, and 0x62, how
to construct a string ("string" in C#) with those hex numbers?

Thanks!
 
If there's bunch of hex numbers: 0xA6, 0xD9, 0x00, 0xAA, 0x00, and 0x62, how
to construct a string ("string" in C#) with those hex numbers?

What do you mean by "a bunch of hex numbers?" Do you mean an array of
bytes? if so:

string s = BitConverter.ToString(byteArray);

would give you a string that looked like "A6-D9-00-AA-00-62"
 
Doug Semler said:
What do you mean by "a bunch of hex numbers?" Do you mean an array of
bytes? if so:

string s = BitConverter.ToString(byteArray);

would give you a string that looked like "A6-D9-00-AA-00-62"
No. I'd like to have a string like "xyzabc" and each ASCII char corresponds
to a hex number.
 
No. I'd like to have a string like "xyzabc" and each ASCII char corresponds
to a hex number.

Please define "bunch of hex numbers." How are they being stored?
Array of bytes? Array of chars? in an array of doubles? Are the "hex"
digits only going to be ASCII or are they Unicode characters? From
your numbers above, you aren't representing an ASCII string anyway so
then you have to start getting into Char conversions and how to do
that properly.

There are a few overloads of the String() class that take in an array
of Char or a pointer to sbyte (last one must be null terminated and is
only used in unsafe code)...
 
dh said:
No. I'd like to have a string like "xyzabc" and each ASCII char corresponds
to a hex number.

public static string ToHex(byte[] ba)
{
StringBuilder sb = new StringBuilder(2 * ba.Length);
for(int i = 0; i < ba.Length; i++)
{
sb.Append(ba.ToString("X2"));
}
return sb.ToString();
}

and then call it with:

string resultstring = ToHex(yourencoding.GetBytes(inputstring));

Arne
 
Arne Vajhøj said:
dh said:
No. I'd like to have a string like "xyzabc" and each ASCII char
corresponds to a hex number.

public static string ToHex(byte[] ba)
{
StringBuilder sb = new StringBuilder(2 * ba.Length);
for(int i = 0; i < ba.Length; i++)
{
sb.Append(ba.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().
 
John said:
Arne Vajhøj said:
public static string ToHex(byte[] ba)
{
StringBuilder sb = new StringBuilder(2 * ba.Length);
for(int i = 0; i < ba.Length; i++)
{
sb.Append(ba.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 ?

Arne
 
Arne Vajhøj said:
John said:
Arne Vajhøj said:
public static string ToHex(byte[] ba)
{
StringBuilder sb = new StringBuilder(2 * ba.Length);
for(int i = 0; i < ba.Length; i++)
{
sb.Append(ba.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 <shrug> I 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?
 
Doug said:
No, but how about BitConverter.ToString(array).Replace('-', '');

That will probably do fine even though it seems unelegant to put
in hyphens and them remove them.
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")

Could be.

I have seen the request I assumed it was a couple of time in
contexts of generating printable text of various encrypted
stuff or hash values.

Arne
 
Arne Vajhøj said:
John said:
Arne Vajhøj said:
public static string ToHex(byte[] ba)
{
StringBuilder sb = new StringBuilder(2 * ba.Length);
for(int i = 0; i < ba.Length; i++)
{
sb.Append(ba.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 ?


I don't think that BitCOnverter can omit the hyphen. I looked back at the
history of this thread to see what the OP really wanted and I saw that Doug
has already suggested BitConverter.ToString but, it sounds to me like the OP
has a byte array of ASCII values and wants to turn it into a string which
would be something like:

string myString = System.Text.Encoding.ASCII.GetString(myByteArray);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top