473,700 Members | 2,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1205
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(origi nalString);

--
Jon Skeet - <sk***@pobox.co m>
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(origi nalString);

that will convert it from ASCII int to CHAR.
Try out this
char c = (char)int.Parse ("56",System.Gl obalization.Num berStyles.HexNu mber);

--
Rgds,
Divyesh
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... 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(origi nalString);

--
Jon Skeet - <sk***@pobox.co m>
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.He xNumber) (and cast to
char as before).

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

--
Jon Skeet - <sk***@pobox.co m>
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.Lengt h-1;i=i+2)
{
type = type + (char)int.Parse (hexstring.Subs tring(i,2),

NumberStyles.He xNumber);
}

(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
1594
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 re-written?? Thanks a lot void _display_number(int v, int n){ if(v >= 1000){ int r = v % 1000; _display_number(v / 1000,n); printf(",%03d",r); // how to translate that into
5
1360
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 ("?") why??? what can i do the get the same string...?
11
3161
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, int16_t etc. My questions are: 1. Should the conversion macros be used even when assigning from a variable of the same type?
1
1079
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 SqlConnection("Server=Localhost;uid=xx;pwd=xxxx;database=xxx") cmdBet = New SqlCommand("PlBetII", ConWg)
8
1866
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: Date_001 = Array_001(0) Date_002 = Array_001(1) If (Date_001 - Date_002) <= Array_002(i, 0) Then
0
2409
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 I have managed to convert a date from Persian to Gregorian or vice versa is to use the getyear, getmonth and getdayofmonth methods. ie. my code looks something like this: 'Get gregorian date and return persian date
4
13114
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 format into Date format and I'm not sure how. I tried this and I thought it would work, but I get the
4
2735
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 solution. How do you (or where can I find, though I've looked) an answer to the following question.... If I have a string, like string name = "stan"; How do I turn into a vector of chars so I can use tolower. BTW, I've seen lots of abstract...
6
2826
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
8641
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9203
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8958
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7797
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6557
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2379
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2021
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.