How Convert Files Between Ascii and Utf-8
I have a java code and I want to convert it into Utf-8. How can I do it in console. By the way I have multiple files so it need to be do multiple conversion.
Getting Info About Character Set
We start by getting information about character set. We need to be sure files character set to convert accordingly.
$ file -i main.java
main.java: text/plain; charset=us-ascii
- file is very useful tool to get information about files like file type, encoding etc.
- -i is used to get encoding information about file main.java
List Supported Encodings of iconv
We will use iconv to conversation. But we need to know which encodings are supported by iconv .
$ iconv -l
Whooa there is a lot of options to use but we think that ASCII and UTF-8 is enough for now.
Convert ASCII to UTF-8
We will convert our java code by providing from and to encodings.
root@ubu1:~# iconv -f us-ascii -t UTF8 main.java -o main-out.java
- iconv is the tool to convert
- -f us-ascii is the source file encoding type
- -t UTF8 is target encoding type
- main.java is our source file to be converted
- main-out.java is new file encoded with UTF8
Convert Multiple Files Encoding
We can make things automated with simple bash scripting help.
root@ubu1:~# for files in *.java ; do iconv -t UTF8 $files -o $files; done
- *.java is source files to be converted with java extension
- UTF8 is the destination encoding to be used.