Converting from hex to string

后端 未结 6 705
慢半拍i
慢半拍i 2020-11-27 04:44

I need to check for a string located inside a packet that I receive as byte array. If I use BitConverter.ToString(), I get the bytes a

6条回答
  •  生来不讨喜
    2020-11-27 05:33

    string str = "47-61-74-65-77-61-79-53-65-72-76-65-72";
    string[] parts = str.Split('-');
    
    foreach (string val in parts)
    { 
        int x;
        if (int.TryParse(val, out x))
        {
             Console.Write(string.Format("{0:x2} ", x);
        }
    }
    Console.WriteLine();
    

    You can split the string at the -
    Convert the text to ints (int.TryParse)
    Output the int as a hex string {0:x2}

提交回复
热议问题