This is probably another newbie question...but...
even after reading quite some messages like '..hex to decimal',
'creating a hex value' , I can't figure this out:
If i do m=66 n=hex(m) n
'0x42'
i cannot use n as value for a variable that takes hex values, because it
throws:
error: required argument is not an integer
int(n)
gives me:
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
ValueError: invalid literal for int(): 0x42
how do I convert '0x42' to 0x42 ?
thanks! 8 2968
If you just want to convert a string to an integer, it would be: int(n)
in your case it would be:
m=66 n=int(hex(m))
avnit wrote: If you just want to convert a string to an integer, it would be:
int(n)
That's what the OP tried and it didn't work.
BECAUSE you have to tell the int function what base the string is in
(even though it has "0x" at the start).
int(n,16)
66 in your case it would be:
m=66 n=int(hex(m))
Wrong again, you still have to tell it what base.
h=int(hex(m),16) h
66
but then i get : m
66 n=int(hex(m))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
ValueError: invalid literal for int(): 0x42
what am I missing here ?
thank you
Tim
avnit wrote: If you just want to convert a string to an integer, it would be:
int(n)
in your case it would be:
m=66 n=int(hex(m))
tim wrote: but then i get :
>>> m 66 >>> n=int(hex(m)) Traceback (most recent call last): File "<interactive input>", line 1, in ? ValueError: invalid literal for int(): 0x42 >>> what am I missing here ?
Avnit's solution was wrong. When converting a string, you
must state what base you are converting from. int(hex(m),16)
66
Fredrik Lundh's solution works if the hex string starts with "0x"
(which it will when the string is created with the hex function).
int(hex(m),0)
66
But it won't work without the "0x".
int('0x1A',0)
26 int('0x1A',16)
26 int('1A',16)
26 int('1A',0)
Traceback (most recent call last):
File "<pyshell#18>", line 1, in -toplevel-
int('1A',0)
ValueError: invalid literal for int(): 1A
thank you Tim
avnit wrote:
If you just want to convert a string to an integer, it would be: >int(n) > >
in your case it would be: >m=66 >n=int(hex(m)) > > me********@aol.com wrote: tim wrote:
but then i get :
>>> m 66 >>> n=int(hex(m)) Traceback (most recent call last): File "<interactive input>", line 1, in ? ValueError: invalid literal for int(): 0x42 >>>
what am I missing here ?
Avnit's solution was wrong. When converting a string, you must state what base you are converting from. int(hex(m),16)
66
Fredrik Lundh's solution works if the hex string starts with "0x" (which it will when the string is created with the hex function).
aren't you converting from a hex string to a decimal value here?
On 2005-11-23, tim <ti******@skynet.be> wrote: >int(hex(m),16) > > 66
Fredrik Lundh's solution works if the hex string starts with "0x" (which it will when the string is created with the hex function).
aren't you converting from a hex string to a decimal value here?
No.
He's converting from a hex string to an integer object.
--
Grant Edwards grante Yow! It's OKAY --- I'm an
at INTELLECTUAL, too.
visi.com me********@aol.com wrote: Fredrik Lundh's solution works if the hex string starts with "0x"
that's what "interpret [it] as a Python literal" meant.
(which it will when the string is created with the hex function).
int(hex(m),0) 66
But it won't work without the "0x". int('0x1A',0) 26 int('0x1A',16) 26 int('1A',16) 26 int('1A',0)
Traceback (most recent call last): File "<pyshell#18>", line 1, in -toplevel- int('1A',0) ValueError: invalid literal for int(): 1A
as the error message says, 1A isn't a valid literal.
</F>
Fredrik Lundh wrote: me********@aol.com wrote:
Fredrik Lundh's solution works if the hex string starts with "0x" that's what "interpret [it] as a Python literal" meant.
I know from personal experience that the implications of that
sometimes go right over the head of a newbie. Did I do something
wrong by pointing out exactly what that means? Isn't it better to
have the OP understand the problem than simply solve it for him? (which it will when the string is created with the hex function).
>> int(hex(m),0) 66
But it won't work without the "0x".
>> int('0x1A',0) 26>> int('0x1A',16) 26>> int('1A',16) 26>> int('1A',0)
Traceback (most recent call last): File "<pyshell#18>", line 1, in -toplevel- int('1A',0) ValueError: invalid literal for int(): 1A
as the error message says, 1A isn't a valid literal.
Yes, not all strings of valid hex characters are valid literals.
But surely you're not suggesting that the user simply give
up when faced with that situation? </F> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: dpakpaul |
last post by:
Hi,
I have an XSD schema where I have attributes that are declared to
contain non string values such as integers etc. Take for example, this
declaration -
<xs:attribute name="IsThisTrue"...
|
by: Webgour |
last post by:
How to go from string "abc" to string "a|b|c"?
|
by: Chris Simmons |
last post by:
I know that a String is immutable, but I don't understand why this
piece of code fails in nUnit:
// BEGIN CODE
using System;
class Test
{
public static void Main( String args )
|
by: rsine |
last post by:
I have developed a program that sends a command through the serial port
to our business system and then reads from the buffer looking for a
number. Everything worked great on my WinXP system, but...
|
by: sernamar |
last post by:
Hi,
I have the following base class
class UnitOfMeasure
{
public:
//...
std::string& GetName() {return _uomName;};
//...
protected:
|
by: elliotng.ee |
last post by:
I have a text file that contains a header 32-bit binary. For example,
the text file could be:
%%This is the input text
%%test.txt
Date: Tue Dec 26 14:03:35 2006...
|
by: wizofaus |
last post by:
Given the following code:
public class Test
{
static unsafe void StringManip(string data)
{
fixed (char* ps = data)
ps = '$';
}
|
by: =?Utf-8?B?TGFzdGJ1aWxkZXJz?= |
last post by:
Hi all,
I have a weird problem which has been causing me a headache for the last two
days.
I have to dynamicly generate a schema in memory and load it into a dataset
in memory to be returned...
|
by: Anders Borum |
last post by:
Hi!
While implementing a property manager (that supports key / value pairs), I
was wondering how to constrain T to a struct or string type. Basically, I
guess what I'm looking for is the common...
|
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= |
last post by:
Hi
Is there any way to Read an INIFile from a string or Stream instead of a
physical file ???
I want to read the INIFile into a string then store in a db but when I read
the string from the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |