A
Analizer1
Hi I have a Long Number
In a Byte[]
Im not sure how to convert this Byte[] array to a Hex
string
thanks
In a Byte[]
Im not sure how to convert this Byte[] array to a Hex
string
thanks
Hi I have a Long Number
In a Byte[]
Im not sure how to convert this Byte[] array to a Hex
string
Hi I have a Long Number
In a Byte[]
Im not sure how to convert this Byte[] array to a Hex
string
Hi I have a Long Number
In a Byte[]
Im not sure how to convert this Byte[] array to a Hex
string
Well, the .NET way would be to use BitConverter to get the bytes into an
actual long value, and then use ToString("x") to convert to hex:
byte[] rgb = { 1, 2, 3, 4, 5, 6, 7, 8 };
string strHex = BitConverter.ToInt64(rgb, 0).ToString("x");
Pete
Rene said:You can also call the ToString() directly in the byte variable:
byte[] ba = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 255 };
foreach(byte b in ba)
{
string hex = b.ToString("X");
Console.WriteLine( (hex.Length == 1 ? "0" : "") + hex );
}
[...]Hi I have a Long Number
In a Byte[]
Or as simple as strHex = BitConverter.ToString(rgb);
It depends on how you want to present the result, - this one puts a
whitespace between each hex pair, - great when debugging.
You can also call the ToString() directly in the byte variable:
byte[] ba = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 255 };
foreach(byte b in ba)
{
string hex = b.ToString("X");
Console.WriteLine( (hex.Length == 1 ? "0" : "") + hex );
}
Console.Read();
Hi I have a Long Number
In a Byte[]
Im not sure how to convert this Byte[] array to a Hex
string
Analizer1 said:My Byte[] array consists of DirectoryServices Users Sid from the domain
I needed to Convert Each Byte to a Hex String , Then Concatenate each
following byte
below is a example...is the proper way, or is there a better way of
accomplishing this
uint uDec;
for (int x = 0; x < aBytes.GetLength(0); x++)
{
uDec = Convert.ToUInt32(aBytes[x]);
sHexId.Append(String.Format("{0:x2}",uDec));
}
Console.WriteLine(sHexId);
My Byte[] array consists of DirectoryServices Users Sid from the domain
I needed to Convert Each Byte to a Hex String , Then Concatenate each
following byte
below is a example...is the proper way, or is there a better way of
accomplishing this
uint uDec;
for (int x = 0; x < aBytes.GetLength(0); x++)
{
uDec = Convert.ToUInt32(aBytes[x]);
sHexId.Append(String.Format("{0:x2}",uDec));
}
Console.WriteLine(sHexId);
//it matches the hex id Sql Returns
This works, but it's horribly inefficient. Of course, as long as you don'tAnalizer1 said:My Byte[] array consists of DirectoryServices Users Sid from the domain
I needed to Convert Each Byte to a Hex String , Then Concatenate each
following byte
below is a example...is the proper way, or is there a better way of
accomplishing this
uint uDec;
for (int x = 0; x < aBytes.GetLength(0); x++)
{
uDec = Convert.ToUInt32(aBytes[x]);
sHexId.Append(String.Format("{0:x2}",uDec));
}
Console.WriteLine(sHexId);
You can also call the ToString() directly in the byte variable:
byte[] ba = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 255 };
foreach(byte b in ba)
{
string hex = b.ToString("X");
Console.WriteLine( (hex.Length == 1 ? "0" : "") + hex );
}
Console.Read();
Jeroen Mostert said:This works, but it's horribly inefficient. Of course, as long as you don'tAnalizer1 said:My Byte[] array consists of DirectoryServices Users Sid from the domain
I needed to Convert Each Byte to a Hex String , Then Concatenate each
following byte
below is a example...is the proper way, or is there a better way of
accomplishing this
uint uDec;
for (int x = 0; x < aBytes.GetLength(0); x++)
{
uDec = Convert.ToUInt32(aBytes[x]);
sHexId.Append(String.Format("{0:x2}",uDec));
}
Console.WriteLine(sHexId);
need to do it a few hundred times every second, it doesn't matter. And
even if you do, this function probably won't be the bottleneck. Still, if
you do need this to be fast...
const string hexDigits = "0123456789abcdef";
int bl = aBytes.Length;
char[] result = new char[bl * 2];
for (int n = 0; n != bl; ++n) {
result[n * 2 + 0] = hexDigits[aBytes[n] / 16];
result[n * 2 + 1] = hexDigits[aBytes[n] % 16];
}
Console.WriteLine(new string(result));
This approach is over 50 times faster than yours. If you don't like to
spend that much keystrokes on it, even this oneliner is better (better
still than doing it yourself with a StringBuilder):
Console.WriteLine(BitConverter.ToString(aBytes).Replace("-","");
Avoid String.Format() and its relatives in tight loops. If you need to do
positional replacement or locale-dependent formatting they're well worth
it (it's much more readable than gluing strings together yourself) but for
trivial conversions like these, repeated in a loop, they're not good.
Analizer1 said:this was my 1st Run at this type of conversion...
so your remarks are welcome...
I Want The Fastest and Tightest Conversion Possible
Why, out of interest? Just how many of these are you doing? Do you have
specific performance goals in mind? If you're getting the data from
directory services, that's likely to be much, much slower than any of
the conversion mechanisms given here.
ah, I didn't paid attention to the "Hi I have a *Long *Number"..... such
a
small oversight
I thought that all he wanted to do was to show hex of a byte array.
This is not the first time nor the last time I will make such mistakes!!
I know I was being evil by talking about fast code for that (since peopleAgreed. Jeroen's version is an interesting exercise, but it should
almost never be the case that one would actually need code written like
that.
Now here's where I'm slightly offended at the suggestion of producingWriting code that's comparatively difficult to understand like that is
only something you should do once you've tried a readable version and
found it to be _the_ bottleneck performance-wise.
[...]
I know I was being evil by talking about fast code for that (since
people get obsessed with "the fastest code" too easily), but I couldn't
help myself. I did point out that it's highly unlikely that formatting
hexstrings would be a bottleneck for anything.
Now here's where I'm slightly offended at the suggestion of producing
unreadable code. Most C programmers would agree that my version is a
paragon of readability. :-D
Also, my comment on not using String.Format() in loops if it's obvious
to avoid still stands (and in this case, using BitConverter is pretty
obvious, and fast to boot as a bonus). Extensive string manipulation
*can* become a bottleneck fairly quickly, or even just a noticeable
drag, and StringBuilder isn't a cure-all for string performance problems.