Convert input.txt from UTF-8 to ISO-8859-1 and write output.txt.
All input characters are representable in the target encoding.
Which command is appropriate?
iconv uses -f for the input encoding and -t for the output encoding.
Detailed explanation
iconv -f UTF-8 -t ISO-8859-1 input.txt > output.txtCorrect. It converts from UTF-8 to ISO-8859-1.
Correct. It converts from UTF-8 to ISO-8859-1.
iconv -f ISO-8859-1 -t UTF-8 input.txt > output.txtIncorrect. It reverses the source and target encodings.
Incorrect. It reverses the source and target encodings.
iconv -t UTF-8 -f UTF-8 input.txt > output.txtIncorrect. Both sides are UTF-8, so it does not convert to ISO-8859-1.
Incorrect. Both sides are UTF-8, so it does not convert to ISO-8859-1.
locale -f UTF-8 -t ISO-8859-1 input.txtIncorrect. locale does not convert file contents.
Incorrect. locale does not convert file contents.
Try it yourself
An example you can run in a temporary verification environment.
printf 'Cafe
' | iconv -f UTF-8 -t ISO-8859-1 | od -An -tx1Expected result
43 61 66 65 0aKey points
- -f means from
- -t means to
- Unrepresentable characters can fail
Notes
- Environment: GNU libiconvまたはglibc iconv / ASCII範囲の標準入力のみ
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.