btowc - Linux


Overview

btowc is a utility program that converts a single-byte character from the current locale to a wide-character code. This is useful for processing text files or data streams that contain non-ASCII characters, as it allows for correct handling of multi-byte character encodings.

Syntax

btowc [-w WIDTH] CHAR

Options/Flags

| Option | Description | Default |
|—|—|—|
| -w WIDTH | Specifies the width of the output wide-character code. The valid values are 16, 32, or 64, representing the number of bits in the code. | 16 |

Examples

Convert an ASCII character to its wide-character code:

$ echo "a" | btowc
97

Convert a multi-byte UTF-8 character to its wide-character code:

$ echo "あ" | btowc -w 32
12354

Convert a character in a specific locale:

$ LANG=ja_JP.UTF-8 echo "あ" | btowc
12354

Common Issues

  • Invalid input: If the input character is not a valid character in the current locale, btowc will return an error. Ensure that the input is valid and encoded correctly.
  • Incorrect width: If the specified width is not one of the supported values (16, 32, or 64), btowc will return an error.

Integration

btowc can be integrated with other commands for advanced processing, such as:

  • iconv: Convert text files between different character encodings using iconv, with btowc used to convert individual characters as needed.
  • wcwidth: Calculate the display width of a wide-character string using wcwidth, where btowc can be used to convert multi-byte characters into wide-character codes.

Related Commands

  • iconv – Convert character encodings
  • wcwidth – Calculate display width of wide-character strings
  • mbstowcs – Convert a multi-byte string to a wide-character string
  • wctomb – Convert a wide-character code to a multi-byte character