locked
Check string is of type byte RRS feed

  • Question

  • Hello,

    How can I check whether string (9B-8D-08-84-00-00-01-37-00-01-02-00-0A-64-64-0A-00-00-00-00-B9-D3-25-69-34-F6-01-96-01-D6-9B-0E-78-9E-06-9B-00-00-00-00-00-00-00-5A or Hello) is of type byte or not and If it is of type byte then convert to string ?

    Tuesday, October 6, 2020 4:05 PM

Answers

  • Hi Tech Aspirant,

    Thank you for posting here.

    The string from 9B to 5A is like converting an ordinary string into a byte array and then into a hexadecimal string.

        string str = "HelloWorld";
        byte[] bytes = Encoding.ASCII.GetBytes(str);
        string res = BitConverter.ToString(bytes);


    Result: 48-65-6C-6C-6F-57-6F-72-6C-64

    If you want to convert it to the original string, you can reverse the process.

      byte[] bytes1 = StringToByteArray(res);
      string resultString = Encoding.ASCII.GetString(bytes1);
    
      **********************
    
     public static byte[] StringToByteArray(String hex)
     {
          int NumberChars = hex.Length;
          byte[] bytes = new byte[NumberChars / 2];
          for (int i = 0; i < NumberChars; i += 2)
              bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
          return bytes;
      }

    But there is a question. In this example, I used Encoding.ASCII to convert the string to a byte array, so we can use the same encoding to convert the byte array back.

    But we don't know what encoding format is used in your example, this needs to be confirmed by the provider of this string.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Tech Aspirant Monday, November 9, 2020 2:29 PM
    Wednesday, October 7, 2020 9:53 AM

All replies

  • Hi Tech Aspirant,

    Thank you for posting here.

    The string from 9B to 5A is like converting an ordinary string into a byte array and then into a hexadecimal string.

        string str = "HelloWorld";
        byte[] bytes = Encoding.ASCII.GetBytes(str);
        string res = BitConverter.ToString(bytes);


    Result: 48-65-6C-6C-6F-57-6F-72-6C-64

    If you want to convert it to the original string, you can reverse the process.

      byte[] bytes1 = StringToByteArray(res);
      string resultString = Encoding.ASCII.GetString(bytes1);
    
      **********************
    
     public static byte[] StringToByteArray(String hex)
     {
          int NumberChars = hex.Length;
          byte[] bytes = new byte[NumberChars / 2];
          for (int i = 0; i < NumberChars; i += 2)
              bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
          return bytes;
      }

    But there is a question. In this example, I used Encoding.ASCII to convert the string to a byte array, so we can use the same encoding to convert the byte array back.

    But we don't know what encoding format is used in your example, this needs to be confirmed by the provider of this string.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Tech Aspirant Monday, November 9, 2020 2:29 PM
    Wednesday, October 7, 2020 9:53 AM
  • My Input string can be either "Hello" or "9B-8D-08-84-00-00-01-37-00-01-02-00-0A-64-64-0A-00-00-00-00-B9-D3-25-69-34-F6-01-96-01-D6-9B-0E-78-9E-06-9B-00-00-00-00-00-00-00-5A"

    Before converting to string wanted to check "9B-8D-08-84-00-00-01-37-00-01-02-00-0A-64-64-0A-00-00-00-00-B9-D3-25-69-34-F6-01-96-01-D6-9B-0E-78-9E-06-9B-00-00-00-00-00-00-00-5A" is of type byte string

    If yes than convert byte string to UTF 8 string.

    Wednesday, October 7, 2020 9:56 AM
  • Hi,

    What should be the final result of the conversion of this string? When I used the above method to convert, I got a bunch of garbled codes.

    Isn't this string converted by the above method? If not, how did you get it?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 8, 2020 7:41 AM