how to convert a string of hex chars to ascii, including nulls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aatif
    New Member
    • Mar 2009
    • 28

    how to convert a string of hex chars to ascii, including nulls

    I want to convert a string of hex characters (2 hex chars = 1 byte), to ASCII. Hex chars include zeros (0x00) as well, which I want to include in ASCII string.

    hex string: 5000005355....
    ASCII: P<null><null>SU ...

    I can do it and the string length also includes nulls but when I concatenate other string, it doesn't show as its part.


    Code:
    string HexValue = "500000535500";
    string StrValue = "";
    while (HexValue.Length > 0)
    {
        StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
        HexValue = HexValue.Substring(2, HexValue.Length - 2);
    }
    StrValue += "hello";
    textASCII.Text = StrValue;
    MessageBox.Show(StrValue.Length.ToString());

    Or .. is there some problem with the text field in which I am displaying the result?
    Actually, I need to send the string containing nulls through socket.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Put a breakpoint in after the concatenate and check the actual value of your variable. I wouldn't be surprises if it was complete but displaying wrong. Strings often don't deal well with having nulls in them. Null termination is fairly standard so when a control reaches the null it may just stop looking.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Why would you convert it to a string if you were going to send it on a socket?

      Comment

      • aatif
        New Member
        • Mar 2009
        • 28

        #4
        Originally posted by tlhintoq
        Put a breakpoint in after the concatenate and check the actual value of your variable. I wouldn't be surprises if it was complete but displaying wrong. Strings often don't deal well with having nulls in them. Null termination is fairly standard so when a control reaches the null it may just stop looking.
        In the tooltip, it shows the string with nulls and "hello" at the end, but in 'quick watch' window, it shows only characters before first '\0' and no "hello"...

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Well tlhintoq really WAS right, \0 is not a valid character in an ASCII string and is used for termination. Its just part of the language specs.
          If you really want to see the whole string, look at it in hex

          Comment

          • aatif
            New Member
            • Mar 2009
            • 28

            #6
            Originally posted by Plater
            Why would you convert it to a string if you were going to send it on a socket?
            I have to convert because I want it to be read on the receiver end as it is in hex at my end, if I send it in hex form, it shows up as numerics on the other end.

            If I send 5000005355, it becomes 353030303030353 33535, i-e '5' converted to ASCII (0x35), and so on ...

            But I want to send 5000005355 as 5000005355, which is only possible by converting them in ASCII form, 'P' would be taken as 0x50, <null> as 0x00 and so on ...

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              You could convert them to bytes and send them as bytes.
              Sockets use bytes to send the data, you would have to convert the string to bytes to send it anyway

              Comment

              • aatif
                New Member
                • Mar 2009
                • 28

                #8
                Originally posted by Plater
                You could convert them to bytes and send them as bytes.
                Sockets use bytes to send the data, you would have to convert the string to bytes to send it anyway
                Convert the hex string to bytes or the ASCII? How?

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  so you have a literal string such as
                  string s = "5000005355 " ?
                  How do you get this string?
                  I mean byte b= 0x50 is the first byte and
                  byte[] ba = new byte[]{0x50, 0x00, 0x00, 0x53, 0x55}; would be the byte array of it

                  Comment

                  • aatif
                    New Member
                    • Mar 2009
                    • 28

                    #10
                    Originally posted by Plater
                    so you have a literal string such as
                    string s = "5000005355 " ?
                    How do you get this string?
                    I mean byte b= 0x50 is the first byte and
                    byte[] ba = new byte[]{0x50, 0x00, 0x00, 0x53, 0x55}; would be the byte array of it
                    The hex string is created at runtime so I cannot assign byte array like this.

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      So someone types into a box "5000005355 " or something?
                      And even if created at runtime, you can still parse into byte[]

                      Comment

                      • vekipeki
                        Recognized Expert New Member
                        • Nov 2007
                        • 229

                        #12
                        Check this link: http://www.thinksharp.org/hex-string...ray-converter/

                        Comment

                        • aatif
                          New Member
                          • Mar 2009
                          • 28

                          #13
                          Originally posted by vekipeki
                          Thanks! ... to all for their quick response.

                          Comment

                          Working...