Hex To Decimal - help pls?

  • Thread starter Thread starter xlar54
  • Start date Start date
X

xlar54

Hey folks, I am in need of a hex to decimal conversion method, but
there are some ugly constraints:

1) All integer types have to be of type ushort (max size is $FFFF or
65535)
2) I can not use any family of the Convert class, Math class, or
System.Globalization.Numberstyles. Has to be all old-school.

(this isnt a homework assignment..) If someone could help, I would
appreciate it. Fought with this all evening.
 
Hey folks, I am in need of a hex to decimal conversion method, but
there are some ugly constraints:

1) All integer types have to be of type ushort (max size is $FFFF or
65535)
2) I can not use any family of the Convert class, Math class, or
System.Globalization.Numberstyles. Has to be all old-school.

(this isnt a homework assignment..) If someone could help, I would
appreciate it. Fought with this all evening.

Some code from the shelf (you will need to modify
it to use ushort yourself):

public class RadixFun
{
private static string DIGITS = "0123456789ABCDEF";
private static int FromAny(string s, int radix)
{
int res = 0;
char[] sa = s.ToCharArray();
int sign = 1;
for (int i = 0; i < s.Length; i++) {
if(sa != '-')
{
res = res * radix + DIGITS.IndexOf(sa);
}
else
{
sign = -1;
}
}
return sign * res;
}
private static string ToAny(int i, int radix)
{
if(i >= 0)
{
string res = "";
int tmp = i;
while (tmp > 0) {
res = DIGITS.ToCharArray()[tmp % radix] + res;
tmp = tmp / radix;
}
return res;
}
else
{
return "-" + ToAny(-i, radix);
}
}
public static int FromHex(string s)
{
return FromAny(s, 16);
}
public static string ToHex(int i)
{
return ToAny(i, 16);
}
}

Note that the code does not have good checks for invalid formats. If
that is needed then it also need to be added.

Arne
 
Hex to decimal conversion is trivial.  You can find umpteen examples via
Google.

Your #2 requirement seems bizarre to me.  What's the point of writing C#
code if you're going to do it with one hand tied behind your back?  :)

Pete

Im working with a system called COSMOS which is a managed OS written
in C#. (try googling it - very nice project actually). But they have
not created plugs yet for all the various portions of the .NET
framework, including the items you mention. Ive looked at various
implementations, but most of them rely on those things which I cant
yet use.

Thanks
 
Ah.  I see.

Well, Cosmos (not "COSMOS") is open-source.  So seems to me one approach
to your problem would be to write your own implementations for the
missing features you need, and contribute them to the project.  It's a
little more work, but in the long run the benefit is greatly magnified
as compared to simply copying some simply hex-to-decimal routine into
your own code.  A little more effort on your part (and really, it
wouldn't be _that_ much more effort) would make a big difference to the
Cosmos project.

Pete

Oh... I get it. :)

It would be _that_ much more effort, if one doesn't have relevant IL -
x86 assembly language experience, which I do not. Not sure that my
question requesting a code snippet warrants a passive-aggressive
lecture on "trying harder" and "helping out an open source project"
like COSMOS, er.. Cosmos (thanks for correcting me). But thank you
for your valuable assistance. We probably aren't the first two who
don't know how to do this without the spoon fed .NET classes.

Also, thanks to Arne for actually helping with code - works great!
I'm torn though on if I should send the Cosmos people a copy of this
code, or take Paul's advice and tell them to try harder and maybe they
will come up with a solution on their own.
 
It's well and good that Arne's offered some code that works for you, but a) you still don't necessarily know how you'd write something that simple from scratch yourself, and

Huh? My problem is solved. I dont care to reinvent the wheel. This
isnt an academic test I'm taking, and you're not my instructor. Step
down just a bit, brother.
That code looked great to me from a teaching point of view, but it's not the sort of thing one normally would find in production code.

You're doing a good bit of criticizing, but not offering much. Maybe
you have a better algorithm or code snippet (see original request).
Or maybe not. Maybe its a secret?
In any case, you might do well to remember the adage about not biting the hand that feeds you

I missed the part where you were feeding me. I believe I did thank
Arne though.
People who act like you have here, reacting
defensively and insultingly toward people who are just making an honest
effort to help often find that eventually no one is willing to bother
helping.  

Just "making an honest effort to help"? Really? Pete, you aren't
helping. Anyone can say "go figure it out yourself", and "try a
little harder". I suggest you be a little more honest, yourself. If
you dont know the answer, or dont want to help, then dont reply. Not
sure why this concept is so difficult for some folks. But it is the
Internet, so what can you do. I have no reason to even think you
could help.
Everyone can observe your behavior here, and many will choose to ignore requests from those who jump to false conclusions and make negative characterizations of those who are helping them.

Im sure there are some who will jump on your invisible bandwagon. Im
good with people ignoring me who consider help as "go try harder".

Anyway, this thread is over. Respond if you absolutely must have the
last word.
 
Back
Top