Вы находитесь на странице: 1из 4

There is a built in method for this:

byte[] data = { 1, 2, 4, 8, 16, 32 };


string hex = BitConverter.ToString(data);
Result: 01-02-04-08-10-20
If you want it without the dashes, just remove them:
string hex = BitConverter.ToString(data).Replace("-", string.Empty);
Result: 010204081020
If you want a more compact representation, you can use Base64:
string base64 = Convert.ToBase64String(data);
Result: AQIECBAg
Added By OP My test show the fastest is http://stackoverflow.com/a/18574846/3453
7
There is a built in method for this:
byte[] data = { 1, 2, 4, 8, 16, 32 };
string hex = BitConverter.ToString(data);
Result: 01-02-04-08-10-20
If you want it without the dashes, just remove them:
string hex = BitConverter.ToString(data).Replace("-", string.Empty);
Result: 010204081020
If you want a more compact representation, you can use Base64:
string base64 = Convert.ToBase64String(data);
Result: AQIECBAg
14
down vote
accepted
First you'll need to get it into a byte[], so do this:
byte[] ba = Encoding.Default.GetBytes("sample");
and then you can get the string:
var hexString = BitConverter.ToString(ba);
now, that's going to return a string with dashes (-) in it so you can then simpl
y use this:
hexString = hexString.Replace("-", "");
to get rid of those if you want.


Can anyone tell me how to convert a string to a hex number
Sign in to vote
Here's one way:
Code Snippet
string text = "AbcdefG";
char[] chars = text.ToCharArray();
StringBuilder stringBuilder = new StringBuilder();
foreach(char c in chars)
{
stringBuilder.Append(((Int16)c).ToString("x"));
}
String textAsHex = stringBuilder.ToString();
Console.WriteLine(textAsHex);


If what you're asking is to convert a textual representation of a number to hex:
Code Snippet
string text = "10";
Int32 number;
if(Int32.TryParse(text, out number))
{
String textAsHex = number.ToString("x");
Console.WriteLine(textAsHex);
}
else
{
// TODO: Error processing
}


Thursday, August 09, 2007 1:10 AM
Reply | Quote |
Avatar of Peter Ritchie
Peter Ritchie (MVP) 37,625 Points
Question
Sign in to vote
0
Sign in to vote
Try this How to convert string to hexadecimal and vice versa

Thursday, August 09, 2007 2:33 PM
Reply | Quote |
Avatar of Karthik9
Karthik94,535 Points
All replies
Question
Sign in to vote
0
Sign in to vote
Please clarify your question by providing sample input and output data. Do you
want as output a string that is the concatenation of the hexadecimal representat
ion of each of the ascii character values?
Wednesday, August 08, 2007 4:58 PM
Reply | Quote |
Avatar of sirjis
sirjis1,725 Points
Question
Sign in to vote
0
Sign in to vote
Well, without a few more details about what you want to do exactly, it is going
to be a wild shot.
Anyway if my guess is right and you just want a string converted to a string con
taining the corresponding hex values, you could use:

string myString = "Hello World";
string myHexString = BitConverter.ToString (Encoding.ASCII.GetBytes (myString));

// this will return "48-65-6C-6F-20-57-6F-72-6C-64"

This example assumes your string contains ASCII characters, simply change the En
coding to suite your needs.
HTH
--mc
Wednesday, August 08, 2007 4:58 PM
Reply | Quote |
Avatar of Mario Cossi
Mario CossiQi technologies15,375 Points
Question
Sign in to vote
0
Sign in to vote
A number is just a number, your question is vague, do you mean
1) you have a string like this "10" and you want a string like this "A"
2) you have a string like this "10" and you want it to be turned into an integer
or something
3) you have a stirng like this "A4C" and you want to turn it into a decimal inte
ger
4) you have a stiring like this "Hello there" and you want each character's asci
i or unicode value represented as a hex string
Some of those are a bit of a stretch, but I think you get my point, could you tr
y explaining your problem again
-mwalts
Wednesday, August 08, 2007 5:29 PM
Reply | Quote |
Avatar of mwalts
mwalts695 Points
Question
Sign in to vote
0
Sign in to vote
Here's one way:
Code Snippet
string text = "AbcdefG";
char[] chars = text.ToCharArray();
StringBuilder stringBuilder = new StringBuilder();
foreach(char c in chars)
{
stringBuilder.Append(((Int16)c).ToString("x"));
}
String textAsHex = stringBuilder.ToString();
Console.WriteLine(textAsHex);


If what you're asking is to convert a textual representation of a number to hex:
Code Snippet
string text = "10";
Int32 number;
if(Int32.TryParse(text, out number))
{
String textAsHex = number.ToString("x");
Console.WriteLine(textAsHex);
}
else
{
// TODO: Error processing
}

Вам также может понравиться