---
# System prepended metadata

title: Convert CSV file from UTF-8 to Shift_JIS
tags: [self-taught, web-development, published, blog]

---

# Convert CSV file from UTF-8 to Shift_JIS

### I. Why need to do this?
Shift JIS (SJIS) and UTF-8 are both character encoding systems used to represent text in computers. Shift JIS is primarily used for Japanese characters, while UTF-8 is a more universal encoding that covers a wide range of characters from various languages.

When you open a Shift JIS encoded file on a non-Japanese device, the characters may appear garbled or incorrectly displayed. This is because the non-Japanese device does not understand the Shift JIS encoding.

So, that's why I'm sharing this, but i supposed we could do this with other encoding types.

### II. Implementation

On backend, just make sure to set your character set to `Shift_JIS`.
If there are some weird character appears, we might need to tweek the string a bit, since in `Shift_JIS`, each character is encoded using 2 bytes (16 bits), while in `UTF-8`, it takes 1-4 bytes depending on the characters.

Here's the [ASCII](https://lwp.interglacial.com/appf_01.htm) table incase you need it.

On frontend, add media type when you create the download link.

```javascript!
const downloadCSV = () => {
    const apiUrl = "/download"
    const res = await api.get(apiUrl, {
        responseType: 'blob',
    });
    const a = document.createElement('a');
    const url = window.URL.createObjectURL(new Blob([res.data]));
    a.href = url;
    a.target = '_blank';
    a.type = 'text/csv;charset=Shift_JIS';
    a.download = "fileName";
    document.body.appendChild(a);
    a.click();
}
```

That's it! It's not much but i hope it helpful somehow. Thank you for reading. :woman-bowing: 

<small>
    Published date: 2024-04-30 <br/>
    Also published <a href="medium.com/goalist-blog/convert-csv-file-from-utf-8-to-shift-jis-d8b99535721a">here</a>.
</small>