2

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?

Kobi K
  • 7,743
  • 6
  • 42
  • 86
Alon
  • 2,919
  • 6
  • 27
  • 47

5 Answers5

1

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
senshin
  • 10,022
  • 7
  • 46
  • 59
0

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!

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • i need numbers.I this to change it to binary and every 3 1,0,0 premutation I get number from 1 to 9 – Alon Jan 20 '14 at 15:58
  • any function in python that does this? or something similar? – Alon Jan 20 '14 at 16:00
  • @user3160197 Take a look at my edit with `hexadecimals` – Paulo Bu Jan 20 '14 at 16:00
  • hex sometimes use chars. so I need something similar.. but only with integers – Alon Jan 20 '14 at 16:04
  • @user3160197 Indeed, take a look at my third block of code where I convert it to _pure integer_. – Paulo Bu Jan 20 '14 at 16:14
  • WOW thanks, now I only found out how to do this with python 3 – Alon Jan 20 '14 at 16:18
  • @user3160197 shouldn't be hard. Just keep the concept in mind. Convert a string to hexadecimal, turn hexadecimal in an integer, then convert integer to hex and decode hex into string. – Paulo Bu Jan 20 '14 at 16:19
  • @user3160197 I'll need to go out but when I return I'll figure out a way to do it in Python3, if you find it first please feel free to update my answer with the solution :) – Paulo Bu Jan 20 '14 at 16:20
  • thanks You I find something like this. n=int(binascii.hexlify(b'Blaah'),16) now I need to change it to string again – Alon Jan 20 '14 at 16:25
  • OK I found something that works. str='sdfsdfsdfdsfsdfcxvvdfvxcvsdcsdcs sdcsdcasd' l=[ord (i)for i in str] I=int.from_bytes(bytes(l),byteorder='big') print(I) print(I.to_bytes(len(str),byteorder='big')) – Alon Jan 20 '14 at 16:55
  • @user3160197 I came with another solution in Python3, check if you like it :) Glad I could help. Good luck! – Paulo Bu Jan 20 '14 at 18:27
0

Is this what you are looking for :

>>> str = 'abcdef'
>>> ''.join([chr(y) for y in [ ord(x) for x in str ]])
'abcdef'
>>>
James Sapam
  • 16,036
  • 12
  • 50
  • 73
0
#! /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)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

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'))
Alon
  • 2,919
  • 6
  • 27
  • 47