Creating a 2s compliment hex string for negitive numbers

2014-08-01 Thread bSneddon
I need to calculate an error correction code for an old protocol. I calculate the integer 4617 and want to code the 2s compliment in ASCII hex EDF7. When issue the following. hex(-4617) '-0x1209' Does anyone know a clean way to get to the desired results? My ECC will always be 16 bit (4

Re: Creating a 2s compliment hex string for negitive numbers

2014-08-01 Thread MRAB
On 2014-08-01 21:35, bSneddon wrote: I need to calculate an error correction code for an old protocol. I calculate the integer 4617 and want to code the 2s compliment in ASCII hex EDF7. When issue the following. hex(-4617) '-0x1209' Does anyone know a clean way to get to the desired

Re: Creating a 2s compliment hex string for negitive numbers

2014-08-01 Thread bSneddon
On Friday, August 1, 2014 4:47:20 PM UTC-4, MRAB wrote: On 2014-08-01 21:35, bSneddon wrote: I need to calculate an error correction code for an old protocol. I calculate the integer 4617 and want to code the 2s compliment in ASCII hex EDF7. When issue the following.

Integer as raw hex string?

2012-12-24 Thread Roy Smith
I have an integer that I want to encode as a hex string, but I don't want 0x at the beginning, nor do I want L at the end if it happened to be a long. The result needs to be something I can pass to int(h, 16) to get back my original integer. The brute force way works: h = hex(i) assert

Re: Integer as raw hex string?

2012-12-24 Thread Tim Chase
On 12/24/12 09:36, Roy Smith wrote: I have an integer that I want to encode as a hex string, but I don't want 0x at the beginning, nor do I want L at the end if it happened to be a long. The result needs to be something I can pass to int(h, 16) to get back my original integer. The brute

Re: Integer as raw hex string?

2012-12-24 Thread Roy Smith
In article mailman.1256.1356364625.29569.python-l...@python.org, Tim Chase python.l...@tim.thechases.com wrote: On 12/24/12 09:36, Roy Smith wrote: I have an integer that I want to encode as a hex string, but I don't want 0x at the beginning, nor do I want L at the end if it happened

Re: Integer as raw hex string?

2012-12-24 Thread MRAB
On 2012-12-24 15:58, Tim Chase wrote: On 12/24/12 09:36, Roy Smith wrote: I have an integer that I want to encode as a hex string, but I don't want 0x at the beginning, nor do I want L at the end if it happened to be a long. The result needs to be something I can pass to int(h, 16) to get back

Hex String

2010-05-10 Thread Anthony Cole
How can I concatenate 2 hex strings (e.g. '\x16' and '\xb9') then convert the answer to an integer? When I try i always end up with the ASCII equivalent! Thanks, Anthony -- http://mail.python.org/mailman/listinfo/python-list

Re: Hex String

2010-05-10 Thread Chris Rebert
On Mon, May 10, 2010 at 12:55 PM, Anthony Cole anthony.c...@gmail.com wrote: How can I concatenate 2 hex strings (e.g. '\x16' and '\xb9') then convert the answer to an integer? When I try i always end up with the ASCII equivalent! I think you want the `struct` module: struct — Interpret

Re: Hex String

2010-05-10 Thread MRAB
Anthony Cole wrote: How can I concatenate 2 hex strings (e.g. '\x16' and '\xb9') then convert the answer to an integer? When I try i always end up with the ASCII equivalent! Those are just bytestrings (assuming you're using Python 2.x), ie strings using 1 byte per character. You can convert

Concise way to format list/array to custom(hex) string

2008-08-02 Thread Kurien Mathew
Hello, What will be a concise efficient way to convert a list/array.array of n elements into a hex string? For e.g. given the bytes [116, 111, 110, 103, 107, 97] I would like the formatted string 0x74 0x6f 0x6e 0x67 0x6b 0x61 Is there an approach better than below: hex = '' for b in bytes

Re: Concise way to format list/array to custom(hex) string

2008-08-02 Thread John Machin
On Aug 2, 9:53 pm, Kurien Mathew [EMAIL PROTECTED] wrote: Hello, What will be a concise efficient way to convert a list/array.array of n elements into a hex string? For e.g. given the bytes [116, 111, 110, 103, 107, 97] I would like the formatted string 0x74 0x6f 0x6e 0x67 0x6b 0x61

Re: Concise way to format list/array to custom(hex) string

2008-08-02 Thread Zoltán Nagy
Kurien Mathew írta: Hello, What will be a concise efficient way to convert a list/array.array of n elements into a hex string? For e.g. given the bytes [116, 111, 110, 103, 107, 97] I would like the formatted string 0x74 0x6f 0x6e 0x67 0x6b 0x61 Is there an approach better than below

Re: Concise way to format list/array to custom(hex) string

2008-08-02 Thread Grant Edwards
On 2008-08-02, Zoltán Nagy [EMAIL PROTECTED] wrote: Kurien Mathew írta: Hello, What will be a concise efficient way to convert a list/array.array of n elements into a hex string? For e.g. given the bytes [116, 111, 110, 103, 107, 97] I would like the formatted string 0x74 0x6f 0x6e 0x67

Re: Concise way to format list/array to custom(hex) string

2008-08-02 Thread josh logan
On Aug 2, 9:29 am, Grant Edwards [EMAIL PROTECTED] wrote: On 2008-08-02, Zoltán Nagy [EMAIL PROTECTED] wrote: Kurien Mathew írta: Hello, What will be a concise efficient way to convert a list/array.array of n elements into a hex string? For e.g. given the bytes [116, 111, 110

Re: char string 2 hex string

2008-02-01 Thread Antonio Chay
On Jan 31, 7:09 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: Antonio Chay [EMAIL PROTECTED] writes: AAA should be 414141 'AAA'.encode('hex') 8-O Cool stuff! Thanks! -- http://mail.python.org/mailman/listinfo/python-list

char string 2 hex string

2008-01-31 Thread Antonio Chay
Hello! I need to transform a string from a file into a hexadecimal representation, for example: AAA should be 414141 With perl I do this with: unpack(H*,AAA) And with python I got this: .join([str(hex(ord(x)))[2:] for x in AAA]) But seems a little weird for me. Is there another way? Thanks

Re: char string 2 hex string

2008-01-31 Thread Paul Rubin
Antonio Chay [EMAIL PROTECTED] writes: AAA should be 414141 'AAA'.encode('hex') -- http://mail.python.org/mailman/listinfo/python-list

Re: char string 2 hex string

2008-01-31 Thread Jared Grubb
You could also do: .join(['%02x' % ord(c) for c in 'AAA']) On 31 Jan 2008, at 14:09, Paul Rubin wrote: Antonio Chay [EMAIL PROTECTED] writes: AAA should be 414141 'AAA'.encode('hex') -- http://mail.python.org/mailman/listinfo/python-list On 31 Jan 2008, at 14:05, Antonio Chay wrote:

Re: Converting binary sid's to a hex string

2006-06-09 Thread LittlePython
to a hex string. ADsSID com object will do this for me however it is a little slow. Can anybody point me in the right direction to converting a PySID to a hexstring. Thx import win32netcon import win32net import win32security def report(): resume = 0 while 1: filter

Converting binary sid's to a hex string

2006-06-07 Thread LittlePython
I am trying to create a hexstring of a NT4 user account sid which I can in turn use to query an exchange 55 database. I believe I need to convert a binary sid to a hex string. ADsSID com object will do this for me however it is a little slow. Can anybody point me in the right direction

Re: Converting binary sid's to a hex string

2006-06-07 Thread Roger Upole
an exchange 55 database. I believe I need to convert a binary sid to a hex string. ADsSID com object will do this for me however it is a little slow. Can anybody point me in the right direction to converting a PySID to a hexstring. Thx import win32netcon import win32net import win32security def

Re: hex string to hex value

2005-11-23 Thread [EMAIL PROTECTED]
Fredrik Lundh wrote: [EMAIL PROTECTED] 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

hex string to hex value

2005-11-22 Thread tim
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:

Re: hex string to hex value

2005-11-22 Thread avnit
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)) -- http://mail.python.org/mailman/listinfo/python-list

Re: hex string to hex value

2005-11-22 Thread Fredrik Lundh
tim [EMAIL PROTECTED] wrote: 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,

Re: hex string to hex value

2005-11-22 Thread tim
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)

