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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Convert the string to ASCII(HEX)

Convert the string to ASCII(HEX)

Scheduled Pinned Locked Moved C#
help
4 Posts 2 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.
  • M Offline
    M Offline
    merh
    wrote on last edited by
    #1

    Hi, I need help how I can convert the string below to ASCII(HEX) for (int i = 0; i<=5; i++) { int counter = 0x0 + i; string destinationAdress += (char)s[counter]; ... } Thanks in advaced merh

    L 1 Reply Last reply
    0
    • M merh

      Hi, I need help how I can convert the string below to ASCII(HEX) for (int i = 0; i<=5; i++) { int counter = 0x0 + i; string destinationAdress += (char)s[counter]; ... } Thanks in advaced merh

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

      merh wrote:

      for (int i = 0; i<=5; i++) { int counter = 0x0 + i; string destinationAdress += (char)s[counter]; ... }

      What is all this? 0x0 is exactly the same as 0 o+i is exactly the same as i you did not tell what s is if s is a string, then s[counter] is a char and does not need a (char) cast if s is an array of ints (just a guess), then you want some conversion from int to char. do you? I think you better explain a bit more what it is you are after. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        merh wrote:

        for (int i = 0; i<=5; i++) { int counter = 0x0 + i; string destinationAdress += (char)s[counter]; ... }

        What is all this? 0x0 is exactly the same as 0 o+i is exactly the same as i you did not tell what s is if s is a string, then s[counter] is a char and does not need a (char) cast if s is an array of ints (just a guess), then you want some conversion from int to char. do you? I think you better explain a bit more what it is you are after. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


        M Offline
        M Offline
        merh
        wrote on last edited by
        #3

        thanks I have found the solution. Explanation "s" is a byte array and 0x0 is the first element int counter = 0x0 + i; string Number = s[counter].ToString(); int number = int.Parse(Number); destinationAdress += number.ToString("x2");

        L 1 Reply Last reply
        0
        • M merh

          thanks I have found the solution. Explanation "s" is a byte array and 0x0 is the first element int counter = 0x0 + i; string Number = s[counter].ToString(); int number = int.Parse(Number); destinationAdress += number.ToString("x2");

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

          Hi, your code has too many ToString/Parse calls. To convert a byte to an int, you don't need these!

          byte[] s = ...;
          string destinationAdress = "";
          for (int i = 0; i<6; i++) {
          byte b = s[i];
          destinationAdress += b.ToString("x2");
          }

          :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


          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