Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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#等价于Python';这不太合适吗?_C#_Python_Binary_Hex - Fatal编程技术网

有没有一个C#等价于Python';这不太合适吗?

有没有一个C#等价于Python';这不太合适吗?,c#,python,binary,hex,C#,Python,Binary,Hex,可能重复: 我正在用C#搜索一种与python兼容的方法,将十六进制转换为二进制。我通过以下操作在Python中反转了一个哈希: import sha import base64 import binascii hexvalue = "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8" binaryval = binascii.unhexlify(hexvalue) print base64.standard_b64encode(binaryval) >

可能重复:

我正在用C#搜索一种与python兼容的方法,将十六进制转换为二进制。我通过以下操作在Python中反转了一个哈希:

import sha
import base64
import binascii

hexvalue = "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"
binaryval = binascii.unhexlify(hexvalue)
print base64.standard_b64encode(binaryval)
>> W6ph5Mm5Pz8GgiULbPgzG37mj9g=
到目前为止,我发现的各种Hex2Binary C#方法最终都会抛出OverflowException:

static string Hex2Binary(string hexvalue)
{
    string binaryval = "";
    long b = Convert.ToInt64(hexvalue, 16);
    binaryval = Convert.ToString(b);
    byte[] bytes = Encoding.UTF8.GetBytes(binaryval);
    return Convert.ToBase64String(bytes);
}

有人知道如何生成与python输出匹配的C#方法吗?

您可以将十六进制字符串分成两个数字组,然后使用byte.parse将其转换为字节。使用NumberStyles.AllowHexSpecifier进行此操作,例如:

byte.Parse("3F", NumberStyles.AllowHexSpecifier);
以下例程将十六进制字符串转换为字节数组:

private byte[] Hex2Binary(string hex)
{
    var chars = hex.ToCharArray();
    var bytes = new List<byte>();
    for(int index = 0; index < chars.Length; index += 2) {
        var chunk = new string(chars, index, 2);
        bytes.Add(byte.Parse(chunk, NumberStyles.AllowHexSpecifier));
    }
    return bytes.ToArray();
}
private byte[]Hex2Binary(字符串十六进制)
{
var chars=hex.ToCharArray();
var bytes=新列表();
对于(int index=0;index
这个值对于长时间(64位)来说太大,这就是为什么会出现OverflowException

但是很容易将十六进制逐字节转换为二进制(实际上是逐字节):

静态字符串Hex2Binary(字符串hexvalue)
{
StringBuilder binaryval=新的StringBuilder();
for(int i=0;i
EDIT:上面的方法转换为二进制,而不是base64。这个转换为base64:

static string Hex2Base64(string hexvalue)
{
    if (hexvalue.Length % 2 != 0)
        hexvalue = "0" + hexvalue;
    int len = hexvalue.Length / 2;
    byte[] bytes = new byte[len];
    for(int i = 0; i < len; i++)
    {
        string byteString = hexvalue.Substring(2 * i, 2);
        bytes[i] = Convert.ToByte(byteString, 16);
    }
    return Convert.ToBase64String(bytes);
}
静态字符串Hex2Base64(字符串hexvalue)
{
如果(hexvalue.Length%2!=0)
hexvalue=“0”+hexvalue;
int len=hexvalue.Length/2;
字节[]字节=新字节[len];
对于(int i=0;i
Duplicate of(使用Convert.ToBase64String和生成的字节数组,顺便说一句)。它不是重复的IMO,这是特定于Python和C#之间的兼容性。答案可能是相同的,但问题(其他人可能会偶然发现)肯定是不同的。现在他们会偶然发现这个问题,并立即看到现有问题的链接!这就是为什么我们总是重复提问的原因。现在对相反的端点进行提问吧?:)他需要把它转换成相反的端点吗?我说,只有在需要的时候才做;-)我想我需要增加2,而不是1。
static string Hex2Base64(string hexvalue)
{
    if (hexvalue.Length % 2 != 0)
        hexvalue = "0" + hexvalue;
    int len = hexvalue.Length / 2;
    byte[] bytes = new byte[len];
    for(int i = 0; i < len; i++)
    {
        string byteString = hexvalue.Substring(2 * i, 2);
        bytes[i] = Convert.ToByte(byteString, 16);
    }
    return Convert.ToBase64String(bytes);
}