views:

4684

answers:

4

Help me people Last week I asked about unicode conversion.I got only one reply.I need more suggestions.

I need to convert strings in Python to other types such as unsigned and signed int 8 bits,unsigned and signed int 16 bits,unsigned and signed int 32 bits,unsigned and signed int 64 bits,double,float,string,unsigned and signed 8 bit,unsigned and signed 16 bit, unsigned and signed 32 bit,unsigned and signed 64 bit.

Exact duplicate: conversion of unicode string in python

+2  A: 

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.

yjerem
I believe the same holds true for all other python types. It sounds to me like someone wants C not Python
Unkwntech
+6  A: 

You can convert a string to a 32-bit signed integer with the int function:

str = "1234"
i = int(str)  // 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**7                      // 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.

Adam Rosenfield
It is interesting how different can be accepted answers to the exact same question http://stackoverflow.com/questions/374318/conversion-of-unicode-string-in-python#374335
J.F. Sebastian
+1  A: 

The following types -- for the most part -- don't exist in Python in the first place. In Python, strings are converted to ints, longs or floats, because that's all there is.

You're asking for conversions that aren't relevant to Python in the first place. Here's the list of types you asked for and their Python equivalent.

  • unsigned and signed int 8 bits, int
  • unsigned and signed int 16 bits, int
  • unsigned and signed int 32 bits, unsigned: long, signed int
  • unsigned and signed int 64 bits, long

  • double, float

  • float, float
  • string, this is what you had to begin with

I don't know what the following are, so I don't know a Python equivalent.

  • unsigned and signed 8 bit,
  • unsigned and signed 16 bit,
  • unsigned and signed 32 bit,
  • unsigned and signed 64 bit.

You already have all the conversions that matter: int(), long() and float().

S.Lott
* unsigned and signed 8 bit, * unsigned and signed 16 bit, * unsigned and signed 32 bit, * unsigned and signed 64 bit.refer to bitfields
What does "bit field" mean? An int has 32 bits -- it appears to be a bit field to me. What are the attributes and operations of this "bit" field that makes it different from an int?
S.Lott
A: 

I don't think this can necessarily be answered well without more information. As others have said, there are only int and long for integers in python -- the language doesn't adhere to the bit-width and signedness archetypes of lower-level programming languages.

If you're operating completely within python, then you're probably asking the wrong question. There's likely a better way to do what you need.

If you are interoperating with, for instance, C code, or over the network, then there are ways to do this, and it looks like the answer to your previous posting covered that avenue pretty handily.

Jorenko