C# - Convert a string of hex values to hex

前端 未结 3 1060
感动是毒
感动是毒 2020-12-04 03:52

This might sound odd, but my issue is that I have a text string of hex values from a text file, like so:

\"0x0f, 0x40, 0xff, ....\"

I have

相关标签:
3条回答
  • 2020-12-04 03:58

    Non of Odd hex string is correct. Check source from you get this string . It is because of truncation of string due to limit no of characters. If String is image is stored in database then retrieve it using program not using any tools

    I was having same problem with .net and MSSQL and by using webservice and Java Client

    0 讨论(0)
  • 2020-12-04 04:11

    You just have to parse each string. Because each one is already only one value, you can do this:

    byte b;
    if (byte.TryParse(s, NumberStyles.HexNumber, 
        CultureInfo.InvariantCulture.NumberFormat, out b)) 
    {
        // b contains the value.
    }
    

    where s is the string you want to parse, and b is the resulting value.

    0 讨论(0)
  • 2020-12-04 04:11

    If your string is in the correct format you can create your array using this code (will throw exceptions if the input is badly formatted):

    var text = "0x0f, 0x40, 0xff";
    var bytes = text
      .Split(new[] { ", " }, StringSplitOptions.None)
      .Select(s => (Byte) Int32.Parse(s.Substring(2), AllowHexSpecifier));
    
    0 讨论(0)
提交回复
热议问题