|
using System;
using System.Collections.Generic;
using System.Text;
namespace Utilities
{
public class MyHexConverter
{
/// <summary>
/// Helper array to speedup conversion
/// </summary>
static string[] BATHS = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF" };
static byte[] HSTB = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15 };
/// <summary>
/// Function converts byte array to it's hexadecimal implementation
/// </summary>
/// <param name="ArrayToConvert">Array to be converted</param>
/// <param name="Delimiter">Delimiter to be inserted between bytes</param>
/// <returns>String to represent given array</returns>
public static string ByteArrayToHexString(byte[] ArrayToConvert, string Delimiter)
{
int LengthRequired = ArrayToConvert.Length << 1 + Delimiter.Length * ArrayToConvert.Length + 1;
StringBuilder tempstr = new StringBuilder(LengthRequired, LengthRequired);
foreach (byte CurrentElem in ArrayToConvert)
{
tempstr.Append(BATHS[CurrentElem]);
tempstr.Append(Delimiter);
}
tempstr.Remove(tempstr.Length - 1, 1);
return tempstr.ToString();
}
/// <summary>
/// Function converts given hexadecimal string to it's binary representation
/// </summary>
/// <param name="StringToConvert">String to convert to byte array</param>
/// <returns>Byte array representing given string</returns>
public static byte[] PureHexStringToByteArray(string StringToConvert)
{
byte[] temparr = new byte[StringToConvert.Length / 2];
byte Char1, Char2;
for (int i = 0; i < StringToConvert.Length; i = i + 2)
{
Char1 = (Byte)StringToConvert[i];
Char2 = (Byte)StringToConvert[i + 1];
temparr[i / 2] = (Byte)((HSTB[(Char1 - 48)] << 4) + HSTB[(Char2 - 48)]);
}
return temparr;
}
}
}
|
|
|
|
|
static string[] BATHS = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF" };
public static string ByteArrayToHexString(byte[] ArrayToConvert, char Delimiter)
{
char[] list = new char[ArrayToConvert.Length * 3];
for (int k = 0; k < ArrayToConvert.Length; ++k)
{
list[k * 3 + 0] = BATHS[ArrayToConvert[k]][0];
list[k * 3 + 1] = BATHS[ArrayToConvert[k]][1];
list[k * 3 + 2] = Delimiter;
}
return new string(list);
}
|
|
|
|
|
Yes, it seem so. Just custom-length delimiter is not supported.
|
|
|
|
|
is that your HexEncoding will support big endian hex numbers?
for example, the header signature of JPG file is 0x04034b50 and HexEncoding will convert this as { 4,3,75,80 } - which is correct.
But, any way to provide a functionality to convert bigendian hex. It would be great thing to have in there.
|
|
|
|
|
http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.metadata.w3cxsd2001.soaphexbinary.aspx[^]
From MSDN
<br />
using System;<br />
using System.Runtime.Remoting.Metadata.W3cXsd2001;<br />
<br />
public class Demo<br />
{<br />
public static void Main(string[] args)<br />
{<br />
string xsdHexBinary = "3f789ABC";<br />
SoapHexBinary hexBinary = SoapHexBinary.Parse(xsdHexBinary);<br />
<br />
Console.WriteLine("The SoapHexBinary object in XSD format is {0}.",<br />
hexBinary.ToString());<br />
<br />
Console.WriteLine(<br />
"The XSD type of the SoapHexBinary object is {0}.",<br />
hexBinary.GetXsdType());<br />
<br />
Console.Write("hexBinary.Value contains:");<br />
for (int i = 0 ; i < hexBinary.Value.Length ; ++i)<br />
{<br />
Console.Write(" " + hexBinary.Value[i]);<br />
}<br />
Console.WriteLine();<br />
<br />
Console.WriteLine("The XSD type of the class SoapHexBinary is {0}.",<br />
SoapHexBinary.XsdType);<br />
}<br />
}
|
|
|
|
|
This is exactly what I was looking for. I was looking for something that was built into .NET versus writing custom code. The HexEncoding class also worked great btw.
www
|
|
|
|
|
Yep, SoapHexBinary handles all very smoothly. Have anybody made any perfomance test which method is faster?
|
|
|
|
|
Be wary of this if you're using it to convert user input. It discards all spaces, so if a user enters 1 44 a, they'll actually end up with 14 4a instead of 01 44 0a.
|
|
|
|
|
Didnt have to to do this pattern again - thanks!
Tim.Smith@Venturality.com
|
|
|
|
|
Thanks a lot you did lots of work for me..
very simple and very usuful..
thanks again..
sinan
|
|
|
|
|
I am trying to convert '3e5a43e6'H to string and I get the converted value as ">ZCae" but the right value is ">ZC." . For some reason '.' is not properly converted. Here is another example '0a001351'H where in there are 3 '.' and its equivalent is "...Q" but the above conversion routine converts it to something else. Help appreciated.
|
|
|
|
|
<code>
public string IntToHex(int number)
{
return String.Format("{0:x}", number);
}
</code>
|
|
|
|
|
The hex string is limited to the MaxValue of int
|
|
|
|
|
hi there iam astudent in IT department i wanna ask you afavor i ha ve an assinment about mulithreaded applications i want you send me ideas about this problem.and i have also an assinment in image processing (spatial filter ,smooth filter,segmentation of an image)please reply me as fast as you can...
thanx alot.
|
|
|
|
|
Hi,
public static bool IsHexDigit(Char c)
{
int numChar;
int numA = Convert.ToInt32('A');
int num1 = Convert.ToInt32('0');
c = Char.ToUpper(c);
numChar = Convert.ToInt32(c);
if (numChar >= numA && numChar < (numA + 6))
return true;
if (numChar >= num1 && numChar < (num1 + 10))
return true;
return false;
}
Can be replaced with:
public static bool System.Uri.IsHexDigit(Char character);
There are some useful static methods in the Uri class for dealing with hex strings.
Shaun Brown, MCAD, MCSD
|
|
|
|
|
Thanks to everyone who have posted performance tips, and all the positive feedback.
I have not updated the article with the speed-ups suggested, because the code people have commented with is quite clear if someone needs it.
I do often use the StringBuilder class, and it should be used if you are converting a large amount of data, but left it out of the example for clarity.
|
|
|
|
|
This may not be the 'best' way to do it, but I was able to drop the class in my code and using your example I was converting numbers in just a few minutes. Hey... no thinking required , just what I needed.
Thanks,
Joe
More stuff at www.smileymicros.com
|
|
|
|
|
Great work neilck! I really appreciate you putting up your work for us.
Thanks again!
|
|
|
|
|
If someone really looks at performance, this is the fastest method I found on the net
public static string ToHexString(byte[] bytes)
{
char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
char[] chars = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes[i];
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
return new string(chars);
}
|
|
|
|
|
Stotti, you have definitely found the most efficient method. Using a fixed char array is much faster than StringBuilder!
|
|
|
|
|
Thank you for the code, it is great.
Here is a small element you could add to improve the speed,
if you do call it very often.
The is my function to transfert from byte[] to hex string.
It is about 4x faster.
Don't worry, it does not come from me I found the pseudo code on the net.
///
/// Convert the content of a byte array to an hexadecimal string.
///
/// <param name="bytes" />array of bytes to convert
/// <returns>resulting string
static public string ConvertToHexaDecimal(byte[] bytes) {
System.Text.StringBuilder buffer = new System.Text.StringBuilder();
int length = bytes.Length;
for(int i = 0; i < length; i++) {
buffer.Append(S_tableau[bytes[i] >> 4]);
buffer.Append(S_tableau[bytes[i] & 15]);
}
return buffer.ToString();
}
private static char[] S_tableau = new char[]{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
Thank you again for the article,
Chris
|
|
|
|
|
this is what i looking for! i want user to input a string (MAC address ) which is in hexadecimal and stored it in an array of type byte and it works with your program. you have been solved my problems!
|
|
|
|
|
//no need code "discarded++" or "discarded--"
discarded = hexString.Length - newString.Length;
//next code to get hexstring from byte[]
hexString = BitConverter.ToString(bytes);
hexString = hexString.Replace("-", "");
|
|
|
|
|
This articles had saves my time of coding the hex to bytes. Thanks again I love the stringbuilder part...
|
|
|
|
|
Use the StringBuilder class to improve performance
change for
discarded = 0;
string newString = null;
StringBuilder sb = new StringBuilder();
char c;
// remove all none A-F, 0-9, characters
for (int i=0; i
|
|
|
|
|