> 技术文档 > C++源代码批量转码utf8

C++源代码批量转码utf8


C++源代码批量转码utf8

脚本

废话不说,直接上脚本呢

#!/bin/bashset -e# set -xscriptPath=$(cd $(dirname $0) && pwd)cd ${scriptPath}# 脚本函数:尝试将文件从GB2312、GB18030、GBK转换为UTF-8iconv_try_convert() { local source_file=\"$1\" local base_name=$(basename -- \"$source_file\") local dir_name=$(dirname -- \"$source_file\") for src_encoding in GB2312 GB18030 GBK; do temp_file=\"${dir_name}/.${base_name}.iconv_${src_encoding}_to_utf8\" # iconv -f \"$src_encoding\" -t UTF-8 \"$source_file\" -o \"$temp_file\" 2>/dev/null # if [ $? -eq 0 ]; then if iconv -f \"$src_encoding\" -t UTF-8 \"$source_file\" -o \"$temp_file\" 2>/dev/null; then mv -f \"$temp_file\" \"$source_file\" echo \"Converted $source_file from $src_encoding to UTF-8 successfully.\" return 0 else rm -f \"$temp_file\" fi done echo \"Failed to convert $source_file to UTF-8 from any of GB2312, GB18030, GBK.\" return 0}find . -type f -name \"*.h\" | while read -r file; do iconv_try_convert ${file}donefind . -type f -name \"*.cpp\" | while read -r file; do iconv_try_convert ${file}done