I have a ASCII string = "abcdefghijk". I want to write this to a binary file in binary format using python.
I tried following:
str = "abcdefghijk"
fp = file("test.bin", "wb")
hexStr = "".join( (("\\x%s") % (x.encode("hex"))) for x in str)
fp.write(hexStr)
fp.close()
However, when I open the test.bin I see the following in ascii format instead of binary.
\x61\x62\x63\x64\x65\x66\x67
I understand it because for two slashes here ("\\x%s"). How could I resolve this issue? Thanks in advance.
Update :
Following gives me the expected result:
file = open("test.bin", "wb")
file.write("\x61\x62\x63\x64\x65\x66\x67")
file.close()
But how do I achieve this with "abcdef" ASCII string. ?