Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting String to a Hexadecimal value

Status
Not open for further replies.

shussai2

Programmer
Aug 28, 2003
42
CA
I want to convert a string into a hex value without the quotations. So for example I have:

s='0x'
r='01'
sr=s+r

//when I print sr it gives me '0x01'

what I want to do is to convert it in hex value by getting rid of the quotations and just having value as 0x01.

Is there function which converts a string or another type of value in Hex without the quotations in there. Meaning it gives me just the value no string representation of hex.


 
Code:
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> s='0x'
>>> r='01'
>>> sr = s+r
>>> sr
'0x01'
>>> print sr
0x01
>>> eval(sr)
1
>>> long(sr, 16)
1L
>>>
 
This brings up a point:

1 is a hexidecimal value.
0x01 is a string representation of the value 1 expressed in a common notation indicating that you are operating in base 16.

What are you trying to accomplish?

 
Well I was trying to use PIL to get particular pixels in a bitmap. But after I posted this I figured out the solution given above. Although what I do is convert the equivalent s='0x01' into a int by using string conversion base to 16.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top