A lightweight iconv library specialized for Shift_JIS ↔ UTF-8 character encoding conversion.
iconv_mini is a minimal, efficient implementation of character encoding conversion specifically designed for converting between Shift_JIS (Japanese character encoding) and UTF-8. This library provides a compact alternative to the standard iconv library when only Shift_JIS ↔ UTF-8 conversion is needed.
- Lightweight: Minimal memory footprint and binary size
- Specialized: Optimized for Shift_JIS ↔ UTF-8 conversion only
- Fast: Direct lookup table implementation for efficient conversion
- Configurable: Compile-time options to include only needed conversion direction
- Standards compliant: Compatible with standard Shift_JIS and UTF-8 encodings
The library provides two main conversion functions:
int iconv_s2u(char **src_buf, size_t *src_len,
char **dst_buf, size_t *dst_len);Converts Shift_JIS encoded text to UTF-8.
Parameters:
src_buf: Pointer to source buffer pointer (Shift_JIS input)src_len: Pointer to source buffer lengthdst_buf: Pointer to destination buffer pointer (UTF-8 output)dst_len: Pointer to destination buffer length
Returns:
0: Success-1: Error (invalid input, insufficient buffer space, etc.)
int iconv_u2s(char **src_buf, size_t *src_len,
char **dst_buf, size_t *dst_len);Converts UTF-8 encoded text to Shift_JIS.
Parameters:
src_buf: Pointer to source buffer pointer (UTF-8 input)src_len: Pointer to source buffer lengthdst_buf: Pointer to destination buffer pointer (Shift_JIS output)dst_len: Pointer to destination buffer length
Returns:
0: Success-1: Error (invalid input, insufficient buffer space, etc.)
#include "iconv_mini.h"
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "こんにちは"; // UTF-8 input
char dst[256];
char *src_ptr = src;
char *dst_ptr = dst;
size_t src_len = strlen(src);
size_t dst_len = sizeof(dst);
// Convert UTF-8 to Shift_JIS
if (iconv_u2s(&src_ptr, &src_len, &dst_ptr, &dst_len) == 0) {
printf("Conversion successful\n");
} else {
printf("Conversion failed\n");
}
return 0;
}The library supports compile-time configuration to reduce size when only one conversion direction is needed:
ICONV_ONLY_U2S: Include only UTF-8 to Shift_JIS conversionICONV_ONLY_S2U: Include only Shift_JIS to UTF-8 conversion
Example:
gcc -DICONV_ONLY_U2S -c iconv_mini.c- C compiler (GCC, Clang, etc.)
- Standard C library
# Compile the library
gcc -c iconv_mini.c -o iconv_mini.o
# Link with your application
gcc your_app.c iconv_mini.o -o your_appThe library includes tools to generate the conversion tables:
# Build the C-based table generator
make createtable
# Or use the Python version
python3 createtable.py > iconv_table.hiconv_mini.h: Main header file with function declarationsiconv_mini.c: Core conversion implementationiconv_table.h: Pre-generated conversion lookup tablescreatetable.c: C program to generate conversion tablescreatetable.py: Python script to generate conversion tablesMakefile: Build configuration
This library supports the standard Shift_JIS character set including:
- ASCII characters (0x00-0x7F)
- Half-width Katakana (0xA1-0xDF)
- JIS X 0208 characters (Kanji, Hiragana, Katakana, symbols)
- Does not support JIS X 0213 plane 2 characters
Copyright (c) 2023,2025 Yuichi Nakamura (@yunkya2)
This project is licensed under the MIT License - see the source files for details.
This library is particularly useful for:
- Embedded systems with limited memory
- Legacy system integration
- File format converters
- Network protocols handling Japanese text
- Any application requiring efficient Shift_JIS ↔ UTF-8 conversion
- Only supports Shift_JIS ↔ UTF-8 conversion (no other encodings)
- No support for JIS X 0213 plane 2 characters
- No locale-specific handling
- Error handling is basic (returns -1 on any error)