4

I'm trying to read an array of bytes character by character and decode it to a unicode string, as below:

current_character = byte_array[0:1].decode("utf-8")

For each character, I'm trying to check whether the result of .decode("utf-8") equals the empty string, but I can't seem to be able to detect this. When I print out the result of decoding, I get the empty string. But how do I translate this detection into code?

I've tried:

if not current_character

if current_character is u""

But they both don't work. Any suggestions?

3
  • How about current_character = byte_array.decode("utf-8")[0:1]? Commented Nov 10, 2014 at 5:35
  • What do you mean by they don't work? Also, can you add a sample value for byte_array? Commented Nov 10, 2014 at 5:57
  • unrelated: utf-8 is a variable-length encoding: it is incorrect to decode a single byte at a time if arbitrary utf-8 encoded input is allowed. Commented Apr 29, 2016 at 15:31

4 Answers 4

5

Binary strings seem to be written like b'some data', so try this:

if current_character == b'':
    # Code
Sign up to request clarification or add additional context in comments.

Comments

1

if not current_character: is the correct way to find out whether current_character is an empty string in Python. It is equivalent to if len(current_character) == 0:

>>> not ''
True
>>> not b''
True

If it doesn't work for you then something else is broken in your code.

Comments

0

try this:

if current_character == '':
    print('empty string')

Comments

0

No need to decode one byte, just check if it equals 0

current_character = byte_array[x:x+1]
if current_character == 0x00:
    pass #...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.