Sobes.tech
Junior — Middle

How can you convert a string value into a byte array in your chosen programming language?

sobes.tech AI

Answer from AI

In Python, the .encode() method is used to convert a string into a byte array. By default, UTF-8 encoding is used, but you can specify a different one.

Example:

text = "Пример"
byte_array = text.encode('utf-8')
print(byte_array)  # Outputs: b'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xbc\xd0\xb5\xd1\x80'

If you want to get a list of numbers (bytes), you can use:

byte_list = list(byte_array)
print(byte_list)  # For example: [208, 191, 209, 128, 208, 184, 208, 188, 208, 181, 209, 128]