473,749 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4521
On Sep 11, 4:14 pm, dh <d...@discussio ns.microsoft.co mwrote:
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.To String(byteArra y);

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...@discussio ns.microsoft.co mwrote:
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.To String(byteArra y);

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...@discussio ns.microsoft.co mwrote:
"Doug Semler" wrote:
On Sep 11, 4:14 pm, dh <d...@discussio ns.microsoft.co mwrote:
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.To String(byteArra y);
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...@discussio ns.microsoft.co mwrote:
>>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.To String(byteArra y);

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(yourencod ing.GetBytes(in putstring));

Arne
Sep 15 '07 #5

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:46******** *************** @news.sunsite.d k...
dh wrote:
>"Doug Semler" wrote:
>>On Sep 11, 4:14 pm, dh <d...@discussio ns.microsoft.co mwrote:
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.To String(byteArra y);

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(yourencod ing.GetBytes(in putstring));
You don't need ToHex, use System.BitConve rter.ToString() .

Sep 16 '07 #6
John Vottero wrote:
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:46******** *************** @news.sunsite.d k...
> 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(yourencod ing.GetBytes(in putstring));

You don't need ToHex, use System.BitConve rter.ToString() .
Can BitConverter.To String omit the hyphen ?

Arne
Sep 16 '07 #7
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:46******** *************** @news.sunsite.d k...
John Vottero wrote:
>"Arne Vajhøj" <ar**@vajhoej.d kwrote 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(yourencod ing.GetBytes(in putstring));

You don't need ToHex, use System.BitConve rter.ToString() .

Can BitConverter.To String omit the hyphen ?
No, but how about BitConverter.To String(array).R eplace('-', '');

or even (if you want it to look like "0x12, 0x23, 0x45"):

"0x" + BitConverter.To String(array).R eplace("-", ", 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.WriteLi ne(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.To String(array).R eplace('-', '');
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.d kwrote in message
news:46******** *************** @news.sunsite.d k...
John Vottero wrote:
>"Arne Vajhøj" <ar**@vajhoej.d kwrote 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(yourencod ing.GetBytes(in putstring));

You don't need ToHex, use System.BitConve rter.ToString() .

Can BitConverter.To String 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.To String 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.Enc oding.ASCII.Get String(myByteAr ray);

Sep 17 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1437
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 the current precision is variable. For instance, some numbers have 4 decimal places while others have 2, etc. (10.7435 vs 1074.35)... all numbers are of fixed length. I have some ideas of how to do this, but I'm wondering if there's a better...
1
3053
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 TextBox1_AfterUpdate() Dim MyString As String (or you could say Dim MyString As Variant) MyString = Textbox1.Text If MyString contains any of the following strings(terms); {Apt No., Or Apartment Number, Or Unit #} Then Msgbox("You cannot enter Apartment Numbers...
5
2332
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 02/28/2005 060208 02507M303 32,049.00 Is there a way to construct a query that will give me the difference by customer and CUSIP by month? The real app has over 200,000 records (12 months with 20,000+ entries per
12
2664
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: Nonbacktracking subexpression (also known as a "greedy" subexpression). The subexpression is fully matched once, and then does not participate piecemeal
17
4665
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 analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if( someString.ToLower() == "somestring" ) and
2
1550
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 be done by mapping string function names with functions themselves? Is there a better and direct way? Thanks in advance!
8
2685
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 returning null pointers.) It looks pretty watertight to me, but my version of lint complains about use of deallocated pointers, etc. Is this code completely safe on all input, or have I missed something? /* Header files included in the program...
6
1416
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 5 characters long? how do I insert spaces untill it is 10 characters long, so I can still use the string.insert at space number 11? I hope this made sense. thanks.
1
1799
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 samples will be sent to a lab for analysis and the lab will send back a report (usually in excel or .txt format), that data will be imported into Access. If the concentration of a heavy metal is lower than the detectable limit of the analysis, the...
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9388
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.