Dtypes don't work like they look at first glance. np.uint16 isn't a dtype object. It's just convertible to one. np.uint16 is a type object representing the type of array scalars of uint16 dtype.
x.dtype is an actual dtype object, and dtype objects implement == in a weird way that's non-transitive and inconsistent with hash. dtype == other is basically implemented as dtype == np.dtype(other) when other isn't already a dtype. You can see the details in the source.
Particularly, x.dtype compares equal to np.uint16, but it doesn't have the same hash, so the dict lookup doesn't find it.
SOLVED: 3 Solutions:
-
using Numpy : np.uint16()
-
Using CTypes : ctypes.c_uint16()
-
Using Bitwise : & 0xFFFF
Hi, I'm trying to convert this code to Python from Go / C. It involves declaring a UInt16 variable and run a bit shift operation. However cant seem to create a variable with this specific type. Need some advise here.
Go Code:
package main
import "fmt"
func main() {
var dx uint16 = 38629
var dy uint16 = dx << 8
fmt.Println(dy) //58624 -> Correct value
}
Python Code:
dx = 38629 dy = (dx << 8) print(dy) # 9889024 -> not the expected value print(type(dx)) # <class 'int'> print(type(dy)) # <class 'int'>
I cant seem to figure out a way to cast or similar function to get this into an Unsigned Int 16.\
Please help.
string - Typecasting in Python - Stack Overflow
Python How to convert the numpy array(dtype=uint16) to a string? - Stack Overflow
.net - How to use C# UInt16[,] in python - Stack Overflow
python - how to present uint16 as float16 - Stack Overflow
Dtypes don't work like they look at first glance. np.uint16 isn't a dtype object. It's just convertible to one. np.uint16 is a type object representing the type of array scalars of uint16 dtype.
x.dtype is an actual dtype object, and dtype objects implement == in a weird way that's non-transitive and inconsistent with hash. dtype == other is basically implemented as dtype == np.dtype(other) when other isn't already a dtype. You can see the details in the source.
Particularly, x.dtype compares equal to np.uint16, but it doesn't have the same hash, so the dict lookup doesn't find it.
x.dtype returns a dtype object (see dtype class)
To get the type inside, you need to call x.dtype.type :
print(x.dtype.type in d) # True
You can convert a string to a 32-bit signed integer with the int function:
string = "1234"
i = int(string) # i is a 32-bit integer
If the string does not represent an integer, you'll get a ValueError exception. Note, however, that if the string does represent an integer, but that integer does not fit into a 32-bit signed int, then you'll actually get an object of type long instead.
You can then convert it to other widths and signednesses with some simple math:
s8 = (i + 2**7) % 2**8 - 2**7 # convert to signed 8-bit
u8 = i % 2**8 # convert to unsigned 8-bit
s16 = (i + 2**15) % 2**16 - 2**15 # convert to signed 16-bit
u16 = i % 2**16 # convert to unsigned 16-bit
s32 = (i + 2**31) % 2**32 - 2**31 # convert to signed 32-bit
u32 = i % 2**32 # convert to unsigned 32-bit
s64 = (i + 2**63) % 2**64 - 2**63 # convert to signed 64-bit
u64 = i % 2**64 # convert to unsigned 64-bit
You can convert strings to floating point with the float function:
f = float("3.14159")
Python floats are what other languages refer to as double, i.e. they are 64-bits. There are no 32-bit floats in Python.
Python only has a single int type. To convert a string to an int, use int() like this:
>>> str = '123'
>>> num = int(str)
>>> num
123
Edit: Also to convert to float, use float() in the exact same way.
You need to use shorts or unsigned shorts:)
>>> import struct
>>> data_read
b'\x00\x00\x01\x00\x02\x00\x03\x00'
>>> struct.unpack("hhhh" , data_read)
(0, 1, 2, 3)
>>> struct.unpack("HHHH" , data_read)
(0, 1, 2, 3)
If you get the ascii display for b,',\,x, etc., it means that the file actually contains it. In fact it is caused by the str(b_a) call.
Demo:
>>> b_a = b'\x00\x00\x01\x00\x02\x00\x03\x00'
>>> s_a = str(b_a)
>>> print(s_a)
b'\x00\x00\x01\x00\x02\x00\x03\x00'
>>> [hex(ord(x)) for x in s_a]
['0x62', '0x27', '0x5c', '0x78', '0x30', '0x30', '0x5c', '0x78', '0x30', '0x30', '0x5c', '0x78', '0x30', '0x31', '0x5c', '0x78', '0x30', '0x30', '0x5c', '0x78', '0x30', '0x32', '0x5c', '0x78', '0x30', '0x30', '0x5c', '0x78', '0x30', '0x33', '0x5c', '0x78', '0x30', '0x30', '0x27']
which is the display for b, ', \, x, 0, etc.
ast.literal_eval can be used to convert it back to a byte string. Demo:
>>> b2 = ast.literal_eval(s_a)
>>> b2 == b_a
True
>>> struct.unpack('HHHH', b2)
(0, 1, 2, 3)
So if you want to keep the str call when writing, you have to read it that way:
with open('data_0.txt') as f:
data_read = f.read()
data = struct.unpack('HHHH', ast.literal_eval(data_read))