Python from hex to str

hex to string formatting conversion in python

The key generated this way is most often a 32 character string, but once I’ve got 31 chars. This is what I don’t get: why it’s 32 chars, not 16? Doesn’t one hex digit take one character to print? So if I ask for %016x — shouldn’t one expect sixteen chars with possible leading zeroes? Why string legth is not always the same?

Test case

import random import collections stats = collections.defaultdict(int) for i in range(1000000): key = '%016x' % random.getrandbits(128) length = len(key) stats[length] += 1 for key in stats: print key, ' ', statsPython from hex to str 
32 937911 27 1 28 9 29 221 30 3735 31 58123 

2 Answers 2

Yes, but the format you’re using doesn’t truncate — you generate 128 random bits, which require (usually) 32 hex digits to show, and the %016 means AT LEAST 16 hex digits, but doesn’t just throw away the extra ones you need to show all of that 128-bit number. Why not generate just 64 random bits if that’s what you actually need? Less work for the random generator AND no formatting problems.

To satisfy your side curiosity, the length is occasionally 31 digits because 1 time in 16 the top 4 bits will all be 0; actually 1 time in 256 all the top 8 bits will be 0 so you’ll get only 30 digits, etc. You’ve only asked for 16 digits, so the formatting will give the least number that’s >= 16 and doesn’t require the truncation you have not asked for.

Читайте также:  Html фильтр для изображений

Each hex characters from 0 to F contains 4 bits of information, or half a byte. 128 bits is 16 bytes, and since it takes two hex characters to print a byte you get 32 characters. Your format string should thus be ‘%032x’ which will always generate a 32-character string, never shorter.

jkugelman$ cat rand.py #!/usr/bin/env python import random import collections stats = collections.defaultdict(int) for i in range(1000000): key = '%032x' % random.getrandbits(128) length = len(key) stats[length] += 1 for key in stats: print key, ' ', statsPython from hex to str jkugelman$ python rand.py 32 1000000 

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Python Hex to String

Python hex to String

The representation of data in Hexadecimal is commonly used in computer programming. It is used to represent binary data in a human-readable form, as understanding the machine language (i.e., Binary) is difficult for humans. Hexadecimal notation is used in networking protocols, e.g., IPv6 and cryptography, and widely used in graphics, e.g. to represent numbers. Note that hexadecimal uses 16 digits, including 0-9 and A-F , to represent numbers.

Considering the importance of hexadecimal, some programming languages and APIs require string representation of binary data. As a lot of data is in hexadecimal form, so we need to convert the hexadecimal number to a string. This conversion helps to display and manipulate the binary data as text , which makes the task more convenient for humans.

In python language, the hexadecimal can be converted into a string using the following:

  • bytes.fromhex() method
  • binascii module
  • codecs module
  • List comprehension

Let’s take a hexadecimal string, 48656c6c6f20576f726c64 ; this hexadecimal string represents the Hello World string. Below, the hexadecimal string is converted using different methods.

Using bytes.fromhex() Method

Use the bytes.fromhex() method to convert a hexadecimal string to a simple string in Python.

Источник

Convert from ASCII string encoded in Hex to plain ASCII?

How can I convert from hex to plain ASCII in Python? Note that, for example, I want to convert «0x7061756c» to «paul».

With the help of the link you just gave us, I found the function you were looking for. What exactly did you try and why didn’t it work?

