944,031 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1130
  • Python RSS
Oct 26th, 2009
0

Create a string with Hex values

Expand Post »
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

Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamai is offline Offline
17 posts
since Oct 2009
Oct 26th, 2009
0
Re: Create a string with Hex values
Click to Expand / Collapse  Quote originally posted by iamai ...
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:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 26th, 2009
0
Re: Create a string with Hex values
Well jlm699 thank you !!!
your solution it was great
its similar to C syntax ...
and its only one line
Python Syntax (Toggle Plain Text)
  1. new_address="%04X" % new_address
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamai is offline Offline
17 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Making an official-looking window
Next Thread in Python Forum Timeline: Converting wxPython files to .exe





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC