Alon
Alon

Reputation: 2929

Python strings as long number

I have long string and I want to present it as a long num. I tried:

l=[ord (i)for i in str1]

but this is not what I need. I need to make it long number and not numbers as items in the list. this line gives me [23,21,45,34,242,32] and I want to make it one long Number that I can change it again to the same string.

any idea?

Upvotes: 2

Views: 188

Answers (5)

Paulo Bu
Paulo Bu

Reputation: 29794

What about using base 64 encoding? Are you fine with it? Here's an example:

>>>import base64
>>>s = 'abcde'
>>>e = base64.b64encode(s)
>>>print e
YWJjZGU=
>>>base64.b64decode(e)
'abcde'

The encoding is not pure numbers but you can go back and forth from string without much trouble.

You can also try encoding the string to hexadecimal. This will yield numbers although I'm not sure you can always come back from the encode string to the original string:

>>>s='abc'
>>>n=s.encode('hex')
>>>print n
'616263'
>>>n.decode('hex')
'abc'

If you need it to be actual integers then you can extend the trick:

>>>s='abc'
>>>n=int(s.encode('hex'), 16)  #convert it to integer
>>>print n
6382179
hex(n)[2:].decode('hex') # return from integer to string
>>>abc

Note: I'm not sure this work out of the box in Python 3

UPDATE: To make it work with Python 3 I suggest using binascii module this way:

>>>import binascii
>>>s = 'abcd'
>>>n = int(binascii.hexlify(s.encode()), 16) # encode is needed to convert unicode to bytes
>>>print(n)
1633837924  #integer
>>>binascii.unhexlify(hex(n)[2:].encode()).decode()
'abcd'

encode and decode methods are needed to convert from bytes to string and the opposite. If you plan to include especial (non-ascii) characters then probably you'll need to specify encodings.

Hope this helps!

Upvotes: 0

Alon
Alon

Reputation: 2929

This is code that I found that works.

str='sdfsdfsdfdsfsdfcxvvdfvxcvsdcsdcs sdcsdcasd'
I=int.from_bytes(bytes([ord (i)for i in str]),byteorder='big')
print(I)
print(I.to_bytes(len(str),byteorder='big'))

Upvotes: 0

senshin
senshin

Reputation: 10350

Here is a translation of Paulo Bu's answer (with base64 encoding) into Python 3:

>>> import base64
>>> s = 'abcde'
>>> e = base64.b64encode(s.encode('utf-8'))
>>> print(e)
b'YWJjZGU='
>>> base64.b64decode(e).decode('utf-8')
'abcde'

Basically the difference is that your workflow has gone from:

string -> base64
base64 -> string

To:

string -> bytes
bytes -> base64
base64 -> bytes
bytes -> string

Upvotes: 1

Robᵩ
Robᵩ

Reputation: 168616

#! /usr/bin/python2 
# coding: utf-8


def encode(s):
  result = 0
  for ch in s.encode('utf-8'):
    result *= 256
    result += ord(ch)
  return result

def decode(i):
  result = []
  while i:
    result.append(chr(i%256))
    i /= 256
  result = reversed(result)
  result = ''.join(result)
  result = result.decode('utf-8')
  return result


orig = u'Once in Persia reigned a king …'
cipher = encode(orig)
clear = decode(cipher)
print '%s -> %s -> %s'%(orig, cipher, clear)

Upvotes: 0

James Sapam
James Sapam

Reputation: 16940

Is this what you are looking for :

>>> str = 'abcdef'
>>> ''.join([chr(y) for y in [ ord(x) for x in str ]])
'abcdef'
>>>

Upvotes: 0

Related Questions