如何在Python中将十六进制字符串转换为int?
我可以把它作为" 0xffff"或只是" ffff".
我编写的代码将包含所有16位长的负数和正数,MSB是符号,也就是二进制补码.这意味着我可以拥有的最小数字是-32768,它是1000 0000 0000 0000二进制补码形式.我可以有最大数量是32767是0111 1111 1111 1111.
我遇到的问题是Python是用相同的二进制记数法表示负数为正数只是把一个减号出前即-16384显示为-0100 0000 0000 0000我所要显示的数字,如-16384是1100 0000 0000 0000.
我不太清楚这是如何编码的.这是我的代码.基本上,如果数字在180到359之间,那么它将是负数.我需要将其显示为两个恭维值.我没有关于如何显示它的任何代码,因为我真的不知道该怎么做.
def calculatebearingActive(i):
numTracks = trackQty_Active
bearing = (((i)*360.0)/numTracks)
if 0< bearing <=179:
FC = (bearing/360.0)
FC_scaled = FC/(2**(-16))
return int(FC_scaled)
elif 180<= bearing <=359:
FC = -1*(360-bearing)/(360.0)
FC_scaled = FC/(2**(-16))
return int(FC_scaled)
elif bearing ==360:
FC = 0
return FC
Run Code Online (Sandbox Code Playgroud) 我有一个负整数(4个字节),我希望它的二进制补码表示形式的十六进制形式.
>>> i = int("-312367")
>>> "{0}".format(i)
'-312367'
>>> "{0:x}".format(i)
'-4c42f'
Run Code Online (Sandbox Code Playgroud)
但我想看看"FF ......"
我正在设计一个bloom过滤器,我想知道Python中性能最高的位数组实现是什么.
Python的优点是它可以处理开箱即用的任意长度整数,这就是我现在使用的,但我不太了解Python内部,知道这是否是在Python中执行它的最高性能方式.
我找到了,bitarray但它处理了很多其他的事情,比如切片,我不需要.我只需要&和|和<<操作.
我想知道是否有一种方法可以像使用Python中的C/C++那样使用标准库(最好是在bitarray上)进行二进制补码符号扩展.
C/C++:
// Example program
#include <iostream>
#include <string>
int main()
{
int x = 0xFF;
x <<= (32 - 8);
x >>= (32 - 8);
std::cout << x;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我编写的Python函数(在我的测试中)完成同样的事情.我只是想知道是否有内置(或更快)的方式:
def sign_extend(value, bits):
highest_bit_mask = 1 << (bits - 1)
remainder = 0
for i in xrange(bits - 1):
remainder = (remainder << 1) + 1
if value & highest_bit_mask == highest_bit_mask:
value = (value & remainder) - highest_bit_mask
else:
value = value & remainder
return value
Run Code Online (Sandbox Code Playgroud) 出于好奇,我看到了将对象的 id 转换为其哈希的操作在字符串域中的样子,而不是使用通常的按位操作,例如^, |, &, ~。
class A:
pass
def my_hash(a):
bits = format(id(a), '064b')
rot4 = bits[-4:] + bits[:-4]
n = int(rot4, 2)
return n
for _ in xrange(10):
a = A()
print hash(a) == my_hash(a), hash(a), my_hash(a)
Run Code Online (Sandbox Code Playgroud)
但是正如您在下面看到的,下面的函数有时不正确。我错过了什么?
>>> run /tmp/thing.py
True 272331835 272331835
False -9223372036582443978 9223372037127107638
True 272331835 272331835
False -9223372036582443978 9223372037127107638
True 272331835 272331835
False -9223372036582443978 9223372037127107638
True 272331835 272331835
False -9223372036582443978 9223372037127107638
True 272331835 272331835
False -9223372036582443978 9223372037127107638
Run Code Online (Sandbox Code Playgroud) 二进制补码是当您反转位然后添加二进制1位数.所以例如......
0011001
apply two's complement
1. inverse the bits, 1100110
2. add a binary digit, 1100110 + 1 = 1100111
Run Code Online (Sandbox Code Playgroud)
另一个显示溢出情况的例子......
1001100
apply two's complement
1. inverse the bits, 0110011
2. add a binary digit, 0110011 + 1 = 0110100
Run Code Online (Sandbox Code Playgroud)
在python中实现它的最佳方法是什么.到目前为止,我有这个代码,但我希望它更有效,因为我太多使用这种方法.
def toTwosComplement(binarySequence):
convertedSequence = [0] * len(binarySequence)
carryBit = 1
# INVERT THE BITS
for i in range(0, len(binarySequence)):
if binarySequence[i] == '0':
convertedSequence[i] = 1
else:
convertedSequence[i] = 0
# ADD BINARY DIGIT 1
if convertedSequence[-1] == 0: …Run Code Online (Sandbox Code Playgroud) python ×7
hex ×2
performance ×2
bit-shift ×1
bitarray ×1
bloom-filter ×1
hash ×1
integer ×1
signed ×1
string ×1