Hex char / string to Long

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hi,
It must be easy but I can't see how to this is done.
I mean there will be a plethora of built in methods / properties DEVOTED to
this fundmental need
just waiting to be invoked right?
It must be simple to find. Right in your face so to speak because NOBODY
would have left this out of the latest and greatest framework
implementation.
Then why am I floundering around the net looking for this info?
So OK I give in.
Could someone please point me at the elusive
char.Tohex and string.Tohex functions or do they only exist in my caffeine
addled brain?
What year is this?
Are we still writing hex to decimal code snippets??
Thanks
Bob
 
It must be easy but I can't see how to this is done.
OK.

I mean there will be a plethora of built in methods / properties DEVOTED
to
this fundmental need just waiting to be invoked right?

Not sure if there are a plethora, but there is at least one...
It must be simple to find. Right in your face so to speak because NOBODY
would have left this out of the latest and greatest framework
implementation.

That's right.
Then why am I floundering around the net looking for this info?

Have you tried reading the help?
So OK I give in.
Could someone please point me at the elusive
char.Tohex and string.Tohex functions or do they only exist in my caffeine
addled brain?

string strHex = "FF";
long lngHex = System.Convert.ToInt64(strHex, 16);
What year is this?

It's 2005, for the next couple of months or so...
Are we still writing hex to decimal code snippets??

No.
 
Umm... your message has a lot of flowery prose talking *around* your
problem, and only a couple word actually describing it. Now, let's try to
get the details.

Are you to convert a string of characters (0-9A-F) into it's equivalent
byte/byte array/int
Or to convert a byte/byte array/int into a string of hex characters?

string sHex ="123ABC";
int i = Int32.Parse(sHex,
System.Globalization.NumberStyles.HexNumber);
byte b = Convert.ToByte(sHex.Substring(0,2));

int iHex = 0x123ABC;
string s = iHex.ToString("X");




--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
try

string hexNum = "0ff";
int num = System.Int32.Parse(hexNum,

System.Globalization.NumberStyles.AllowHexSpecifier);

(elapsed search time, 2 mins 37 secs.)

hth,
Alan.
 
Hi All,
Thanks
for your replies;
My main gripe is that such a fundamental need is poorly documented and
implemented. (IMHO)
Try looking up hex in the C# help and see what I mean.
Try looking up 'Convert hex' also. The VB school is adequately served, the
C# ...?
Ever tried
? long.Parse("0xF") in the immediate window?
No Go.
Why ?
Because you are missing the 'well known'
System.Globalization.NumberStyles.HexNumber parameter
(Thanks James)
And even that doesn't work!
Why? because in this situation you leave out the hex specifier!
Fool. Why did you not know that?

I don't mind spending time researching the more esoteric features of C# when
needed, but I begrudge every second spent on something that should be drop
dead simple.
Well, enough said. I'll grab another cup of coffee and rumble back into my
cave.
Thanks again.
Bob
 
I don't mind spending time researching the more esoteric features of C#
when
needed, but I begrudge every second spent on something that should be drop
dead simple.

BTW, Bob, that's not C#; it's the .Net Framework CLR, the same for any
language.

And it *is* drop-dead simple to do it. It's just not drop-dead simple to
find the reference to it. The CLR is huge. I spend at least an hour a day in
the MSDN Library, and have been for many years. Still, I don't feel like
I've read that much of it.

Of course, that's why we have communities like this! :-D

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
Back
Top