3

In C#, I can cast things to 8bit signed ints like so:

(sbyte)arg1;

which when arg1 = 2, the cast returns 2 also. However, obviously casting 128 will return -128. More specifically casting 251 will return -5.

What's the best way to emulate this behavior?

Edit: Found a duplicate question: Typecasting in Python

s8 = (i + 2**7) % 2**8 - 2**7      // convert to signed 8-bit
Community
  • 1
  • 1
Dominic Bou-Samra
  • 14,799
  • 26
  • 100
  • 156

5 Answers5

3

With ctypes:

from ctypes import cast, pointer, c_int32, c_byte, POINTER
cast(pointer(c_int32(arg1)), POINTER(c_byte)).contents.value
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
2

I'd use the struct module of the Python standard library, which, as so often, comes in handy for turning values into bytes and viceversa:

>>> def cast_sbyte(anint):
    return struct.unpack('b', struct.pack('<i', anint)[0])[0]
... ... 
>>> cast_sbyte(251)
-5
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
0

struct module can help you, e.g. here is a way to convert int(4 bytes) to 4 signed bytes

>>> import struct
>>> struct.pack('i',251)
'\xfb\x00\x00\x00'
>>> s=struct.pack('i',251)
>>> print struct.unpack('bbbb',s)
(-5, 0, 0, 0)
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
0
>>> from numpy import int8
>>> int8(251)
-5
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Try one of the following:

>>> sbyte=lambda n:(255 & n^128)-128 
>>> # or sbyte=lambda n:(n+128 & 255)-128

>>> sbyte(251)
-5
>>> sbyte(2)
2
Nas Banov
  • 28,347
  • 6
  • 48
  • 67