Question

I use the bitstring module. What is the easiest way to convert a bitstring to an utf-8 string?

Was it helpful?

Solution

You can use the .bytes property on a BitVector to convert the sequence of bits in the vector into the same sequence of bits in the form of a Python (2.7) str object.

Because strs are encoding-agnostic, you could consider yourself done at this point, provided that the sequence of bits that you stored in your vector correspond to a UTF-8--encoded Unicode string. (In other words, using this approach it's up to you to store bits in your vector that correspond to some UTF-8--encoded sequence of Unicode characters.)

OTHER TIPS

As mentioned in bitstring module documentation:

# To covert to an ordinary string use the bytes property
open('video.m2v', 'wb').write(s.bytes)

Information available here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top