471,317 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Should be a simple conversion!!

Hi guys & gals
I need to convert, in C#, a decimal hex string into an ascii string
(ie "56" -> "V"), and I can't find anything in the vs2003 help files
that is remotely helpful. Any suggestions that will save me writing my
own convert function would be gratefully received!

Dec 21 '05 #1
5 1120
Russ <ru***********@sheldonreed.com> wrote:
Hi guys & gals
I need to convert, in C#, a decimal hex string into an ascii string
(ie "56" -> "V"), and I can't find anything in the vs2003 help files
that is remotely helpful. Any suggestions that will save me writing my
own convert function would be gratefully received!


Is it always a single character? If so, use:

char c = (char) int.Parse(originalString);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #2
Hi Jon,
No, this doesn't work I'm afraid. It converts 56 to "8" rather than
"V", so it is obviously thinking that the 56 is decimal, not hex. Any
ideas please?
Russ

Dec 21 '05 #3
> Is it always a single character? If so, use:

char c = (char) int.Parse(originalString);

that will convert it from ASCII int to CHAR.
Try out this
char c = (char)int.Parse("56",System.Globalization.NumberSt yles.HexNumber);

--
Rgds,
Divyesh
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Russ <ru***********@sheldonreed.com> wrote:
Hi guys & gals
I need to convert, in C#, a decimal hex string into an ascii string
(ie "56" -> "V"), and I can't find anything in the vs2003 help files
that is remotely helpful. Any suggestions that will save me writing my
own convert function would be gratefully received!


Is it always a single character? If so, use:

char c = (char) int.Parse(originalString);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 21 '05 #4
Russ <ru***********@sheldonreed.com> wrote:
No, this doesn't work I'm afraid. It converts 56 to "8" rather than
"V", so it is obviously thinking that the 56 is decimal, not hex. Any
ideas please?


Ah, I was confused by your use of "decimal hex string" in the original
post. I didn't see the "hex" bit, just the "decimal" bit (and didn't
check the code for 'V').

In that case, use int.Parse(text, NumberStyles.HexNumber) (and cast to
char as before).

Alternatively, use Convert.ToInt32(text, 16) (and cast).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #5
Thanks guys for your virtually identical solutions. This worked well
I ended up putting it into a loop to get the whole string thus :-

string type = "";
for(int i=0;i<= hexstring.Length-1;i=i+2)
{
type = type + (char)int.Parse(hexstring.Substring(i,2),

NumberStyles.HexNumber);
}

(And, yes, I know that I should have used stringbuilder, but I will
tidy up later!!)
Russ

Dec 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by news.hku.hk | last post: by
11 posts views Thread by santosh | last post: by
1 post views Thread by Rudy | last post: by
8 posts views Thread by William Foster | last post: by
4 posts views Thread by Phillip Vong | last post: by
6 posts views Thread by Lasse Reichstein Nielsen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.