8 examples of 'python string to byte' in Python

Every line of 'python string to byte' 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
104def strtobyte(s):
105 return bytes(s, encoding="ascii")
352def ToChar(byte):
353 """Convert a byte to a character
355 This is useful because in Python 2 bytes is an alias for str, but in
356 Python 3 they are separate types. This function converts an ASCII value to
357 a value with the appropriate type in either case.
359 Args:
360 byte: A byte or str value
361 """
362 return chr(byte) if type(byte) != str else byte
79def int2byte(c):
80 return bytes([c])
23def int2bytes(x):
24 output = ""
25 while x != 0:
26 output += chr(x%0x100)
27 x >>= 8
28 return output
9def byte_to_int(byte): # already byte
10 return byte
481def byteb(byte):
482 return bstructx.byteb(byte)
64def YString2BytePython2x(strBuffer):
65 return strBuffer.encode("latin-1")
32def as_byte(data):
33 if sys.version_info < (3,):
34 return ord(data)
35 else:
36 return data

Related snippets