Convert CSV file from UTF-8 to Shift_JIS

リン (linh)
Goalist Blog
Published in
2 min readApr 30, 2024

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 response character set to Shift_JIS.
If there are some weird characters appear, 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 table incase you need it.

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

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.

--

--

リン (linh)
Goalist Blog

A career-changed-non-tech-background point of view.