Convert string to bytes python - python-3.x

Hello is there a way to convert string to bytes like this "b'\x..."?
I have tried:
string = "mystring"
print(string.encode('utf-8'))
But I get
b'mystring'
I want it to print like
b'\x...

Related

Convert string literal of bytes object to bytes

How can I convert a string like this: "b'this is something'" back to a bytes object?
Thank you.

How to convert a variable to a raw string?

If I have a string, "foo; \n", I can turn this into a raw string with r"foo; \n". If I have a variable x = "foo; \n", how do I convert x into a raw string? I tried y = rf"{x}" but this did not work.
Motivation:
I have a python string variable, res. I compute res as
big_string = """foo; ${bar}"""
from string import Template
t = Template(big_string)
res = t.substitute(bar="baz")
As such, res is a variable. I'd like to convert this variable into a raw string. The reason is I am going to POST it as JSON, but I am getting json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 620 (char 619). Previously, when testing I fixed this by converting my string to a raw string with: x = r"""foo; baz""" (keeping in line with the example above). Now I am not dealing with a big raw string. I am dealing with a variable that is a JSON representation of a string where I have replaced a single variable, bar above, with a list for a query, and now I want to convert this string into a raw string (e.g. r"foo; baz", yes I realize this is not valid JSON).
Update: As per this question I need a raw string. The question and answer flagged in the comments as duplicate do not work (res.encode('unicode_escape')).

How i could decode byte string to store in string variable without null bytes and human readable?

I read some lines from a textfile which was in a zip file and have to modified to a readable string.
for example one line i get from file show like this:
byte_code = b"\x000\x002\x002\x008\x007\x00:\x00,\x00'\x001\x004\x00.\x001\x002\x00.\x002\x000\x001\x009\x00 \x002\x000\x00:\x002\x008\x00:\x002\x007\x00'\x00,\x00$\x000\x001\x00F\x00B\x00,\x00,\x00,\x00,\x00\r\x00\n"
if i decode and print it, i get a readabel result (sory the output have some null byte in it and i could not enter her
print(byte_code.decode('latin-1'))
i would to get the readable result like the print function got into a normal string variable without null bytes in it
this line i expect
02287:,'14.12.2019 20:28:27',$01FB,,,,
but if i assign the decode line to a string variable i get this one not readable string
mystr = byte_code.decode('latin-1')
mystr
Out[55]: "\x000\x002\x002\x008\x007\x00:\x00,\x00'\x001\x004\x00.\x001\x002\x00.\x002\x000\x001\x009\x00 \x002\x000\x00:\x002\x008\x00:\x002\x007\x00'\x00,\x00$\x000\x001\x00F\x00B\x00,\x00,\x00,\x00,\x00\r\x00\n"
Is the decoding of byte string with the correct encoding format?
How i get the correct readable string without null bytes?
This might not be the perfect answer, but it might be a quick solution until something better comes up.
byteLine = b"\xff\xfe0" + byte_code + b"\x00"
strLine = byteLine.decode('utf16')
The strLine value is then:
In [1] : strLine
Out[2] : "002287:,'14.12.2019 20:28:27',$01FB,,,,\r\n"

How to convert <class bytes> to array or string in python?

subprocess.popen is returning the output as class bytes as below
b'Caption FreeSpace Size \r\r\nC: 807194624
63869808640 \r\r\nD: \r\r\nY:
216847310848 2748779065344 \r\r\n\r\r\n'
How to remove all occurance of \r\r\n Or how can I convert this to python string or array
my_str = "hello world"
Converting string to bytes
my_str_as_bytes = str.encode(my_str)
Converting bytes to string
my_decoded_str = my_str_as_bytes.decode()
You can print it by:
print(b'Caption FreeSpace ... \r\r\n\r\r\n'.decode())
Result:
Caption FreeSpace ...
You can remove the characters with translate like so
b'Caption FreeSpace ... \r\r\n\r\r\n'.translate(None, b'\r\n')
which results in
b'Caption FreeSpace Size C: 807194624 63869808640 D: Y: 216847310848 2748779065344 '
If you know the encoding of the returned data you may want to use decode which will give you a string for further processing.
For example, assumed it is encoded in utf-8, you can just call decode with its default value and directly call split on it to split by white-space characters to get an array like this
b'Caption FreeSpace ... \r\r\n\r\r\n'.translate(None, b'\r\n').decode().split()
Result
['Caption', 'FreeSpace', 'Size', 'C:', '807194624', '63869808640', 'D:', 'Y:', '216847310848', '2748779065344']

How to get the ASCII value of a string

Suppose there is a string:
String str="Hello";
HOw can i get the ASCII value of that above mentioned string?
Given your comment, it sounds like all you need is:
char[] chars = str.ToCharArray();
Array.Sort(chars);
A char value in .NET is actually a UTF-16 code unit, but for all ASCII characters, the UTF-16 code unit value is the same as the ASCII value anyway.
You can create a new string from the array like this:
string sortedText = new string(chars);
Console.WriteLine(chars);
As it happens, "Hello" is already in ascending ASCII order...
byte[] asciiBytes =Encoding.ASCII.GetBytes(str);
You now have an array of the ASCII value of the bytes

Resources