Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C“如何将字符串十六进制值转换为”;“银行标识代码”;像xxd_C#_Binary_Hexdump - Fatal编程技术网

C# C“如何将字符串十六进制值转换为”;“银行标识代码”;像xxd

C# C“如何将字符串十六进制值转换为”;“银行标识代码”;像xxd,c#,binary,hexdump,C#,Binary,Hexdump,我正在尝试将字符串十六进制值转换为“binary”(.bin)格式的解决方案, 像xxd命令 例如: 我有一个字符串hexValue“17C7DFEF853ADCDB2C4B71DD8D0B3E3363” 在linux上,我希望得到如下结果: echo“hexvalue”>file.hex cat file.hex | xxd-r-p>file.bin file.bin的hextump提供: 0000000 17 c7 df ef 85 3a dc dd b2 c4 b7 1d d8 d0 b3

我正在尝试将字符串十六进制值转换为“binary”(.bin)格式的解决方案, 像xxd命令

例如: 我有一个字符串hexValue“17C7DFEF853ADCDB2C4B71DD8D0B3E3363”

在linux上,我希望得到如下结果:

echo“hexvalue”>file.hex cat file.hex | xxd-r-p>file.bin

file.bin的hextump提供:

0000000 17 c7 df ef 85 3a dc dd b2 c4 b7 1d d8 d0 b3 e3

0000010363636

我的转换程序是:

Private static string convertHex(string value)
{
    string[] hexVal = value.Split(' ');
    StringBuilder s = new StringBuilder();

    foreach (string hex in hexVal)
    {
         int val = Convert.ToInt32(hex, 16);
         string stringValue = char.ConvertFromUtf32(val);
         s.Append(stringValue);
    }
    return s.ToString();
}

string MyString = "17 c7 df ef 85 3a dc dd b2 c4 b7 1d d8 d0 b3 e3 36 36 36 36 36";
string newString = converthex(MyString);
Console.WriteLine(newString);
File.WriteAllText("file2.bin", newString);
现在,当查看file2.bin的hextump时,我看到:

0000000 17 c3 87 c3 97 c3 af c2 85 3a c3 9c c3 9d c2 b2

0000010 c3 84 c2 b7 1d c3 98 c3 90 c2 b3 a3 36

为什么我的新文件中存在c3或c2? 你有什么解决办法吗


谢谢你的帮助

File.WriteAllText用于文本文件写入,它用UTF-8编码传递的字符串。这就是为什么输出文件中会出现额外的字节

您想要的效果表明您需要一个二进制文件,而不需要在进程中转换为UTF-32字符串。以下是工作版本的示例:

class Program
{
    private static byte[] convertHex(string value)
    {
        string[] hexVal = value.Split(' ');
        byte[] output = new byte[hexVal.Length];
        var i = 0;
        foreach (string hex in hexVal)
        {
            byte val = (byte)(Convert.ToInt32(hex, 16));
            output[i++] = val;
        }
        return output;
    }
    static void Main(string[] args)
    {
        string MyString = "17 c7 df ef 85 3a dc dd b2 c4 b7 1d d8 d0 b3 e3 36 36 36 36 36";
        var file = new FileStream("file2.bin", FileMode.Create);
        var byteArray = convertHex(MyString);
        file.Write(byteArray, 0, byteArray.Length);
    }
}

嗯,好吧,这是我的错。我的程序必须计算十六进制字符串中的md5sum;以二进制格式。但是,我没有看到,我的函数md5是用utf8.getstring调用的。好的,很好,有你的帮助。非常感谢。有一天。。天啊!!