Re: hex string to hex value

2005-11-22 Thread [EMAIL PROTECTED]
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

[Fwd: Re: hex string to hex value]

2005-11-22 Thread tim
ok, but if i do n=66 m=hex(n) m '0x42' h=int(m,16) h 66 I end up with 66 again, back where I started, a decimal, right? I want to end up with 0x42 as being a hex value, not a string, so i can pas it as an argument to a function that needs a hex value. (i am trying to replace the

Re: hex string to hex value

2005-11-22 Thread [EMAIL PROTECTED]
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

Re: hex string to hex value

2005-11-22 Thread tim
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? -- http

Re: [Fwd: Re: hex string to hex value]

2005-11-22 Thread Rocco Moretti
tim wrote: ok, but if i do n=66 m=hex(n) m '0x42' h=int(m,16) h 66 I end up with 66 again, back where I started, a decimal, right? I want to end up with 0x42 as being a hex value, not a string, so i can pas it as an argument to a function that needs a hex value. (i am

Re: [Fwd: Re: hex string to hex value]

2005-11-22 Thread Brett g Porter
tim wrote: I end up with 66 again, back where I started, a decimal, right? I want to end up with 0x42 as being a hex value, not a string, so i can pas it as an argument to a function that needs a hex value. (i am trying to replace the 0x42 in the line midi.note_off(channel=0, note=0x42)

Re: [Fwd: Re: hex string to hex value]

2005-11-22 Thread tim
Brett g Porter wrote: tim wrote: I end up with 66 again, back where I started, a decimal, right? I want to end up with 0x42 as being a hex value, not a string, so i can pas it as an argument to a function that needs a hex value. (i am trying to replace the 0x42 in the line

Re: hex string to hex value

2005-11-22 Thread Grant Edwards
On 2005-11-23, tim [EMAIL PROTECTED] 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

Re: hex string to hex value

2005-11-22 Thread Fredrik Lundh
[EMAIL PROTECTED] 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

Re: hex string into binary format?

2005-04-01 Thread Tim Roberts
Tertius Cronje [EMAIL PROTECTED] wrote: How do I get a hexvalued string to a format recognized for binary calculation? You're going to be embarrassed. import binascii s1 = '1C46BE3D9F6AA820' s2 = '8667B5236D89CD46' i1 = binascii.unhexlify(s1) i2 = binascii.unhexlify(s2) x = i1 ^i2

hex string into binary format?

2005-03-31 Thread Tertius Cronje
Hi, How do I get a hexvalued string to a format recognized for binary calculation? import binascii s1 = '1C46BE3D9F6AA820' s2 = '8667B5236D89CD46' i1 = binascii.unhexlify(s1) i2 = binascii.unhexlify(s2) x = i1 ^i2 TypeError: unsupported operand type(s) for ^: 'str' and 'str' Many

Re: hex string into binary format?

2005-03-31 Thread Peter Hansen
Tertius Cronje wrote: How do I get a hexvalued string to a format recognized for binary calculation? import binascii s1 = '1C46BE3D9F6AA820' s2 = '8667B5236D89CD46' i1 = binascii.unhexlify(s1) i2 = binascii.unhexlify(s2) Try this instead: i1 = long(s1, 16) i2 = long(s2, 16) x = i1 ^i2 --

Re: Help need with converting Hex string to IEEE format float

2004-12-17 Thread Ian Vincent
Max M [EMAIL PROTECTED] wrote in news:41bf121e$0$280 [EMAIL PROTECTED]: ## st = '80 00 00 00' import binascii import struct s = ''.join([binascii.a2b_hex(s) for s in st.split()]) v = struct.unpack(f, s)[0] print v ## This one worked great for what I was trying to do. Thanks to

Re: Help need with converting Hex string to IEEE format float

2004-12-15 Thread TZOTZIOY
On Tue, 14 Dec 2004 16:57:02 +0100, rumours say that Fredrik Lundh [EMAIL PROTECTED] might have written: how about: # convert to byte string import struct s = .join([chr(int(c, 16)) for c in x]) v = struct.unpack(!f, s) I think that the third line in the snippet above could also

Re: Help need with converting Hex string to IEEE format float

2004-12-15 Thread Peter Hansen
Christos TZOTZIOY Georgiou wrote: s = .join(x).decode(hex) I am not sure I remember in which version of Python the hex codec was added, but it is handy. Of course, binascii could do this since 2.0 or so, but not having to import another module *is* nice: 'ff12'.decode('hex') '\xff\x12' import

Re: Help need with converting Hex string to IEEE format float

2004-12-14 Thread Fredrik Lundh
Max M wrote: Oh, programmers loves this kind stuff. You should get tons of answers. data = '80 00 00 00' import Image v = Image.fromstring(F, (1, 1), data, hex, F;32BF).getpixel((0, 0)) /F -- http://mail.python.org/mailman/listinfo/python-list

Help need with converting Hex string to IEEE format float

2004-12-13 Thread i_vincent
Hi all, Newbie Python programmer here, so please be patient. I have spent all day googling for an answer to my problem, but everything I try fails to work (or works from the Interpreter with a set value but not from my code with dynamic values). Okay, here is the general gist of the problem. I

Re: Help need with converting Hex string to IEEE format float

2004-12-13 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: Newbie Python programmer here, so please be patient. I have spent all day googling for an answer to my problem, but everything I try fails to work (or works from the Interpreter with a set value but not from my code with dynamic values). Okay, here is the general