# Malformed UTF-8 (category Ma: Malformed, bad data)

**URL:** https://discourse.julialang.org/t/malformed-utf-8-category-ma-malformed-bad-data/82421
**Category:** New to Julia
**Created:** [June 8, 2022, 9:33am UTC](https://discourse.julialang.org/t/malformed-utf-8-category-ma-malformed-bad-data/82421 "2022-06-08T09:33:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Daniel\_Lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daniel_lewis/32/31250_2.png) [@Daniel\_Lewis](https://discourse.julialang.org/u/Daniel_Lewis)
#### Post date: [June 8, 2022, 9:33am UTC](https://discourse.julialang.org/t/malformed-utf-8-category-ma-malformed-bad-data/82421/1 "2022-06-08T09:33:53Z")

</div>

Hi, I have an issue with `Malformed UTF-8 (category Ma: Malformed, bad data)` when reading input data from a serial port, instead of receiving an Hex data like “0xec” I get “\xec”.  
I don’t fully understand way I get the data wrong, but I need to replace the “\x” with “0x” but failed to do so.  
Can someone help me?  
Thanks in ahead

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [June 8, 2022, 9:52am UTC](https://discourse.julialang.org/t/malformed-utf-8-category-ma-malformed-bad-data/82421/2 "2022-06-08T09:52:10Z")

</div>

My guess, without seeing your code, is you should be using `read(io, UInt8)` and not `read(io, String)`

---

<div class="post-metadata">

### Author: ![Daniel\_Lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daniel_lewis/32/31250_2.png) [@Daniel\_Lewis](https://discourse.julialang.org/u/Daniel_Lewis)
#### Post date: [June 8, 2022, 9:56am UTC](https://discourse.julialang.org/t/malformed-utf-8-category-ma-malformed-bad-data/82421/3 "2022-06-08T09:56:15Z")

</div>

hi, thanks for the quick reply  
this is the code I use:

```julia
using LibSerialPort
sp = LibSerialPort.open(ports[1], 115200, ndatabits=8, nstopbits=1)

datain = []
append!(datain, readline(sp))

```

---

<div class="post-metadata">

### Author: ![woclass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/woclass/32/212699_2.png) [@woclass](https://discourse.julialang.org/u/woclass)
#### Post date: [June 8, 2022, 10:05am UTC](https://discourse.julialang.org/t/malformed-utf-8-category-ma-malformed-bad-data/82421/4 "2022-06-08T10:05:18Z")

</div>

try

```diff
-append!(datain, readline(sp))
+append!(datain, read(sp, UInt8))

```

---

<div class="post-metadata">

### Author: ![Daniel\_Lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daniel_lewis/32/31250_2.png) [@Daniel\_Lewis](https://discourse.julialang.org/u/Daniel_Lewis)
#### Post date: [June 8, 2022, 10:09am UTC](https://discourse.julialang.org/t/malformed-utf-8-category-ma-malformed-bad-data/82421/5 "2022-06-08T10:09:20Z")

</div>

Thank you all for the help.  
my code look like this now:

```julia
using LibSerialPort

ports = get_port_list()
sp = LibSerialPort.open(ports[1], 115200, ndatabits=8, nstopbits=1)

datain = []
while bytesavailable(sp) > 0
  push!(datain, read(sp,UInt8))
end
close(sp)

```

and now it’s working fine

---

<div class="post-metadata">

### Author: ![woclass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/woclass/32/212699_2.png) [@woclass](https://discourse.julialang.org/u/woclass)
#### Post date: [June 8, 2022, 10:13am UTC](https://discourse.julialang.org/t/malformed-utf-8-category-ma-malformed-bad-data/82421/6 "2022-06-08T10:13:14Z")

</div>

The issue is that `readline` treats the read data as a UTF-8 string.  
Whereas `read` can specify the parsed form (type) of the read data.

```julia
help?> readline
search: readline readlines readlink

  readline(io::IO=stdin; keep::Bool=false)
  readline(filename::AbstractString; keep::Bool=false)

  Read a single line of text from the given I/O stream or file (defaults to stdin). When reading from a file, the text
  is assumed to be encoded in UTF-8.

```

```julia
help?> read
search: read read! readdir readlink readline readeach readuntil readlines readchomp readbytes! readavailable

  read(io::IO, T)

  Read a single value of type T from io, in canonical binary representation.

```