I tried the following: >>> binascii.b2a_hqx(«0x7061756c») ‘-(Jh-$Ba0c8fB`’ >>> binascii.b2a_uu(«0x7061756c») «*,’@W,#8Q-S4V8P \n» >>> binascii.b2a_base64(«0x7061756c») ‘MHg3MDYxNzU2Yw==\n’ >>> binascii.b2a_qp(«0x7061756c») ‘0x7061756c’ >>> binascii.b2a_hex(«0x7061756c») ‘30783730363137353663’ >>> binascii.b2a_hex(0x7061756c) Traceback (most recent call last): File ««, line 1, in TypeError: must be string or buffer, not int >>>

Don’t you mean «7-bit» ASCII? (Which is sort of silly because ASCII is only 7-bits.) A GUID is 128bits.

9 Answers 9

A slightly simpler solution (python 2 only):

Thanks for pointing that out, I’m not as familiar with Python 3. This solution also won’t work in 1 as far as I know.

codecs.decode(«7061756c», «hex») works for Python 2 and Python 3. But it returns a bytes() string in Python 3. But that’s reasonable for an ASCII string.

No need to import any library:

>>> bytearray.fromhex("7061756c").decode() 'paul' 

Best solution for me (works with python 3) as it even accepts spaces : bytearray.fromhex(«70 61 75 6C»).decode()

bytearray.fromhex(«70e4756c»).decode(encoding=»Latin1″) ‘päul’ For those of us playing in binary, the extended characters choke on the default utf-8 decode, other than that, this is the most portable answer I see! Thanks!

Of course you have to know the actual encoding of the data if it is to be interpreted as text. Using ‘latin-1’ will get rid of any errors but may well produce complete gibberish if the text is not actually Latin-1.

In the interpreter, even the repr of the bytearray that is returned without .decode() is human readable, so for quickly checking something, you might get away without the .decode() .

or better bytes.fromhex(«7061756c»).decode() since you don’t need a mutable array and it’s less to type.

>>> txt = '7061756c' >>> ''.join([chr(int(''.join(c), 16)) for c in zip(txt[0::2],txt[1::2])]) 'paul' 

i’m just having fun, but the important parts are:

>>> int('0a',16) # parse hex 10 >>> ''.join(['a', 'b']) # join characters 'ab' >>> 'abcd'[0::2] # alternates 'ac' >>> zip('abc', '123') # pair up [('a', '1'), ('b', '2'), ('c', '3')] >>> chr(32) # ascii to character ' ' 

will look at binascii now.

>>> print binascii.unhexlify('7061756c') paul 

cool (and i have no idea why other people want to make you jump through hoops before they’ll help).

In Python 2:

In Python 3:

>>> bytes.fromhex('7061756c').decode('utf-8') 'paul' 

This is no different from bytes.fromhex() or bytearray.fromhex() . For both these types, .fromhex() is a classmethod.

Here’s my solution when working with hex integers and not hex strings:

def convert_hex_to_ascii(h): chars_in_reverse = [] while h != 0x0: chars_in_reverse.append(chr(h & 0xFF)) h = h >> 8 chars_in_reverse.reverse() return ''.join(chars_in_reverse) print convert_hex_to_ascii(0x7061756c) 

+1 for a useful example, but you are not converting «hex» as the input but you are converting any integer to a hex string. You code will work equally as well with print convert_hex_to_ascii(123456) .

Tested in Python 3.3.2 There are many ways to accomplish this, here’s one of the shortest, using only python-provided stuff:

import base64 hex_data ='57696C6C20796F7520636F6E76657274207468697320484558205468696E6720696E746F20415343494920666F72206D653F2E202E202E202E506C656565656173652E2E2E212121' ascii_string = str(base64.b16decode(hex_data))[2:-1] print (ascii_string) 

Of course, if you don’t want to import anything, you can always write your own code. Something very basic like this:

ascii_string = '' x = 0 y = 2 l = len(hex_data) while y  

Источник

How to convert from hex-encoded string to a "human readable" string?

I'm using the Net-SNMP bindings for python and I'm attempting to grab an ARP cache from a Brocade switch. Here's what my code looks like:

#!/usr/bin/env python import netsnmp def get_arp(): oid = netsnmp.VarList(netsnmp.Varbind('ipNetToMediaPhysAddress')) res = netsnmp.snmpwalk(oid, Version=2, DestHost='10.0.1.243', Community='public') return res arp_table = get_arp() print arp_table 
 IP-MIB::ipNetToMediaPhysAddress.128.10.200.6.158 = STRING: 0:1b:ed:a3:ec:c1 IP-MIB::ipNetToMediaPhysAddress.129.10.200.6.162 = STRING: 0:1b:ed:a4:ac:c1 IP-MIB::ipNetToMediaPhysAddress.130.10.200.6.166 = STRING: 0:1b:ed:38:24:1 IP-MIB::ipNetToMediaPhysAddress.131.10.200.6.170 = STRING: 74:8e:f8:62:84:1 
('\x00$8C\x98\xc1', '\x00\x1b\xed;_A', '\x00\x1b\xed\xb4\x8f\x81', '\x00$86\x15\x81', '\x00$8C\x98\x81', '\x00\x1b\xed\x9f\xadA', . etc) 

I've spent some time googling and came across the struct module and the .decode("hex") string method, but the .decode("hex") method doesn't seem to work:

Python 2.7.3 (default, Apr 10 2013, 06:20:15) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> hexstring = '\x00$8C\x98\xc1' >>> newstring = hexstring.decode("hex") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode output = binascii.a2b_hex(input) TypeError: Non-hexadecimal digit found >>> 

Источник

Оцените статью