hi,

is there a way to convert from string to long? for eg: i want to concatenate all the arrays into data and make it same type (long) as data1.

a='0x'
array0 = '00000018000004000000000000000000'
array1 = '00000000000000000000000000000000'
array2 = 'fe000000000000000000000000000000'
array3 = '00000000000000000000000000ffffff'

data = a+array0+array1+array2+array3
print data
print type(data)

data1 = 0x0000001800000400000000000000000000000000000000000000000000000000fe00000000000000000000000000000000000000000000000000000000ffffff
print type(data1)

code:

###result:

0x0000001800000400000000000000000000000000000000000000000000000000fe00000000000000000000000000000000000000000000000000000000ffffff
<type 'str'>
<type 'long'>

######


thanks

tcl

great but the leading zeroes are being truncated.
i want it to be: 0x0000001800000400000000000000000000000000000000000000000000000000L

>>> array0='00000018000004000000000000000000'
>>> array1='00000000000000000000000000000000'
>>> array=array0+array1
>>> a=int(array,16)
>>> print a
647038726439367532107969464256319505531941876229785714677657987186688
>>> print hex(a)
0x1800000400000000000000000000000000000000000000000000000000L
>>>

You can't have it both ways: Either it is an int (long) and there is no such thing as 'leading zeroes' which is about a string representation; or it is a string in which case there is no need to convert from the string in the first place. You can use a format string, of course: print("%099d"%a)

Edited 5 Years Ago by griswolf: n/a

The problem is that the int representation doesn't see the leading zeroes

>>> data1 = 0x0000001800000400000000000000000000000000000000000000000000000000fe00000000000000000000000000000000000000000000000000000000ffffff
>>> data2 = 0x1800000400000000000000000000000000000000000000000000000000fe00000000000000000000000000000000000000000000000000000000ffffff
>>> data1 == data2
True

A solution is to add a leading "1" to your string and to remove the "1" when you convert back to a string.

Edited 5 Years Ago by Gribouillis: n/a

This article has been dead for over six months. Start a new discussion instead.