| | |
Create a string with Hex values
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
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
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)
new_address=hex(new_address)[2:].upper() if len(new_address)==3: new_address="0"+new_address elif len(new_address)==2: new_address="00"+new_address elif len(new_address)==1: new_address="000"+new_address print new_address
0
#2 Oct 26th, 2009
•
•
•
•
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
python Syntax (Toggle Plain Text)
>>> print '%04X' % 16 0010 >>> print '%04X' % 245 00F5 >>> print '%04X' % 1024 0400 >>> print '%04X' % 4096 1000
HTH
Last edited by jlm699; Oct 26th, 2009 at 9:46 am. Reason: Added explanation
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
0
#3 Oct 26th, 2009
Well jlm699 thank you !!!
your solution it was great
its similar to C syntax ...
and its only one line
your solution it was great
its similar to C syntax ...
and its only one line
Python Syntax (Toggle Plain Text)
new_address="%04X" % new_address
![]() |
Similar Threads
- Writing a String of Hex Files to a File (Java)
- Hex to String (C)
- comparing stings to string array values (C++)
- jTextField string of Hex characters to binary (Java)
- Convert string to to HEX (C)
- unsigned character pointer to Hex values (C++)
Other Threads in the Python Forum
- Previous Thread: Making an official-looking window
- Next Thread: Converting wxPython files to .exe
| Thread Tools | Search this Thread |
accessdenied advanced application argv beginner change color command convert csv cursor def dictionary digital dynamic dynamically edit editing enter event examples excel file float format frange function google gui homework i/o import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path port prime programming projects py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session simple skinning smtp sprite stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape windows wxpython






