473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Converting hex string to an integer

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?
Jul 18 '05 #1
6 5976
Le 26-08-2004, Derek Fountain <no****@example.com> a écrit*:
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?

s = "0x00A1B2C3"
int(s, 16)

10597059
--
Alexandre Fayolle LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
Jul 18 '05 #2
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])

Jul 18 '05 #3
Rick Holbert wrote:
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 /')"

or the Windows version:

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

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

-Peter
Jul 18 '05 #4
Peter Hansen wrote:
Rick Holbert wrote:
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.


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...
int("0x00A1B2C3",16) 10597059 int("__import_('os').system('rm -rf /')",16) Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): __import_('os').system('rm -rf /')


Ciao, Michael.
Jul 18 '05 #5
Peter Hansen <pe***@engcorp.com> writes:
Rick Holbert wrote:
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.

Cheers,
mwh

--
I'm not particularly fond of singing GSTQ because she stands for
some things I don't, but it's not really worth letting politics
getting in the way of a good bawling. -- Dan Sheppard, ucam.chat
Jul 18 '05 #6
Michael Hudson wrote:
Peter Hansen <pe***@engcorp.com> writes:

Rick Holbert wrote:
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
Jul 18 '05 #7

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

Similar topics

4
by: Peter Warder | last post by:
I'm a newbie and I'm sorry this is so basic but . . . . If I have a String containing "123", how do I convert it to an Integer variable? I've tried all kinds of Integer.valueOf("123")...
2
by: Ken Fettig | last post by:
I have a quick simple question. How do you convert an integer to a string in Python? Thanks Ken Fettig
13
by: Gil | last post by:
I have a string : string A = "2"; I need to get it's integer value : // ?? int val = A ; // ??
4
by: rainmaker1234 | last post by:
Its very simple in VC++. In the followeing code I have declared a String, and a double than I am taking the string and converting it into Double. getch() at the end is only to pause the screen so...
0
by: wrytat | last post by:
In one web page, I redirect it to "prtDelReq.aspx?prtindex=0A2A3A8" after the user click the button. The prtindex actually contains many integers separated by 'A'. On the prtDelReq page, I use...
3
by: SiewSa | last post by:
I have come to a situation that I need to use the result of a string variable as a formula for performing calculation. As an example below: Dim X As Integer Dim Y As Integer Dim Result...
1
by: mdawoodk | last post by:
i am getting error "input string was not in correct format" when converting a string decimal into integer value. code is like this: string strVal = ""; int nVal = 0; strVal = "14.9"; nVal...
9
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string froma file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
1
by: sake | last post by:
This problem seems to have a fairly simple solution in C, and google seems to love to pull up solutions for C. However, C++ just seems have the exact opposite luck. So pretty much, I want to...
7
by: tragic54 | last post by:
Alright so i've done this in some of my recent programs with option strict on and for some reason When i debug and select the option from the combo box, it crashes and gives me a 'Input String was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.