# Weird Decoding Behaviour of bytes in python

**URL:** <https://forum.codecrafters.io/t/weird-decoding-behaviour-of-bytes-in-python/2770>\
**Category:** Challenges\
**Tags:** challenge:redis\
**Created:** [November 4, 2024, 1:49am UTC](https://forum.codecrafters.io/t/weird-decoding-behaviour-of-bytes-in-python/2770 "2024-11-04T01:49:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shreyasganesh0](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/shreyasganesh0/32/2559_2.png) [@shreyasganesh0](https://forum.codecrafters.io/u/shreyasganesh0)\
**Post date:** [November 4, 2024, 1:49am UTC](https://forum.codecrafters.io/t/weird-decoding-behaviour-of-bytes-in-python/2770/1 "2024-11-04T01:49:01Z")

</div>

Anyone who knows python codec. Im seeing some weird behaviour and would love to know the reason

Here are my logs:

```auto
your_program] File is empty 'utf-8' codec can't decode byte 0xfa in position 9: invalid start byte

```

And here’s a snippet of my code:

```python
self.file_byte_data_as_string = str(self.file_byte_data, 'utf-8')

```

It doesnt let me decode to utf-8 when I try to convert the file byte array directly but if i do this

```auto
header = str(self.file_byte_data[:5],'utf-8')

```

it works perfectly fine and i get a string.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 4, 2024, 2:30am UTC](https://forum.codecrafters.io/t/weird-decoding-behaviour-of-bytes-in-python/2770/2 "2024-11-04T02:30:05Z")

</div>

@shreyasganesh0 The first 9 bytes in an RDB file are valid ASCII/Unicode characters (`REDIS0011` in this case).

However, the 10th byte at position 9 is `0xfa`, which is not a valid Unicode character. Attempting to decode it as UTF-8 would result in the error you saw:

> ‘utf-8’ codec can’t decode byte 0xfa in position 9: invalid start byte

---

<div class="post-metadata">

**Author:** ![shreyasganesh0](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/shreyasganesh0/32/2559_2.png) [@shreyasganesh0](https://forum.codecrafters.io/u/shreyasganesh0)\
**Post date:** [November 4, 2024, 2:31am UTC](https://forum.codecrafters.io/t/weird-decoding-behaviour-of-bytes-in-python/2770/3 "2024-11-04T02:31:39Z")

</div>

Oh that makes so much sense. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [November 9, 2024, 2:31am UTC](https://forum.codecrafters.io/t/weird-decoding-behaviour-of-bytes-in-python/2770/4 "2024-11-09T02:31:51Z")

</div>

Closed on November 9, 2024, 2:31am UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
