Should be a simple conversion!!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russ

    #1

    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!

  • Jon Skeet [C# MVP]

    #2
    Re: Should be a simple conversion!!

    Russ <russ.mcanulla@ sheldonreed.com > wrote:[color=blue]
    > 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![/color]

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

    char c = (char) int.Parse(origi nalString);

    --
    Jon Skeet - <skeet@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

    Comment

    • Russ

      #3
      Re: Should be a simple conversion!!

      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

      Comment

      • Divyesh

        #4
        Re: Should be a simple conversion!!

        > Is it always a single character? If so, use:[color=blue]
        >
        > char c = (char) int.Parse(origi nalString);
        >[/color]

        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]" <skeet@pobox.co m> wrote in message
        news:MPG.1e133d 2ffead07ac98cb7 0@msnews.micros oft.com...[color=blue]
        > Russ <russ.mcanulla@ sheldonreed.com > wrote:[color=green]
        > > 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![/color]
        >
        > Is it always a single character? If so, use:
        >
        > char c = (char) int.Parse(origi nalString);
        >
        > --
        > Jon Skeet - <skeet@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[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Should be a simple conversion!!

          Russ <russ.mcanulla@ sheldonreed.com > wrote:[color=blue]
          > 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?[/color]

          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 - <skeet@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

          Comment

          • Russ

            #6
            Re: Should be a simple conversion!!

            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

            Comment

            Working...