7 examples of 'python convert string to byte array' in Python

Every line of 'python convert string to byte array' 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
104def strtobyte(s):
105 return bytes(s, encoding="ascii")
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

34def _bytearray(data):
35
36 if type(data) == str:
37 return data
38 elif type(data) == list:
39 tmp = [chr(x) if type(x) == int else x for x in data]
40 return "".join(tmp)
41 elif type(data) == int:
42 tmp = ""
43 #for i in range(0,data):
44 # tmp = tmp + chr(0)
45 # return tmp
46 return [0] * data
47
48 return ""
64def YString2BytePython2x(strBuffer):
65 return strBuffer.encode("latin-1")
39def bytearray(source=string, encoding=string, errors=string):
40 return [number]
53def bytes(string, encoding=None):
54 """."""
55 return str(string)
43def to_bytes(s):
44 if bytes != str:
45 if type(s) == str:
46 return s.encode('utf-8')
47 return s
287def GetBytes(byte, size):
288 """Get a string of bytes of a given size
289
290 This handles the unfortunate different between Python 2 and Python 2.
291
292 Args:
293 byte: Numeric byte value to use
294 size: Size of bytes/string to return
295
296 Returns:
297 A bytes type with 'byte' repeated 'size' times
298 """
299 if sys.version_info[0] >= 3:
300 data = bytes([byte]) * size
301 else:
302 data = chr(byte) * size
303 return data

Related snippets