472,785 Members | 1,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,785 software developers and data experts.

How to convert arbitrary objects directly to base64 without initial string conversion?

I've got a case where I want to convert binary blocks of data (various
ctypes objects) to base64 strings.

The conversion calls in the base64 module expect strings as input, so
right now I'm converting the binary blocks to strings first, then
converting the resulting string to base64. This seems highly
inefficient and I'd like to just go straight from binary to a base64
string.

Here is the conversion we're using from object to string...

import ctypes
def ObjAsString(obj):
sz = ctypes.sizeof(obj)
charArray = ctypes.c_char * sz
buf = charArray.from_address(ctypes.addressof(obj))
return buf.raw[:sz]

The returned string can then be sent to base64 for conversion (although
we're actually using xmlrpc.Binary), but there is obviously some waste
in here.

import base64
b64 = base64.b64encode(ObjAsString(foo))

Is there a canned/pre-existing way to convert a block of memory to a
base64 string more efficiently? I'd like to avoid writing my own
base64 conversion routine if possible. Anyone have any good ideas?
Even a mroe efficient/less clunky way of conevrting an arbitrary object
to a string would be appreciated.

Thanks,
Russ

Jul 13 '06 #1
4 5446
Russell Warren schrieb:
I've got a case where I want to convert binary blocks of data (various
ctypes objects) to base64 strings.

The conversion calls in the base64 module expect strings as input, so
right now I'm converting the binary blocks to strings first, then
converting the resulting string to base64. This seems highly
inefficient and I'd like to just go straight from binary to a base64
string.
[...]
Is there a canned/pre-existing way to convert a block of memory to a
base64 string more efficiently? I'd like to avoid writing my own
base64 conversion routine if possible. Anyone have any good ideas?
Even a mroe efficient/less clunky way of conevrting an arbitrary object
to a string would be appreciated.
Many functions that operate on strings also accept buffer objects as parameters,
this seems also be the case for the base64.encodestring function. ctypes objects
support the buffer interface.

So, base64.b64encode(buffer(ctypes_instance)) should work efficiently.

Thomas

Jul 13 '06 #2
Many functions that operate on strings also accept buffer objects as parameters,
this seems also be the case for the base64.encodestring function. ctypes objects
support the buffer interface.

So, base64.b64encode(buffer(ctypes_instance)) should work efficiently.
Thanks! I have never used (or even heard of) the buffer objects. I'll
check it out.

Russ

Jul 13 '06 #3
After some digging around it appears there is not a tonne of
documentation on buffer objects, although they are clearly core and
ancient... been sifting through some hits circa 1999, long before my
python introduction.

What I can find says that buffer is deprecated (Python in a Nutshell),
or non-essential/for-older-versions (Python documentation).

At least it no longer seems terribly weird to me that I never noticed
this built-in before... I got this from the python docs in reference to
buffer and others:

"Python programmers, trainers, students and bookwriters should feel
free to bypass these functions without concerns about missing something
important".

Is buffer safe to use? Is there an alternative?
ctypes objects support the buffer interface
How can you tell what objects support the buffer interface? Is
anything visible at the python level, or do you need to dig into the C
source?

Regarding documentation, I assume the C PyBufferObject is the
underlying thing for the python-level buffer? If so, is the best place
for docs on this ancient object to glean what I can from this link:
http://www.python.org/doc/1.5.2p2/ap...erObjects.html ?

Any help is appreciated... I'd like to understand what I can about this
object if I'm to use it... I'm wary of nasty surprises.

Russ

Jul 13 '06 #4
Russell Warren wrote:
How can you tell what objects support the buffer interface? Is
anything visible at the python level, or do you need to dig into the C
source?
At the C level, there is a function for testing:

int PyObject_CheckReadBuffer(PyObject* o)

http://docs.python.org/dev/api/abstract-buffer.html

According to

http://docs.python.org/dev/lib/non-e...-in-funcs.html

a Python call to buffer(obj) will either return a buffer object or (tested)
raise a TypeError:
>>buffer("123")
<read-only buffer for 0x2b597d52b9c0, size -1, offset 0 at 0x2b597d52e340>
>>buffer(1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: buffer object expected

Stefan
Jul 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: praba kar | last post by:
Dear All, I have doubt regarding date string to time conversion function. In Python I cannot find flexible date string conversion function like php strtotime. I try to use following type...
3
by: RC | last post by:
Just wondering if there are good reasons (performance, ?) to use one of these string conversion alternatives over the other: 1. Convert.ToString() 2. someObject.ToString() Example:...
2
by: Diablo | last post by:
Hi, I am converting a byte array to string using System.Text.Encoding.UTF8.GetString() or Unicode.GetString(). Then the resulting text is converted back to binary, using the reverse function:...
1
by: James dean | last post by:
The following code is the current method i have for converting an image to Base64.Is this the most efficient way to convert an image to base64 or are there others. The Image class's Save method is...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
7
by: LuB | last post by:
I have to convert a wide (16bit) char to a standard (8bit) char. Please nevermind that I am using WideCharToMultiByte ... but my question is as follows: Does this approach make any sense? My...
28
by: walterbyrd | last post by:
Python seems to have a log of ways to do collections of arbitrary objects: lists, tuples, dictionaries. But what if I want a collection of non-arbitrary objects? A list of records, or something...
2
by: Saeed Amrollahi | last post by:
Dear All Hi I usually use the following function for built-in types to std::string conversion: // convert a type (typically built-in type) to std::string template<class T> inline std::string...
9
by: jl_post | last post by:
Hi, A few months back I remember reading through C++ newsgroups trying to find a way to quickly convert a number to a C++ std::string. I often see code like: // Create a string that holds a...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.