473,396 Members | 2,034 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,396 software developers and data experts.

How to construct a string with bunch of hex numbers?

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
9 4491
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
"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
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
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

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

Similar topics

5
by: Lorn | last post by:
I'm trying to work on a dataset that has it's primary numbers saved as floats in string format. I'd like to work with them as integers with an implied decimal to the hundredth. The problem is that...
1
by: Admin | last post by:
I would like to know how to manipulate a string in order to restrict user error input. This is what I would like to achieve. Please refer to the following pseudocode. Private Sub...
5
by: Colleyville Alan | last post by:
I have some data in a table structured like this: Date Cust_ID CUSIP Amount 01/31/2005 060208 02507M303 27,061.84...
12
by: Laser Lu | last post by:
Hello, everybody, do you know how to use this Grouping Construct? (?> ) I've found its reference on MSDN, but still can not understand it totally. The following is its description: ...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
2
by: newhand | last post by:
If somehow a bunch of function names in string can be passed into an executible, is it possible that for each name call, it will trigger the corresponding function of that name? Of course, this can...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
6
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a bunch of strings that are 20 characters long. The user inputs data 10 characters long and I am inserting data at character spot number 11. well what if the user inputs data that is...
1
by: Ducknut | last post by:
Not so much a problem as a discussion. I am currently in the early stages of designing a database to hold a bunch of water quality data (e.g., concentrations of heavy metals in drinking water). Water...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.