The number is bigger than int
& long
but can be accomodated in Decimal
. But the normal ToString
or Convert
methods don't work on Decimal
.
views:
3125answers:
4
+1
A:
I've got to agree with James - do it manually - but don't use base-16. Use base 2^32, and print 8 hex digits at a time.
MSalters
2008-10-21 08:33:22
+1
A:
I guess one option would be to keep taking chunks off it, and converting individual chunks? A bit of mod/division etc, converting individual fragments...
So: what hex value do you expect?
Here's two approaches... one uses the binary structure of decimal; one does it manually. In reality, you might want to have a test: if bits[3] is zero, do it the quick way, otherwise do it manually.
decimal d = 588063595292424954445828M;
int[] bits = decimal.GetBits(d);
if (bits[3] != 0) throw new InvalidOperationException("Only +ve integers supported!");
string s = Convert.ToString(bits[2], 16).PadLeft(8,'0') // high
+ Convert.ToString(bits[1], 16).PadLeft(8, '0') // middle
+ Convert.ToString(bits[0], 16).PadLeft(8, '0'); // low
Console.WriteLine(s);
/* or Jon's much tidier: string.Format("{0:x8}{1:x8}{2:x8}",
(uint)bits[2], (uint)bits[1], (uint)bits[0]); */
const decimal chunk = (decimal)(1 << 16);
StringBuilder sb = new StringBuilder();
while (d > 0)
{
int fragment = (int) (d % chunk);
sb.Insert(0, Convert.ToString(fragment, 16).PadLeft(4, '0'));
d -= fragment;
d /= chunk;
}
Console.WriteLine(sb);
Marc Gravell
2008-10-21 08:39:45
I expect the value : 7c86fdcb000000000004
2008-10-21 08:47:34
Cool, that's what I get ;-p
Marc Gravell
2008-10-21 08:55:27
Marc: We clearly think alike yet again, although I'd like to think my formatting code is somewhat simpler - if slower. I wonder how easy it would be to fix the "failing with some integers" problem. We really need a Normalize method in decimal :(
Jon Skeet
2008-10-21 08:57:04
Normalize... well, changing the scale (via GetBits()) is easy enough - just need to check that it is an integer afterwads (or use Floor/Ceiling)... if only you had some kind of general purpose library that might host such a method...
Marc Gravell
2008-10-21 09:07:56
Thanks a lot.... it worked for me..
2008-10-21 10:28:50
+2
A:
I believe this will produce the right results where it returns anything, but may reject valid integers. I dare say that can be worked around with a bit of effort though... (Oh, and it will also fail for negative numbers at the moment.)
static string ConvertToHex(decimal d)
{
int[] bits = decimal.GetBits(d);
if (bits[3] != 0) // Sign and exponent
{
throw new ArgumentException();
}
return string.Format("{0:x8}{1:x8}{2:x8}",
(uint)bits[2], (uint)bits[1], (uint)bits[0]);
}
Jon Skeet
2008-10-21 08:55:20
Well, I didn't have my handy crib-sheet to hand! It isn't just negatives - it could fail for any integer with a non-zero scale, such as 100.00M. Re thinking alike, I imagine that protocol-buffers has a lot to do with it in this case ;-p
Marc Gravell
2008-10-21 09:02:21
I didn't have my crib-sheet to hand either, but I guessed until it worked :)I'll have to think about a normalize method for decimal. It shouldn't be *that* hard...
Jon Skeet
2008-10-21 09:17:08