0

Pythonでこれと同等の(および逆toByte)を実行したいのですが、Pythonでどのようにマッピングしますか?

int toInt(byte b) {
  return map(b, 0, 255, -128, 127);
}

やってみます

int([representation],base)-128 

しかし、私は表現とベースが何であるかわかりません

4

3 に答える 3

3

int([representation],base)-128私があなたの質問をよく理解していれば。何らかの理由で関数に満足できない場合は、python 辞書構造を使用してみてください

于 2013-02-26T18:34:23.407 に答える
0

それは単に次のことではないでしょうか:

def toInt(b):
    return b-128

def toByte(i):
    return i+128
于 2013-02-26T18:49:20.073 に答える
0

それを行う方法は複数あります。明示的なマッピングを使用できます。

INT_MAP = {x: x - 128 for x in range(256)}
def to_int(val):
    """Maps an unsigned integer to a signed one (for values up to 256)"""
    try:
        return INT_MAP[val]
    except KeyError:
        raise ValueError("val must be a value between 0 and 255")

または、数学を使用することもできます。

def to_int(val, max_signed_val=128):
    max_val = max_signed_val * 2
    assert val < max_val, "val must be less than {:d}".format(max_val)
    return val - max_signed_val
于 2013-02-26T18:30:31.440 に答える