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.

Answer from Adam Rosenfield on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › unsigned string to signed integer?
r/learnpython on Reddit: Unsigned String to Signed Integer?
November 14, 2023 -

I have a string (list member) that's being received as 65520, for instance. However this value is being received from a register that's a regular INT 2's complement representation. So really it's -16 in disguise. Obviously that's a problem on the sending side, when it does the INT to ASCII it should just encode it as -16, but it doesn't/can't.

When I read this value into an INT it of course reads the INT as 65520 since it has no way of knowing the original representation. Is there a way to force the ASCII to INT conversion to interpret the string value as a signed INT? So that input string = 65520 outputs INT = -16?

Top answer
1 of 7
146

Assuming:

  1. You have 2's-complement representations in mind; and,
  2. By (unsigned long) you mean unsigned 32-bit integer,

then you just need to add 2**32 (or 1 << 32) to the negative value.

For example, apply this to -1:

>>> -1
-1
>>> _ + 2**32
4294967295L
>>> bin(_)
'0b11111111111111111111111111111111'

Assumption #1 means you want -1 to be viewed as a solid string of 1 bits, and assumption #2 means you want 32 of them.

Nobody but you can say what your hidden assumptions are, though. If, for example, you have 1's-complement representations in mind, then you need to apply the ~ prefix operator instead. Python integers work hard to give the illusion of using an infinitely wide 2's complement representation (like regular 2's complement, but with an infinite number of "sign bits").

And to duplicate what the platform C compiler does, you can use the ctypes module:

>>> import ctypes
>>> ctypes.c_ulong(-1)  # stuff Python's -1 into a C unsigned long
c_ulong(4294967295L)
>>> _.value
4294967295L

C's unsigned long happens to be 4 bytes on the box that ran this sample.

2 of 7
95

To get the value equivalent to your C cast, just bitwise and with the appropriate mask. e.g. if unsigned long is 32 bit:

>>> i = -6884376
>>> i & 0xffffffff
4288082920

or if it is 64 bit:

>>> i & 0xffffffffffffffff
18446744073702667240

Do be aware though that although that gives you the value you would have in C, it is still a signed value, so any subsequent calculations may give a negative result and you'll have to continue to apply the mask to simulate a 32 or 64 bit calculation.

This works because although Python looks like it stores all numbers as sign and magnitude, the bitwise operations are defined as working on two's complement values. C stores integers in twos complement but with a fixed number of bits. Python bitwise operators act on twos complement values but as though they had an infinite number of bits: for positive numbers they extend leftwards to infinity with zeros, but negative numbers extend left with ones. The & operator will change that leftward string of ones into zeros and leave you with just the bits that would have fit into the C value.

Displaying the values in hex may make this clearer (and I rewrote to string of f's as an expression to show we are interested in either 32 or 64 bits):

>>> hex(i)
'-0x690c18'
>>> hex (i & ((1 << 32) - 1))
'0xff96f3e8'
>>> hex (i & ((1 << 64) - 1)
'0xffffffffff96f3e8L'

For a 32 bit value in C, positive numbers go up to 2147483647 (0x7fffffff), and negative numbers have the top bit set going from -1 (0xffffffff) down to -2147483648 (0x80000000). For values that fit entirely in the mask, we can reverse the process in Python by using a smaller mask to remove the sign bit and then subtracting the sign bit:

>>> u = i & ((1 << 32) - 1)
>>> (u & ((1 << 31) - 1)) - (u & (1 << 31))
-6884376

Or for the 64 bit version:

>>> u = 18446744073702667240
>>> (u & ((1 << 63) - 1)) - (u & (1 << 63))
-6884376

This inverse process will leave the value unchanged if the sign bit is 0, but obviously it isn't a true inverse because if you started with a value that wouldn't fit within the mask size then those bits are gone.

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-signed-to-unsigned-integer-in-python
How to convert signed to unsigned integer in Python ? - GeeksforGeeks
April 5, 2021 - signed_integer = -1 # Adding 1<<32 to convert signed to # unsigned integer unsigned_integer = signed_integer+(1 << 32) print(unsigned_integer)
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 359675 › signed-and-unsigned-int-in-python
Signed and Unsigned int in python [SOLVED] | DaniWeb
A signed integer just uses one of the bits as a sign, so an 8 bit unsigned can store 0-->255, and an 8 bit signed -127-->127 because one bit is used as a sign. There is bitstring, bitbuffer and bitarray, Also, check PyPi for existing packages whenever you run into a problem like this . ... In Python integers have a size only limited by your computer's memory. If you want to force compatibility with C you can use Python module struct
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-signed-to-unsigned-integer-in-python
How to Convert Signed to Unsigned Integer in Python?
July 24, 2023 - In Python, integers are represented using a fixed number of bits, typically 32 bits. To convert a signed integer to its unsigned counterpart, we can simply add 2**32 to the signed integer value.
🌐
Reddit
reddit.com › r/learnpython › convert unsigned int to signed int in python 3
r/learnpython on Reddit: Convert unsigned int to signed int in Python 3
February 19, 2019 -

I have an integer from the result of binascii.crc32(). In Python 2, this function returned a signed int. However, in Python 3, it has been changed to always return an unsigned int. I am porting a piece of software from 2 to 3, and one of the things it does is calculate the crc and pack it with struct.pack(">l", crc32). This now causes an error as ">l" expects -2147483648 <= number <= 2147483647 but the crc can now exceed the upper limit.

How would I go about converting the crc to a signed value? As I understand Python doesn't have a concept of signed/unsigned, so you can't do signed_int = (int)unsigned_int; like you can in C.

Find elsewhere
🌐
Real Python
realpython.com › convert-python-string-to-int
How to Convert a Python String to int – Real Python
January 16, 2021 - There are several ways to represent integers in Python. In this quick and practical tutorial, you'll learn how you can store integers using int and str as well as how you can convert a Python string to an int and vice versa.
🌐
Sololearn
sololearn.com › en › Discuss › 3320064 › python-and-unsigned-int
Python and unsigned int | Sololearn: Learn to code for FREE!
March 12, 2025 - Basically, I need a mechanism in python to generate unsigned int object ... Though I am unfamiliar with using SWIG, I explored your question with ChatGPT. The primary answer was that it should work as long as you don't try to pass a negative integer. Taking it further, I asked how to make SWIG cast a negative int as unsigned. It gave me the SWIG setup below. I hope this helps: %module example %{ #include "example.h" %} // Custom typemap to convert negative Python integers to their unsigned representation %typemap(in) unsigned int { long temp = PyLong_AsLong($input); if (PyErr_Occurred()) return NULL; // Handle conversion errors $1 = static_cast<unsigned int>(temp); // Proper bitwise conversion } %include "example.h"
🌐
AI_FOR_ALL
kiran-parte.github.io › aiforall › blog-post-4.html
Python 101: DATA TYPES Ⅰ - NUMBERS
April 10, 2021 - You can convert from string to integer or float type only if that string resembles a number for e.g. '24', '720', etc. There are four different number systems in python.
🌐
Reddit
reddit.com › r/learnpython › declaring an unsigned integer 16 in python for bit shift operation
r/learnpython on Reddit: Declaring an Unsigned Integer 16 in Python for Bit Shift Operation
October 29, 2023 -

SOLVED: 3 Solutions:

  1. using Numpy : np.uint16()

  2. Using CTypes : ctypes.c_uint16()

  3. 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.

🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-to-integer-in-python
Convert String to Int in Python - GeeksforGeeks
The simplest way to convert a string to an integer in Python is by using the int() function.
Published   September 11, 2025
🌐
Career Karma
careerkarma.com › blog › python › python string to int() and int to string tutorial: type conversion in python
Python String to Int() and Int to String Tutorial: Type Conversion in Python
December 1, 2023 - The Python int method is used to convert a string to an integer. Learn how to use the int and str method on Career Karma.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-to-int-int-to-string
Python String to Int, Int to String | DigitalOcean
August 4, 2022 - In this tutorial, we will learn how to convert python String to int and int to String in python.
🌐
Kristrev
kristrev.github.io › programming › 2013 › 06 › 28 › unsigned-integeres-and-python
Unsigned integers and Python
June 28, 2013 - The easiest (and portable!) way to get unsigned integers in Python is to import the required types from the ctypes module. However, sometimes you need to convert Pythons ‘normal’ integers to unsigned values.