Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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中的ToInt64#_C#_Python_Sha256 - Fatal编程技术网

C# 在Python中将十六进制转换为整数-相当于C中的ToInt64#

C# 在Python中将十六进制转换为整数-相当于C中的ToInt64#,c#,python,sha256,C#,Python,Sha256,我正在尝试将sha256散列转换为整数。这在C#中可以使用,但在Python中无法使用 C# Version : string inputString = "TestString"; SHA256 sha256Hash = SHA256.Create(); byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(inputString));

我正在尝试将sha256散列转换为整数。这在C#中可以使用,但在Python中无法使用

C# Version :
string inputString = "TestString";
            SHA256 sha256Hash = SHA256.Create();
            byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(inputString));
            //Read the Bytes and print
            var sb = new StringBuilder(bytes.Length * 2);
            sb = new StringBuilder(bytes.Length * 2);
            foreach (byte b in bytes)
            {
                sb.Append(b.ToString("x2"));
            }
            Console.WriteLine("Bytes: " + sb);            
            Console.WriteLine("ConvertedInt: " + BitConverter.ToInt64(bytes));
Output :
Bytes: 6dd79f2770a0bb38073b814a5ff000647b37be5abbde71ec9176c6ce0cb32a27
ConvertedInt: 4088037490330425197
上面按预期打印字节和整数值。尝试在Python中执行相同的操作,但无法转换为相同的整数

import hashlib
hashobj = hashlib.sha256("TestString".encode('utf-8')).hexdigest()
print(hashobj)

#Tried Option 1:
print(f"Option 1 prints:\r")
print(int(hashobj,32))

#Tried Option 2:
print(f"Option 1 prints:\r")
print(int(hashobj,16))

Output:
6dd79f2770a0bb38073b814a5ff000647b37be5abbde71ec9176c6ce0cb32a27
Option 1 prints:
428476861264242379186014021585258195649378281633207959348811042267114033125399631735390859241543
Option 1 prints:
49683071055471546380350462257487304408748464885741562355821390565893091830311
如何在python中将hashobj转换为64位int?只是尝试在python中获得相同的整数值,谢谢您的帮助

编辑1: 根据@Sweeper和@Recursing的回答,我们可以继续进行,但在结果整数有符号时出现问题。
案例1:十六进制到整数是+ve,当我尝试将int(8字节)与一个数字(比如300)进行模运算时,会得到预期的结果108。
案例2和案例3:十六进制到整数是-ve,那么我必须读取可变字节数,案例2为7字节,案例3为9字节,然后按300模运算得到预期结果108。

问题:当整数为-ve时,如何确定要读取的字节数,以获得相同的结果?谢谢。

#Case 1
hash_bytes = hashlib.sha256("098C10227K3R".encode('utf-8')).digest()
print("Case 1 : Is Positive : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) > 0))
print("Case 1 : IntegerValue : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True)))
print("Case 1 : 8 Bytes:")
print(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) % 300)

#Case 2
hash_bytes = hashlib.sha256("159YK282MS3T".encode('utf-8')).digest()
print("Case 2 : Is Positive : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) > 0))
print("Case 2 : IntegerValue : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True)))
print("Case 2 : 8 Bytes:")
print(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) % 300)
print("Case 2 : 7 Bytes:")
print(int.from_bytes(hash_bytes[:7], byteorder="little", signed=True) % 300)

#Case 3
print("Case 3:")
hash_bytes = hashlib.sha256("17FK427W501L".encode('utf-8')).digest()
print("Case 3 : Is Positive : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) > 0))
print("Case 3 : IntegerValue : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True)))
print("Case 3 - 8 Bytes:")
print(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) % 300)
print("Case 3 - 9 Bytes:")
print(int.from_bytes(hash_bytes[:9], byteorder="little", signed=True) % 300)

Output:
Case 1 : Is Positive : True
Case 1 : IntegerValue : 9212408962392255108
Case 1 : 8 Bytes:
108
Case 2 : Is Positive : False
Case 2 : IntegerValue : -5822649536180381508
Case 2 : 8 Bytes:
192
Case 2 : 7 Bytes:
Case 3 : IntegerValue 7 bytes: 14015580891781308
108
Case 3:
Case 3 : Is Positive : False
Case 3 : IntegerValue : -2669588811718081008
Case 3 - 8 Bytes:
192
Case 3 - 9 Bytes:
Case 3 : IntegerValue 9 bytes: -445391446580747319792
108
正如@Sweeper所说,C代码只保留散列的前8个字节,并将其转换为整数

在python中,您可以执行以下操作:

导入hashlib
hash_bytes=hashlib.sha256(“TestString.encode('utf-8')).digest()
第一个字节=散列字节[:8]
打印(int.from_字节(第一个字节,byteorder=“little”,signed=True))
另一种方法是使用标准库中的
struct
模块:

打印(结构解包(“q”,第一个字节))

您只需要前8个字节(这是C代码的功能)。谢谢@Sweeper,它可以工作!我觉得这和读取字节没有多大关系。相反,这是因为python的
%
运算符计算模,而C的
%
运算符计算余数。如果是负数,只需将其设为正数,
%300
,然后再设为负数。谢谢@Sweeper,这正是问题所在。这里的一些阅读帮助:[link]
absVal=abs(int.from_字节(hash_字节[:8],byteorder=“little”,signed=True)]打印(“案例3:绝对:{}”。格式(absVal%300))
给出了案例2和案例3的正确结果。感谢you@Sweeper,在读取前8个字节时出现问题(如果为负整数)。请检查编辑1。谢谢