long <-> str binary conversion
Is there any lib that convert very long numbers to string just copying the data?
These one-liners are too slow:
def xlong(s):
return sum([ord(c) << e*8 for e,c in enumerate(s)])
def xstr(x):
return chr(x&255) + xstr(x >> 8) if x else ''
print xlong('abcd'*1024) % 666
print xstr(13**666)
Asked by: Sam913 | Posted: 27-01-2022
Answer 1
You want the struct module.
packed = struct.pack('l', 123456)
assert struct.unpack('l', packed)[0] == 123456
Answered by: Aida306 | Posted: 28-02-2022
Answer 2
How about
from binascii import hexlify, unhexlify
def xstr(x):
hex = '%x' % x
return unhexlify('0'*(len(hex)%2) + hex)[::-1]
def xlong(s):
return int(hexlify(s[::-1]), 16)
I didn't time it but it should be faster and also work on larger numbers, since it doesn't use recursion.
Answered by: Darcy338 | Posted: 28-02-2022Answer 3
In fact, I have a lack of long(s,256) . I lurk more and see that there are 2 function in Python CAPI file "longobject.h":
PyObject * _PyLong_FromByteArray( const unsigned char* bytes, size_t n, int little_endian, int is_signed);
int _PyLong_AsByteArray(PyLongObject* v, unsigned char* bytes, size_t n, int little_endian, int is_signed);
They do the job. I don't know why there are not included in some python module, or correct me if I'am wrong.
Answered by: Chester246 | Posted: 28-02-2022Answer 4
If you need fast serialization use marshal module. It's around 400x faster than your methods.
Answered by: Brad444 | Posted: 28-02-2022Answer 5
I'm guessing you don't care about the string format, you just want a serialization? If so, why not use Python's built-in serializer, the cPickle module? The dumps
function will convert any python object including a long integer to a string, and the loads
function is its inverse. If you're doing this for saving out to a file, check out the dump
and load
functions, too.
>>> import cPickle
>>> print cPickle.loads(cPickle.dumps(13**666)) % 666
73
>>> print (13**666) % 666
73
Answered by: Grace658 | Posted: 28-02-2022
Answer 6
Performance of cPickle
vs. marshal
(Python 2.5.2, Windows):
python -mtimeit -s"from cPickle import loads,dumps;d=13**666" "loads(dumps(d))"
1000 loops, best of 3: 600 usec per loop
python -mtimeit -s"from marshal import loads,dumps;d=13**666" "loads(dumps(d))"
100000 loops, best of 3: 7.79 usec per loop
python -mtimeit -s"from pickle import loads,dumps;d= 13**666" "loads(dumps(d))"
1000 loops, best of 3: 644 usec per loop
marshal
is much faster.
Similar questions
python - What is wrong with this conversion to binary?
I'm writing a program that converts an integer to binary and everything has worked well save for one thing. I have the binary numbers stored in a list, so therefore I want to join the list together using the join() function. This works well too, however since the list is stored as integers I must concatenate an empty string with the binary list (whilst converting each number into a string). By the way, it's no...
python - float to binary <-> binary to float conversion
I want to convert a float number to a binary string and back.
I tried this:
import struct
from ast import literal_eval
float_to_binary = bin(struct.unpack('!i',struct.pack('!f', 3.14))[0])
print (float_to_binary)
binary_to_float = float(int(float_to_binary, 0))
print (binary_to_float)
result = float(literal_eval(float_to_binary))
print (result) #wrong, prints 1078523331.0, should be 3.14
python - Conversion of Binary Float
Given a Binary Float string containing a fractional part s: 100.0011
output must be 4.1875
i used ".".join(map(lambda x:str(int(x,2)),s.split('.')))
this gives 4.3 but not 4.1875
python - 16 bit Binary conversion
I have found several ways to convert both Integer and Float values to binary, and they each have their issues. I need to take an Integer/Float input between values of 0 and 10,000, convert to a 16-digit (exactly) binary string, manipulate the bits at random, and convert back to an Integer/Float (depending on the parameter).
However, I have been using the following code:
python - Why does my conversion of four bytes to binary have only 30 binary digits?
I have an IP address R (ie: "255.255.255.0") in string form that I'm hashing, and taking the first 4 bytes of that hash. I want to then convert that hashed result to binary format:
def H(R):
h = hashlib.sha256(R.encode('utf-8'))
return unhexlify(h.hexdigest())[0:4]
I tried doing the following, but I only get 30 bits instead of 32 (I remove the first 2 char...
mysql - Python 3 SQL Binary Data INSERT without Conversion
I am using MySql and Python 3.6/3.8 with mysqlclient to maintain a database with a binary field. The code was constructed before my time in Python 2.7 to insert directly into field with binary data. For example, the table looks like
+-------------------------+--------------------------------------------------------------------+------+-----+------------+-------+
| Field | Type ...
python - From binary to base 48 conversion
I created a function that takes in a binary as a string then converts it to base48, it works for most test cases but when I run "1010000001101101011000000100000001000101111010000101101010110000001100110" through it
import math
def Binary2Octoquadragesimal(n):
octdict = {"0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9","10":"a","11":"b","12":"c","13":"d","14":"e","15":"f"...
python - Conversion between binary vector and 128 bit number
Is there a way to convert back and forth between a binary vector and a 128-bit number? I have the following binary vector:
import numpy as np
bits = np.array([1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0,
1,...
python - jython date conversion
Given a string as below, I need to convert:
1 Dec 2008 06:43:00 +0100
to
MM/DD/YYYY HH:MM:SSAM
using jython what is the best way to do this?
python - Django and units conversion
I need to store some values in the database, distance, weight etc.
In my model, I have field that contains quantity of something and IntegerField with choices option, that determines what this quantity means (length, time duration etc).
Should I create a model for units and physical quantity or should I use IntegerField that contains the type of unit?
python - pytz utc conversion
What is the right way to convert a naive time and a tzinfo into an UTC time?
Say I have:
d = datetime(2009, 8, 31, 22, 30, 30)
tz = timezone('US/Pacific')
First way, pytz inspired:
d_tz = tz.normalize(tz.localize(d))
utc = pytz.timezone('UTC')
d_utc = d_tz.astimezone(utc)
Second way, from
Python Type Conversion
Whats the best way to convert int's, long's, double's to strings and vice versa in python.
I am looping through a list and passing longs to a dict that should be turned into a unicode string.
I do
for n in l:
{'my_key':n[0],'my_other_key':n[1]}
Why are some of the most obvious things so complicated?
python - png24 to png8 conversion
I want to convert the input 24 bit PNG image to 8 bit, I have tried using Imagemagick and Python PIL, neither works.
for instance:
at Imagemagick I try convert console command as such:
convert -depth 8 png24image.png png8image.png
And here is the way I tried with python:
import Image
def convert_8bit(src, dest):
"""
convert_8bit: String, String ->...
python - numpy arrays type conversion in C
I would like to convert the numpy double array to numpy float array in C(Swig).
I am trying to use
PyObject *object = PyArray_FROM_OT(input,NPY_FLOAT)
or
PyObject *object = PyArray_FROMANY(input,NPY_FLOAT,0,0,NPY_DEFAULT)
or
PyObject *object = PyArray_FromObject(input,NPY_FLOAT,0,0)
or
PyObject *object = P...
regex - python string conversion for eval
I have list like:
['name','country_id', 'price','rate','discount', 'qty']
and a string expression like
exp = 'qty * price - discount + 100'
I want to convert this expression into
exp = 'obj.qty * obj.price - obj.discount + 100'
as I wanna eval this expression like eval(exp or False, dict(obj=my_obj))
m...
python - string conversion
I’ve got a long string object which has been formatted like this
myString = “[name = john, family = candy, age = 72],[ name = jeff, family = Thomson, age = 24]”
of course the string is longer than this.
Also i have 3 lists with related names:
Names = []
Families = []
Ages = []
I want to read that string character by character and take the data and a...
Python to C# conversion
In python i'm creating .an file using
commandStr="dpan.exe -np -Lwork_%s.lib -Owork_%s.lib %s %s.an" %( option1, option2, Sourcefile, Destination file)
os.system( commandStr )
This will create the .an file (Destination file) from Sourcefile.
Now i'm converting this line of code from Python to C#
So how do i do this in C#. How to run the commandStr
python - RGB to HSV conversion using PIL
I'm trying to automate the enhancement of some images that are to be transfered to a digital frame. I have code in place that resizes, adds a date/time to the least-significant (least details) corner of the image and pastes together pairs of portrait images to avoid displaying a single portrait in the frame's 41:20 low resolution screen.
I've implemented a brightness-stretching filter for those pictures where the l...
python - jython date conversion
Given a string as below, I need to convert:
1 Dec 2008 06:43:00 +0100
to
MM/DD/YYYY HH:MM:SSAM
using jython what is the best way to do this?
UTF-8 latin-1 conversion issues, python django
ok so my issue is i have the string '\222\222\223\225' which is stored as latin-1 in the db. What I get from django (by printing it) is the following string, 'ââââ¢' which I assume is the UTF conversion of it. Now I need to pass the string into a function that
does this operation:
strdecryptedPassword + chr(ord(c) - 3 - intCounter - 30)
I get this error:
chr() ...
python - Django and units conversion
I need to store some values in the database, distance, weight etc.
In my model, I have field that contains quantity of something and IntegerField with choices option, that determines what this quantity means (length, time duration etc).
Should I create a model for units and physical quantity or should I use IntegerField that contains the type of unit?
python - need help - bit-field conversion
I want to convert strings to bit-fields.Also,convert them to binary and then use.
Need help with this..help me ..
Ruby to python one-liner conversion
I have a little one-liner in my Rails app that returns a range of copyright dates with an optional parameter, e.g.:
def copyright_dates(start_year = Date.today().year)
[start_year, Date.today().year].sort.uniq.join(" - ")
end
I'm moving the app over to Django, and while I love it, I miss a bit of the conciseness. The same method in Python looks like:
def copyright_dates...
python - pytz utc conversion
What is the right way to convert a naive time and a tzinfo into an UTC time?
Say I have:
d = datetime(2009, 8, 31, 22, 30, 30)
tz = timezone('US/Pacific')
First way, pytz inspired:
d_tz = tz.normalize(tz.localize(d))
utc = pytz.timezone('UTC')
d_utc = d_tz.astimezone(utc)
Second way, from
casting - Automatic String to Number conversion in Python
I am trying to compare two lists of string in python. Some of the strings are numbers however I don't want to use it as number, only for string comparison.
I read the string from a file and put them on a list like this:
def main():
inputFileName = 'BateCarteira.csv'
inputFile = open(inputFileName, "r")
bankNumbers = []
for line in inputFile:
values = line[0:len(line)-1].spl...
java - media conversion library
I am building a mobile website were users can upload/download videos, and I need a library that can convert the media files from mpeg, 3gp, mov depending on what the user wants to download.
Do you happen to know a a library that can do this?
python source code conversion to uml diagram with Sparx Systems Enterprise Architect
Please let me know how to create a uml diagram along with its equivalent documentation for the source code(.py format) using enterprise architecture 7.5
Please help me find the solution, I have read the solution for the question on this website related to my topic but in vain
python - Emulate floating point string conversion behaviour of Linux on Windows
I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"!
I originally encountered the problem in Python, but further investigation showed that the difference is in the underlying C runtime library. (The architecture is x86-x64 in b...
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python