10 examples of 'python byte to hex' in Python

Every line of 'python byte to hex' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
28def _hex_to_bin_byte(hex_byte):
29 #TODO2: Absolutely sure there is a library function for this
30 hex_down = '0123456789abcdef'
31 hex_up = '0123456789ABDCEF'
32 value = 0
33 for i in xrange(2):
34 value *= 16
35 try:
36 value += hex_down.index(hex_byte[i])
37 except ValueError:
38 try:
39 value += hex_up.index(hex_byte[i])
40 except ValueError:
41 raise IdError
42 return chr(value)
25def hex2bin(hexstr):
26 """Convert a hexdecimal string to binary string, with zero fillings. """
27 num_of_bits = len(hexstr) * 4
28 binstr = bin(int(hexstr, 16))[2:].zfill(int(num_of_bits))
29 return binstr
761def format_hex(byte_str):
762 if sys.version_info >= (3, 0):
763 return ' '.join( [ "{:02X}".format(x) for x in byte_str ] )
764 else:
765 return ' '.join( [ "{:02X}".format(ord(x)) for x in byte_str ] )
13def bin2hex(s):
14 return '%0*X' % ((len(s) + 3) // 4, int(s, 2))
18def hex_to_binary(hex_string):
19 return ''.join([b.decode('hex') for b in hex_string.split(" ")])
148def bytes_to_hex(str_as_bytes):
149 return binascii.hexlify(str_as_bytes)
11def to_hex(data):
12 try:
13 data = memoryview(data)
14 except TypeError:
15 data = memoryview(bytes(data))
16 if dump_hex.limit is None or len(data) < dump_hex.limit:
17 return data.hex()
18 else:
19 return "{}... ({} bytes total)".format(
20 data[:dump_hex.limit].hex(), len(data))
292def hexToString(hexString):
293 '''
294 Simple method to convert an hexadecimal string to ascii string
295
296 @param hexString: A string in hexadecimal format
297 @return: A tuple (status,statusContent), where statusContent is an ascii string in case status = 0 or an error in case status = -1
298 '''
299 string = ''
300 if len(hexString) % 2 != 0:
301 hexString = '0'+hexString
302 try:
303 for i in range(0,len(hexString),2):
304 string += chr(int(hexString[i]+hexString[i+1],16))
305 except:
306 return (-1,'Error in hexadecimal conversion')
307 return (0,string)
13def hex_to_bin(s):
14 return binascii.unhexlify(s.replace(' ', ''))
24def to_hexstr(data):
25 return ' '.join('%02x' % b for b in data)

Related snippets