Re: [Tutor] Conversion question
On Tue, Jun 16, 2009 at 12:46 PM, xchime...@gmail.com wrote: Quick question. Say I have a string a=Man and I want to print the string in base2. Is there a python function like there is in perl to do this? Thanks in advance for any input do you mean like this: In [23]: int('Man', 2) --- ValueErrorTraceback (most recent call last) /home/wayne/ipython console in module() ValueError: invalid literal for int() with base 2: 'Man' Or do you mean this? In [24]: mystr = 'some string' In [25]: mystr.encode('hex') Out[25]: '736f6d6520737472696e67' HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Thanks for the reply I would like to print the string in binary Man=01001101011101101110 Sent from my Verizon Wireless BlackBerry -Original Message- From: Wayne sri...@gmail.com Date: Tue, 16 Jun 2009 13:13:58 To: xchime...@gmail.com Cc: tutor@python.org Subject: Re: [Tutor] Conversion question On Tue, Jun 16, 2009 at 12:46 PM, xchime...@gmail.com wrote: Quick question. Say I have a string a=Man and I want to print the string in base2. Is there a python function like there is in perl to do this? Thanks in advance for any input do you mean like this: In [23]: int('Man', 2) --- ValueErrorTraceback (most recent call last) /home/wayne/ipython console in module() ValueError: invalid literal for int() with base 2: 'Man' Or do you mean this? In [24]: mystr = 'some string' In [25]: mystr.encode('hex') Out[25]: '736f6d6520737472696e67' HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
xchime...@gmail.com wrote: Thanks for the reply I would like to print the string in binary Man=01001101011101101110 What's M in binary? Nobody knows... What's M in encoded in 8-bit ASCII string: '0b1001101' Source: bin(ord('M')) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Correct 8-bit ASCII. Sorry about that. I am using Python 2.5.2, which doesn't support bin. If I upgraded how would I go about converting the entire string to 8-bit ASCII? I appreciate your help. On Tue, Jun 16, 2009 at 3:05 PM, Lie Ryan lie.1...@gmail.com wrote: xchime...@gmail.com wrote: Thanks for the reply I would like to print the string in binary Man=01001101011101101110 What's M in binary? Nobody knows... What's M in encoded in 8-bit ASCII string: '0b1001101' Source: bin(ord('M')) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
On Tue, Jun 16, 2009 at 2:25 PM, Tom Green xchime...@gmail.com wrote: Correct 8-bit ASCII. Sorry about that. I am using Python 2.5.2, which doesn't support bin. If I upgraded how would I go about converting the entire string to 8-bit ASCII? I appreciate your help. you write the conversion yourself. 1. # convert a decimal (denary, base 10) integer to a binary string (base 2) 2. # tested with Python24 vegaseat6/1/2005 3. 4. def Denary2Binary(n): 5. '''convert denary integer n to binary string bStr''' 6. bStr = '' 7. if n 0: raise ValueError, must be a positive integer 8. if n == 0: return '0' 9. while n 0: 10. bStr = str(n % 2) + bStr 11. n = n 1 12. return bStr 13. 14. def int2bin(n, count=24): 15. returns the binary of integer n, using count number of digits 16. return .join([str((n y) 1) for y in range(count-1, -1, -1)]) 17. 18. # this test runs when used as a standalone program, but not as an imported module 19. # let's say you save this module as den2bin.py and use it in another program 20. # when you import den2bin the __name__ namespace would now be den2bin and the 21. # test would be ignored 22. if __name__ == '__main__': 23. print Denary2Binary(255) # 24. 25. # convert back to test it 26. print int(Denary2Binary(255), 2) # 255 27. 28. print 29. 30. # this version formats the binary 31. print int2bin(255, 12) # 32. # test it 33. print int(, 2) # 255 34. 35. print 36. 37. # check the exceptions 38. print Denary2Binary(0) 39. print Denary2Binary(-5) # should give a ValueError from http://www.daniweb.com/code/snippet285.html# HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Thanks I just happened to find the site myself. I guess I have to pass each character to the function and build the 8-bit ASCII string or is there a better way to do it? On Tue, Jun 16, 2009 at 3:37 PM, Wayne sri...@gmail.com wrote: On Tue, Jun 16, 2009 at 2:25 PM, Tom Green xchime...@gmail.com wrote: Correct 8-bit ASCII. Sorry about that. I am using Python 2.5.2, which doesn't support bin. If I upgraded how would I go about converting the entire string to 8-bit ASCII? I appreciate your help. you write the conversion yourself. anks 1. # convert a decimal (denary, base 10) integer to a binary string (base 2) 2. # tested with Python24 vegaseat6/1/2005 3. 4. def Denary2Binary(n): 5. '''convert denary integer n to binary string bStr''' 6. bStr = '' 7. if n 0: raise ValueError, must be a positive integer 8. if n == 0: return '0' 9. while n 0: 10. bStr = str(n % 2) + bStr 11. n = n 1 12. return bStr 13. 14. def int2bin(n, count=24): 15. returns the binary of integer n, using count number of digits 16. return .join([str((n y) 1) for y in range(count-1, -1, -1)]) 17. 18. # this test runs when used as a standalone program, but not as an imported module 19. # let's say you save this module as den2bin.py and use it in another program 20. # when you import den2bin the __name__ namespace would now be den2bin and the 21. # test would be ignored 22. if __name__ == '__main__': 23. print Denary2Binary(255) # 24. 25. # convert back to test it 26. print int(Denary2Binary(255), 2) # 255 27. 28. print 29. 30. # this version formats the binary 31. print int2bin(255, 12) # 32. # test it 33. print int(, 2) # 255 34. 35. print 36. 37. # check the exceptions 38. print Denary2Binary(0) 39. print Denary2Binary(-5) # should give a ValueError from http://www.daniweb.com/code/snippet285.html# HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Tom Green wrote: Correct 8-bit ASCII. Sorry about that. I am using Python 2.5.2, which doesn't support bin. If I upgraded how would I go about converting the entire string to 8-bit ASCII? AFAIK, earlier versions of python does not have a function/module that converts a number to its binary representation; so you might have to build your own function there. The concept of base-2 conversion is simple, the modulus for powers of 2. def bin(n): ... if n == 0: return '0' ... if n == 1: return '1' ... return mybin(n // 2) + str(n % 2) (that function is simple, but might be slow if you had to concatenate large strings) or generally: def rebase(n, base=2): ''' Convert a positive integer to number string with base `base` ''' if 0 = n base: return str(n) return rebase(n // base, base) + str(n % base) than you simply map your string to ord and the bin function, ''.join, and done. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Le Tue, 5 May 2009 00:52:11 +0100, Alan Gauld alan.ga...@btinternet.com s'exprima ainsi: Is there a way in Python to say this is a string of HEX characters like Perl's pack? Right now I have to take the string and add a \x to every two values i.e. \x41\x42... Assuming you actually want to send the hex values rather than a hex string representation then the way I'd send that would be to convert that to a number using int() then transmit it using struct() I can hardly imagine why you want an hex string representation for further process instead of the values, neither. Would be interested in the reason for this. Assuming that an interface require hex string, wouldn't it be easier to change this so that it directly gets values? Id est avoid double format conversion. Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Emile van Sebille em...@fenx.com wrote 414243440d0a Is there a way in Python to say this is a string of HEX characters like Perl's pack? Right now I have to take the string and add a \x to every two values i.e. \x41\x42... import binascii binascii.a2b_hex('41424344') I hadn't come across binascii before, but it doesn't do what I expected: import binascii as b b.a2b_hex('414243440d0a') 'ABCD\r\n' This appears to be converting it to a binary value then displaying that binary value as an ascii string. I'm not sure what the value of that is over struct or int? Can anyone enlighten me about why I'd ever want to use this? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Thanks everyone for your replies. This reason for the long string is sometimes I have to pass a 32 byte key (MD5 hash value) to the socket. The data I am sending is the hex values i.e. 41=A. I believe the binascii will work. Mike On Mon, May 4, 2009 at 7:52 PM, Alan Gauld alan.ga...@btinternet.comwrote: Tom Green xchime...@gmail.com wrote Here is my question. I work with a lot of sockets and most of them require hex data. I am usually given a string of data to send to the socket. Example: 414243440d0a Is there a way in Python to say this is a string of HEX characters like Perl's pack? Right now I have to take the string and add a \x to every two values i.e. \x41\x42... Assuming you actually want to send the hex values rather than a hex string representation then the way I'd send that would be to convert that to a number using int() then transmit it using struct() Sometimes my string values are 99+ bytes in length. I did write a parsing program that would basically loop thru the string and insert the \x, but I was wondering if there was another or better way. OK, Maybe you do want to send the hex representation rather than the actual data (I can't think why unless you have a very strange parser at the other end). In that case I think you do need to insert the \x characters. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
On 5/4/2009 4:17 PM Tom Green said... First, thanks in advance for any insight on how to assist in making me a better Python programmer. Here is my question. I work with a lot of sockets and most of them require hex data. I am usually given a string of data to send to the socket. Example: 414243440d0a Is there a way in Python to say this is a string of HEX characters like Perl's pack? Right now I have to take the string and add a \x to every two values i.e. \x41\x42... import binascii binascii.a2b_hex('41424344') Emile ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Thank you, I didn't realize it was that easy. I tried binascii before and I thought it didn't work properly. I appreciate it. Mike. On Mon, May 4, 2009 at 7:40 PM, Emile van Sebille em...@fenx.com wrote: On 5/4/2009 4:17 PM Tom Green said... First, thanks in advance for any insight on how to assist in making me a better Python programmer. Here is my question. I work with a lot of sockets and most of them require hex data. I am usually given a string of data to send to the socket. Example: 414243440d0a Is there a way in Python to say this is a string of HEX characters like Perl's pack? Right now I have to take the string and add a \x to every two values i.e. \x41\x42... import binascii binascii.a2b_hex('41424344') Emile ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Tom Green xchime...@gmail.com wrote Here is my question. I work with a lot of sockets and most of them require hex data. I am usually given a string of data to send to the socket. Example: 414243440d0a Is there a way in Python to say this is a string of HEX characters like Perl's pack? Right now I have to take the string and add a \x to every two values i.e. \x41\x42... Assuming you actually want to send the hex values rather than a hex string representation then the way I'd send that would be to convert that to a number using int() then transmit it using struct() Sometimes my string values are 99+ bytes in length. I did write a parsing program that would basically loop thru the string and insert the \x, but I was wondering if there was another or better way. OK, Maybe you do want to send the hex representation rather than the actual data (I can't think why unless you have a very strange parser at the other end). In that case I think you do need to insert the \x characters. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor