0

so i have variable contains in bytes and i want to save it to str. but how to do it? i tried various kinds of ways, but always got error

'utf-8' codec can't decode byte 0xa0 in position 0: invalid start byte

i want to save the result encrypt file to text file. so i have the proof it the file was encrypt. here;s my ways 1:

 def create_file(f):
     response = HttpResponse(content_type="text/plain")
     response['Content-Disposition'] = 'attachment; filename=file.txt'

     filename = f
     print(filename)
     name_byte = codecs.decode(filename)
     print(name_byte)

     return response

my ways 2 :

def create_file(enc):
    with open("file.txt", "w", encoding="utf-8") as f:
        enc = enc.decode('utf-8')
        f.write(enc)

my ways 3:

def create_file(f):
    file = open("file.txt", "w")
    f = f.decode('utf-8')
    download = file.write(f)
    file.close()
    print(download)

    return download

f = b'\xa0\x0e\xdc\x14' f is the return result of encrypt

i called the function :

#in views
download = create_file(enc)
           print(download)

#in urls
path("create_file", views.create_file, name="create_file"),

#in html
<a href="{% url 'create_file' %}">
HelpMe
  • 21
  • 6
  • very common problem: https://stackoverflow.com/questions/606191/convert-bytes-to-a-string – D.L Sep 04 '22 at 15:37
  • not works. if i use "utf-8", i got error message 'utf-8' codec can't decode bytes in position 1-2: invalid continuation byte @D.L – HelpMe Sep 04 '22 at 15:41
  • okay. try for `hex bytes`: https://stackoverflow.com/questions/6624453/whats-the-correct-way-to-convert-bytes-to-a-hex-string-in-python-3 – D.L Sep 04 '22 at 15:43
  • not the result i want, what i want is i can save b'\x08\xb3Q\x90' to txt file. if i use hex then i just get the result of encrypt. @D.L – HelpMe Sep 04 '22 at 15:48
  • have amended the answer according to the last comment (`save line to file`)... – D.L Sep 04 '22 at 15:57

2 Answers2

0

The phrase "have variable contains in bytes and i want to save it to str" makes no sense at all. bytes and str are different data types serving different purposes. bytes holds a byte stream while str hold abstract text. Yes, bytes can also hold textual info encoded in some text encoding like UTF-8, but that is only a specific case. If, for example, your bytes hold a JPEG image then converting it to str does not make any sense simply because it's not text. So if your encrypted file contains a byte stream you need to treat it as such. It's not text any more until you decrypt it.

So the only thing you can do is to save your byte stream as is using the binary file mode:

v = b'\xa0\x0e\xdc\x14'
with open ('/path', 'wb') as fo:
    fo.write(v)

The same applies to sending the encrypted file as Django response:

response = HttpResponse(v, content_type="application/octet-stream")
response['Content-Disposition'] = 'attachment; filename=file.bin'
Roman Miroshnychenko
  • 1,496
  • 1
  • 10
  • 16
-1

If you just want to write the entire line to file then all you have to do is convert to string.

So this would work:

v = b'\xa0\x0e\xdc\x14'

with open("C:/test/output.txt", 'w') as file:
    file.write(str(v))

The result is a text file like this:

enter image description here

D.L
  • 4,339
  • 5
  • 22
  • 45
  • i got error : The view MusicLockApp.views.create_file didn't return an HttpResponse object. It returned None instead. how to return to html?? – HelpMe Sep 04 '22 at 16:01
  • how did you implement this into your code (maybe a new question) as the above works (using windows file path). – D.L Sep 04 '22 at 16:02
  • please check again my update – HelpMe Sep 04 '22 at 16:07
  • the original question was how to write the text, so are you now asking how to create the file in a new path ? – D.L Sep 04 '22 at 16:19
  • im asking how to create file contains with b'\xa0\x0e\xdc\x14' – HelpMe Sep 04 '22 at 16:25
  • you can use the same code. If the file does not exist then then it will be created (so long as the folder exists). If you are asking how to create a `new folder`, then this might be best as a new question. – D.L Sep 04 '22 at 16:27
  • how to called that func to html? – HelpMe Sep 04 '22 at 16:30
  • https://stackoverflow.com/questions/73601131/how-to-return-result-to-html-django here's new question. if it's work, then i will check this question – HelpMe Sep 04 '22 at 16:43