Create a string with Hex values

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 4
Reputation: iamai is an unknown quantity at this point 
Solved Threads: 0
iamai iamai is offline Offline
Newbie Poster

Create a string with Hex values

 
0
  #1
Oct 26th, 2009
Hello guys

I want to convert an integer to a hexadecimal string
examples
lets say i have an integer value 1 i want to convert it to 0001
integer value of 16 => 0010
245 =>00F5
and so on the string must have a length of 4 digits
my code is like this but i was wondering if there is an another way to do it

  1. new_address=hex(new_address)[2:].upper()
  2. if len(new_address)==3:
  3. new_address="0"+new_address
  4. elif len(new_address)==2:
  5. new_address="00"+new_address
  6. elif len(new_address)==1:
  7. new_address="000"+new_address
  8. print new_address
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #2
Oct 26th, 2009
Originally Posted by iamai View Post
I want to convert an integer to a hexadecimal string
examples
lets say i have an integer value 1 i want to convert it to 0001
integer value of 16 => 0010
245 =>00F5
and so on the string must have a length of 4 digits
It will be way simpler to use string formatting like so:
  1. >>> print '%04X' % 16
  2. 0010
  3. >>> print '%04X' % 245
  4. 00F5
  5. >>> print '%04X' % 1024
  6. 0400
  7. >>> print '%04X' % 4096
  8. 1000
I'm sure you know already how to use the % symbol in strings to substitute values; however you may only know the basic usage. In this particular example we use a number of properties. First, the 0 denotes that we want our string to be padded with zeroes. Then we have a 4, which indicates we're reserving 4 spaces (if we simply had a 4 without the 0 there would be a blank space instead of a zero). And then finally we have the X. This indicates we're formatting the input (in this case an integer) to print out in hexadecimal. If we had used a lower-case X (ie, x) then the output would use lower-case hexadecimal digits.

HTH
Last edited by jlm699; Oct 26th, 2009 at 9:46 am. Reason: Added explanation
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: iamai is an unknown quantity at this point 
Solved Threads: 0
iamai iamai is offline Offline
Newbie Poster
 
0
  #3
Oct 26th, 2009
Well jlm699 thank you !!!
your solution it was great
its similar to C syntax ...
and its only one line
  1. new_address="%04X" % new_address
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 357 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC