| | |
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
Views: 357 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt






