<br><div class="gmail_quote">On 10 October 2010 23:01, Hidura <span dir="ltr"><<a href="mailto:hidura@gmail.com">hidura@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
I try to encode a binary file what was upload to a server and is<br>
extract from the wsgi.input of the environ and comes as an unicode<br>
string.<br></blockquote><div><br>Firstly, UTF-8 is not meant to encode arbitrary binary data. But I guess you could have a Unicode string in which the character index represents a byte number. (But it's ugly!)<br><br>
So if you can, you could make sure to send the file as just bytes, or if it must be a string, base64 encoded. If this is not possible you can try the code below to obtain the bytes, not a very fast solution, but it should work (Python 3):<br>
<br><br>MAP = {}<br>for i in range(256):<br>    MAP[tmp] = eval("'\\u%04i'" % i)<br><br># Let's say 'a' is your string<br>b''.join([MAP[c] for c in a])<br><br><br>Cheers,<br>  Almar<br>
</div></div>