473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1189
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: news.hku.hk | last post by:
can any one translate the following codes into c++ codes, coz i can't run the "printf" command in Unix and it said it's implicit declaration. Is it only the two lines with comment need to be...
5
by: Jazper | last post by:
hi can anybody explain that to me: i did the following: - 1. input of char 244 ("ô") into string - 2. convert to getbytes - 3. convert back to string char 244 changed to char 63 ("?")...
11
by: santosh | last post by:
Hello all, Conversion macros along the name of INT8_C, INT16_C etc, are defined in stdint.h to convert their argument into suitable representations for their corresponding types, i.e. int8_t,...
1
by: Rudy | last post by:
Hello all! I tried a few things, can't seem to figure out the right combination. here the snippet of CODE Dim cmdBet As SqlCommand Dim ConWg As New SqlConnection Try ConWg = New...
8
by: William Foster | last post by:
Good afternoon all, I am trying to convert a date to an integer in Microsoft Visual Basic 2003 .NET and am experiencing a little trouble trying to get it done. This is what I want to do: ...
0
by: Steve | last post by:
Microsoft has introduced a Persian Calendar Class in ASP.net 2.0 http://msdn2.microsoft.com/en-us/library/system.globalization.persiancalendar.aspx I have looked at the methods and the only way...
4
by: Phillip Vong | last post by:
VS2005 in VB.NET I have 2 simple textboxes (Textbox1, Textbox2) and they both have dates. Textbox1=12/31/2006 Textbox2=12/15/2006 I need to convert these two textboxes that are in String...
4
by: LoneHunter01 | last post by:
I need to be able to enter a string and make it go to all lowercase. The only way I think you can do that is by first converting into char's. I've seen alot of "string to char array" but no simple...
6
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.