WindowsXP/ CTypes - How to convert ctypes array to a string?

dudeja.rajat at gmail.com dudeja.rajat at gmail.com
Tue Aug 19 14:29:12 CEST 2008


On Tue, Aug 19, 2008 at 12:40 PM,  <dudeja.rajat at gmail.com> wrote:
> On Tue, Aug 19, 2008 at 12:20 PM, Fredrik Lundh <fredrik at pythonware.com> wrote:
>> dudeja.rajat at gmail.com wrote:
>>
>>> I've used CTYPES module to access a function from a dll. This function
>>> provides me the version of the dll. This information is accessible to
>>> me as an array of 4 long inetegers. information as :
>>> 2, 1, 5, 0
>>>
>>> I want to display these elements concatenated as "v2.1.5.0". This
>>> string ( I'm thinking of writing the above 4 array elements to a
>>> string) is to be displayed as label in a GUI ( the GUI used is Tk)
>>>
>>> Please suggest how can I write these elements to a string to get me
>>> the desired results as "v2.1.5.0". And, is writing to a string is
>>> right way?
>>
>> any special reason why you're not reading replies to your previous post?
>>
>> here's what I wrote last time.
>>
>> expecting that Python/ctypes should be able to figure out that you
>> want an array of 4 integers printed as a dot-separated string is a
>> bit optimistic, perhaps.  but nothing that a little explicit string
>> formatting cannot fix:
>>
>>>>> from ctypes import *
>>>>> versionArr = c_long * 4
>>>>> version = versionArr(1, 2, 3, 4)
>>>>> "%d.%d.%d.%d" % tuple(version)
>> '1.2.3.4'
>>
>> inserting a "v" in the format string gives you the required result:
>>
>>>>> "v%d.%d.%d.%d" % tuple(version)
>> 'v1.2.3.4'
>>
>> </F>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
> Fredrik,
> My apology for any confusion created.
> I read all the replies. In fact I'm thankful to you all guys who are
> responding so quickly to all questions.
>
> I also add the letter 'v' same as you suggested. However, I'm looking
> to store this into some variable ( that variable could be be a string,
> tuple or anything, Im not sure which is the suited one) so that I can
> access it in some other module that will then display this version
> information in a Label Widet in GUI.
>
> So, what is the best way to store 'v1.2.3.4' in a variable? And, what
> type of variable is most suited (so as able to be used in other module
> probably, as string there)?
>
> Thanks,
> Rajat
>

Googled and found :

s = "v%d.%d.%d.%d" % tuple(version)
print s

it's working.

Result is : v1.2.3.4

Thanks,
Rajat



More information about the Python-list mailing list