On Wednesday, January 22, 2020 at 1:35:16 PM UTC-8, Simon King wrote:
>
> Here is an example: 
> Unpickling the data initially results in a string, s: 
>   sage: s = '\x80\x1f' 
> I want it to be interpreted as the following bytes, b: 
>   sage: b = b'\x80\x1f' 
>
> How can I efficiently transform s into b? The following works, but I 
>

This is exactly what we encountered on #28444, where we found that "latin1" 
decoding has a left-inverse (if decoding acts on the left) of "latin1" 
encoding. It looks like the encoding Py3 used for your py2-string was 
"latin1" (that's what we decided was the smart thing to do most of the time 
for possibly binary data), so

s.encode(encoding="latin1")

should do the trick. That should be pretty efficient.

 

> doubt that it is very efficient: 
>   sage: import struct 
>   sage: struct.pack('{}B'.format(len(s)),*(ord(_) for _ in s)) == b 
>   True 
>
> Note that sage.cpython.string.str_to_bytes does't do what I need: 
>   sage: sage.cpython.string.str_to_bytes(s) 
>   b'\xc2\x80\x1f' 
>
> Best regards, 
> Simon 
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/4dfdf5fa-6bb9-4edf-8613-a90056d00ada%40googlegroups.com.

Reply via email to