By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,971 Members | 890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,971 IT Pros & Developers. It's quick & easy.

How to construct a string with bunch of hex numbers?

P: n/a
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!
Sep 11 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Sep 11, 4:14 pm, dh <d...@discussions.microsoft.comwrote:
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"

Sep 11 '07 #2

P: n/a
"Doug Semler" wrote:
On Sep 11, 4:14 pm, dh <d...@discussions.microsoft.comwrote:
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"
No. I'd like to have a string like "xyzabc" and each ASCII char corresponds
to a hex number.
Sep 11 '07 #3

P: n/a
On Sep 11, 5:12 pm, dh <d...@discussions.microsoft.comwrote:
"Doug Semler" wrote:
On Sep 11, 4:14 pm, dh <d...@discussions.microsoft.comwrote:
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"

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)...

Sep 11 '07 #4

P: n/a
dh wrote:
"Doug Semler" wrote:
>On Sep 11, 4:14 pm, dh <d...@discussions.microsoft.comwrote:
>>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"
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[i].ToString("X2"));
}
return sb.ToString();
}

and then call it with:

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

Arne
Sep 15 '07 #5

P: n/a

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
dh wrote:
>"Doug Semler" wrote:
>>On Sep 11, 4:14 pm, dh <d...@discussions.microsoft.comwrote:
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"
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[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().

Sep 16 '07 #6

P: n/a
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 ?

Arne
Sep 16 '07 #7

P: n/a
"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?

Sep 16 '07 #8

P: n/a
Doug Semler wrote:
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
Sep 17 '07 #9

P: n/a
"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 ?
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);

Sep 17 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.