Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to convert string to asci and asci to hex

How to convert string to asci and asci to hex

Scheduled Pinned Locked Moved C#
tutorialquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pallaka
    wrote on last edited by
    #1

    I need to display data in Hexa. I will be geting data in asci which will be getting stored in string and i need to convert theis string to hexa and display it in textbox. Ex Receiving "50 3F 52 40" as string Need to covert in to hex as -> "P ? R @"

    R G L 3 Replies Last reply
    0
    • P pallaka

      I need to display data in Hexa. I will be geting data in asci which will be getting stored in string and i need to convert theis string to hexa and display it in textbox. Ex Receiving "50 3F 52 40" as string Need to covert in to hex as -> "P ? R @"

      R Offline
      R Offline
      riced
      wrote on last edited by
      #2

      Your example seems to do the opposite of what you want. Simplest solution might be to use String.Format. What have you tried? If you post code then you're more likely to get a helpful response. :)

      Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis

      P 1 Reply Last reply
      0
      • P pallaka

        I need to display data in Hexa. I will be geting data in asci which will be getting stored in string and i need to convert theis string to hexa and display it in textbox. Ex Receiving "50 3F 52 40" as string Need to covert in to hex as -> "P ? R @"

        G Offline
        G Offline
        gwithey
        wrote on last edited by
        #3

        Don't know if this will help http://www.testingreflections.com/node/view/5635[^]

        P 1 Reply Last reply
        0
        • R riced

          Your example seems to do the opposite of what you want. Simplest solution might be to use String.Format. What have you tried? If you post code then you're more likely to get a helpful response. :)

          Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis

          P Offline
          P Offline
          pallaka
          wrote on last edited by
          #4

          This is the code snip i am working on...kindly look and let me know the answer

          MessageBox.Show(this.listBox1.Text.ToString());
          this.txtToSend.Text = string.Format("{0:X}", "3F 50");

          i must get an output as "? P" in Hex

          R 1 Reply Last reply
          0
          • P pallaka

            This is the code snip i am working on...kindly look and let me know the answer

            MessageBox.Show(this.listBox1.Text.ToString());
            this.txtToSend.Text = string.Format("{0:X}", "3F 50");

            i must get an output as "? P" in Hex

            R Offline
            R Offline
            riced
            wrote on last edited by
            #5

            You seem to be confused about what Hex means. :) This is Hex (well a string of Hex characters):

            pallaka wrote:

            3F 50

            And this is (printable) ASCII or whatever the encoding is.

            pallaka wrote:

            ? P

            If you have a string of Hex characters separated by spaces (which is what your example shows) I suggest using Split on txtToSend.Text to get an array and then loop through the array to output the chars. You will need convert the array entries to numeric and then the numeric to char.

            Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis

            1 Reply Last reply
            0
            • G gwithey

              Don't know if this will help http://www.testingreflections.com/node/view/5635[^]

              P Offline
              P Offline
              pallaka
              wrote on last edited by
              #6

              It is doing opposite to me... i mean if i send "?" it is giving output as "3F" I agree i really confused...3F is my Hex value and i need to convert to asci I need fully opposite way..... I need to send "3F" and i should get result as "?"

              G 1 Reply Last reply
              0
              • P pallaka

                It is doing opposite to me... i mean if i send "?" it is giving output as "3F" I agree i really confused...3F is my Hex value and i need to convert to asci I need fully opposite way..... I need to send "3F" and i should get result as "?"

                G Offline
                G Offline
                gwithey
                wrote on last edited by
                #7

                Ah i think this will work then. When i try it prints out the correct ascii number label1.Text = Convert.ToInt32("3F", 16).ToString(); may not be the most efficient way.

                P 1 Reply Last reply
                0
                • P pallaka

                  I need to display data in Hexa. I will be geting data in asci which will be getting stored in string and i need to convert theis string to hexa and display it in textbox. Ex Receiving "50 3F 52 40" as string Need to covert in to hex as -> "P ? R @"

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi, The solution will take some 10 lines of code including an explicit loop. here are a few things you will need: 1. split the input string (which is a hex string) into parts, each holding 2 hex digits; you could use string.Split() or string.Substring() 2. convert a 2-digit hex string to its numeric value; you need int.Parse() or int.TryParse() with some options 3. convert that number to an ASCII character; you might try that with a (char) cast, the safer way would use an Encoding method. 4. concatenate all the results; either use a string operator or StringBuilder class. If all this is too much for you, this is what you should have done: http://lmgtfy.com/?q=convert+hex+string+to+ASCII[^] :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  1 Reply Last reply
                  0
                  • G gwithey

                    Ah i think this will work then. When i try it prints out the correct ascii number label1.Text = Convert.ToInt32("3F", 16).ToString(); may not be the most efficient way.

                    P Offline
                    P Offline
                    pallaka
                    wrote on last edited by
                    #9

                    It Prints the value 63. But i need "?" how can i convert 63 to symbol representation. You can have a look in www.asciitable.com

                    G 1 Reply Last reply
                    0
                    • P pallaka

                      It Prints the value 63. But i need "?" how can i convert 63 to symbol representation. You can have a look in www.asciitable.com

                      G Offline
                      G Offline
                      gwithey
                      wrote on last edited by
                      #10

                      I have just learnt something new to. This may work simple cast will convert the int (ascii) to a char / symbol.

                           int i = 63;
                           char c = (char)i;
                           label1.Text = c.ToString();
                      
                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups