10 examples of 'python string to hex' in Python

Every line of 'python string 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
this disclaimer
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
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

45def to_hex(number):
46 if number is not None:
47 h = hex(number)
48 if h.endswith('L'):
49 h = h[:-1]
50 else:
51 h = None
52 return h
24def to_hexstr(data):
25 return ' '.join('%02x' % b for b in data)
35def int_to_hex(i):
36 return format(i, 'x')
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))
13def bin2hex(s):
14 return '%0*X' % ((len(s) + 3) // 4, int(s, 2))
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)
81def to_hex(self):
82 # Convert to hexadecimal string representation
83 alpha = '' if self._alpha >= 1.0 else '{:02X}'.format(int(self._alpha*255))
84 return '#{:02X}{:02X}{:02X}{a}'.format(*(int(c*255) for c in self._rgba[:3]), a=alpha)
51def get_hex_string(value):
52 v = '0x'
53 for c in value:
54 new_hex = hex(ord(c))[2:].upper()
55 if len(new_hex) < 2:
56 new_hex = '0' + new_hex
57 v += new_hex
58 return v
58def hex(x):
59 return int(x, 16)

Related snippets