

You can refer to the below screenshot for the output. To print the string to byte array hex, I have used print(newstring). The omhex() this method returns a required string containing hex numbers.In this example, I have taken a string as string = “AB CD EF” to convert string to byte array hex, I have used newstring = omhex(string).Let’s see how to convert string to byte array hex in python Python string to byte array UTF-16 Python string to byte array hex You can see the utf-16 formatted string as the output in the below screenshot. To print the converted string, I have used print(newstring). The bytearray() method returns the byte array object. I have used newstring = bytes(string, ‘utf-16’). In this example, I have taken a string as “Python guides”.Here, we can see how to convert string to byte array UTF-16 in python Read: Python NumPy concatenate Python string to byte array UTF-16 To convert the string into UTF-8, I have used newstring = bytes(string, ‘utf-8’). In this example, I have taken a string as “Python guides”. Now, we can see how to convert a string to byte array utf-8 in python Python string to byte array encoding Python string to byte array UTF-8
