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?

9
  • Can you paste your string? Commented Jan 20, 2014 at 15:45
  • str1 = ? ya need that string Commented Jan 20, 2014 at 15:46
  • 2
    So what is your desired result? Commented Jan 20, 2014 at 15:47
  • 1
    what you mean by long num from that string ? Commented Jan 20, 2014 at 15:48
  • 1
    are you looking something like this stackoverflow.com/questions/2511058/… Commented Jan 20, 2014 at 16:01

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

Comments

0

Is this what you are looking for :

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

Comments

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)

Comments

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'))

Comments

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!

11 Comments

i need numbers.I this to change it to binary and every 3 1,0,0 premutation I get number from 1 to 9
any function in python that does this? or something similar?
@user3160197 Take a look at my edit with hexadecimals
hex sometimes use chars. so I need something similar.. but only with integers
@user3160197 Indeed, take a look at my third block of code where I convert it to pure integer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.