Search code examples
cpython-3.xunsigned-integer

Return type of unsigned int function


I have a .so file which has C functions for RFID read card, and I'm calling the C functions in my python program.

The C function type is unsigned int get_card() which has unsigned int card as a variable, and returns the value in unsigned int format.

But when I call that C function in my python program, and print the returned value, I'm getting a negative value.

Can you please help on this?


Solution

  • This is because your python code “doesn’t knows” that the value is unsigned int, so if the number in binary starts with the number one, for example “10” that is equal to 2 in decimal, python will read it as “-0” in decimal. So You need to create in python an unsigned int variable and copy the return value in the new variable, then you can print it. Hope it helps you