473,609 Members | 2,263 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 1198
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
1592
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
1357
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
3157
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
1078
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
1863
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
2406
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
13107
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
2732
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
2816
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
8121
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8559
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...
0
8519
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6987
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...
0
5506
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();...
0
4010
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4068
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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.