Converting hex string to an integer

D

Derek Fountain

Given the character string "0x00A1B2C3" arriving at sys.argv[1] how do I
convert that to an integer which I can do some math on?
 
A

Alexandre Fayolle

Le 26-08-2004 said:
Given the character string "0x00A1B2C3" arriving at sys.argv[1] how do I
convert that to an integer which I can do some math on?
10597059
 
R

Rick Holbert

Derek said:
Given the character string "0x00A1B2C3" arriving at sys.argv[1] how do I
convert that to an integer which I can do some math on?

i = eval(sys.argv[1])
 
P

Peter Hansen

Rick said:
Derek said:
Given the character string "0x00A1B2C3" arriving at sys.argv[1] how do I
convert that to an integer which I can do some math on?

i = eval(sys.argv[1])

That's dangerous advice to a newbie if not qualified carefully.

Derek, "eval" could be the source of serious security problems
if you don't understand its power. Specifically it should
almost never be used for input that comes from a user or
via the command line. There is pretty much always another
and much better way to do the simple stuff like conversions
than to use eval.

For example, imagine if a malicious could feed your program this:

(on the Linux command line)

$ myscript "__import_('os').system('rm -rf /')"

or the Windows version:

C:\> myscript "__import_('os').system('deltree /y c:\*.*')"

Bye-bye filesystem... (don't run these examples!)

-Peter
 
?

=?ISO-8859-1?Q?Michael_Str=F6der?=

Peter said:
Rick said:
Derek said:
Given the character string "0x00A1B2C3" arriving at sys.argv[1] how do I
convert that to an integer which I can do some math on?

i = eval(sys.argv[1])

That's dangerous advice to a newbie if not qualified carefully.

Derek, "eval" could be the source of serious security problems
if you don't understand its power.

Yes, eval() is risky! Try to get rid of eval() or you MUST protect each and
every call to eval() with paranoid parameter checking!

int(sys.argv[1],16) would be a better approach here...
Traceback (most recent call last):

Ciao, Michael.
 
M

Michael Hudson

Peter Hansen said:
Rick said:
Derek said:
Given the character string "0x00A1B2C3" arriving at sys.argv[1] how do I
convert that to an integer which I can do some math on?
i = eval(sys.argv[1])

That's dangerous advice to a newbie if not qualified carefully.

Derek, "eval" could be the source of serious security problems
if you don't understand its power. Specifically it should
almost never be used for input that comes from a user or
via the command line. There is pretty much always another
and much better way to do the simple stuff like conversions
than to use eval.

For example, imagine if a malicious could feed your program this:

(on the Linux command line)

$ myscript "__import_('os').system('rm -rf /')"

Well, in this situation, he could just type

$ rm -rf /

But, yes.

Cheers,
mwh
 
P

Peter Hansen

Michael said:
Rick said:
Derek Fountain wrote:

Given the character string "0x00A1B2C3" arriving at sys.argv[1] how do I
convert that to an integer which I can do some math on?

i = eval(sys.argv[1])

That's dangerous advice to a newbie if not qualified carefully.

Derek, "eval" could be the source of serious security problems
if you don't understand its power. Specifically it should
almost never be used for input that comes from a user or
via the command line. There is pretty much always another
and much better way to do the simple stuff like conversions
than to use eval.

For example, imagine if a malicious could feed your program this:

(on the Linux command line)

$ myscript "__import_('os').system('rm -rf /')"

Well, in this situation, he could just type

$ rm -rf /

But, yes.

He could if he were on the same system, but it's quite possible
that sys.argv[1] in this particular program is actually coming
from a remote system in some manner (web?). But, yes. :)

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top