带有十六进制值的c#WebConfig应用程序设置

带有十六进制值的c#WebConfig应用程序设置,c#,hex,appsettings,C#,Hex,Appsettings,我有以下c#代码行: Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); 我需要的是在web.config中存储十六进制值列表: <add key="BytesVector" value="0x49, 0x76, 0x61, 0x

我有以下c#代码行:

Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
我需要的是在web.config中存储十六进制值列表:

<add key="BytesVector" value="0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76" />
有线索吗


您必须将字符串按“,”拆分,并转换为以16为基数的字节

下面是一个例子:

string s = "0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76";
var list = s.Split(',');

byte[] bytes = new byte[list.Length];
for(int i=0; i<list.Count(); i++)
{
    bytes[i] = Convert.ToByte(number.Trim(), 16);
}
string s=“0x49、0x76、0x61、0x6e、0x20、0x4d、0x65、0x64、0x76、0x65、0x64、0x65、0x76”;
var list=s.Split(',');
字节[]字节=新字节[list.Length];

对于(int i=0;iYou可以提供转换为.ToByte的完整
0x49
格式,无需执行子字符串。另外
byte[]bytes=s.Split(',')。选择(e=>Convert.ToByte(e.Trim(),16)).ToArray();
。我删除了子字符串,在进行修剪时不需要它。是的,您还可以使用一行linq解决方案:)
string s = "0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76";
var list = s.Split(',');

byte[] bytes = new byte[list.Length];
for(int i=0; i<list.Count(); i++)
{
    bytes[i] = Convert.ToByte(number.Trim(), 16);
}