Editing
Python programming
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Formatting with bits, decimals, and hexadecimals === <big>Convert text numbers into text hexadecimal values</big> <pre>>>> f = '{:02x}' >>> f.format(255) 'ff' >>> f.format('255') < -- cannot directly convert from string number to hexadecimal number Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> f.format('255') ValueError: Unknown format code 'x' for object of type 'str' >>> f.format(int('255')) < -- cast string number into integer then apply string formatting 'ff' < -- hexadecimal string</pre> <big>Randomly create some bits then format as hexadecimal. Then dynamically specify format using nested parametrized formats.</big> <pre>>>> from random import getrandbits >>> '0x{0:08x}'.format(getrandbits(64)) '0x43c5e5b009f7dc1b' >>> '0x{0:08x}'.format(getrandbits(64)) '0xce3d5ffeaf7d693b' >>> '0x{:08x}'.format(getrandbits(64)) '0x9b3fcfc7b31b9c1a' >>> '0x{:0{width}x}'.format(getrandbits(64), width=8) '0x8f6aae777885c555' >>> '{:0{width}x}'.format(getrandbits(64), width=8) 'f5f900fba39f78ef' >>> '{:0{width}x}'.format(getrandbits(64), width=8) '4f3f855b2d6f4abd' >>> '{:0{width}x}'.format(getrandbits(32), width=8) '7791644a' >>> '{:0{width}x}'.format(getrandbits(32), width=16) '00000000b498791e' >>> def get_random_hex_string(length_of_hex): return '{:0{length}x}'.format(getrandbits(length_of_hex * 4), length=length_of_hex) >>> get_random_hex_string(8) 'c61e3d06' >>> get_random_hex_string(16) '0c81d7dda113c0fd' >>> get_random_hex_string(32) '270cb00a17f5dfc79f65b15ebfd57195' >>> get_random_hex_string(64) 'eb06490b3d208afed79c1f380ab6c7f9c23c16319aa0928d6ac3609fa2c620af'</pre> <big>Create a function to randomly return a hexadecimal string of varied length with integer parameter.</big> <pre>def get_random_hex_string(length_of_hex): """ Function takes the desired total length of hexadecimal characters and returns text string composed of random hexadecimal characters. :param length_of_hex: integer representing the total number of hexadecimal digits desired :return: text string of precise length and random hexadecimal values """ return '{:0{length}x'.format(getrandbits(length_of_hex * 4), length=length_of_hex)</pre> <big>Create random IPv4 address an octet at a time</big> <pre>>>> from random import getrandbits >>> hf = '{:02x}' >>> hl = list() >>> for i in range(4): hl.append(hf.format(getrandbits(8))) < -- build a list of four random octets (32 bits) >>> hl ['73', 'dc', 'd9', '36'] >>> ''.join(hl) < -- join the octets '73dcd936' < -- IPv4 representation in compact hexadecimal format</pre>
Summary:
Please note that all contributions to GotOpinion may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
GotOpinion:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information