关于source insight上中文乱码的问题

时间:2021-12-06 20:14:08

         使用source insight查看源码非常方便,这一点毋庸置疑。但是在中文的输入和显示上又总是让人很烦。个人经验是在ubunutu下面,source insight不支持中文输入,然后源文件中的中文注释在source insight中打开的时候经常显示乱码。这里,针对source insight的中文显示问题的解决方案做个总结。

         在此之前,必须明确一点,source insight不支持utf-8编码,默认编码格式为ansii码。所以,解决问题的思路就是将非ansii码编码格式的文件转换成source insight默认支持的ansii格式。

一、对于单文件

1.使用记事本打开非ANSI格式的源代码文件,另存为,在保存选项对话框中,在编码格式一栏中发现是UTF-8,选择ANSI一项,保存,再用Source Insight打开就可以正常显示中文注释了。

2.将文件用ultraEdit32打开,选择 文件->转换->UTF-8到ANSI, 然后保存。重新用source insight打开就好

了。

3.借助Linux下的iconv命令

iconv -f utf-8 -t  gb18030 file1.txt -o file2.txt

4.借助java的native2ascii工具


二、批量文件转换

1.windows下可以通过写bat脚本,应用native2ascii来批量处理(前提是系统中配好JDK)

参考:http://blog.sina.com.cn/s/blog_4e7453df0101ijch.html


2.linux下可以直接写脚本,应用iconv命令来批量处理

下面附上脚本

完整版:

#!/bin/sh 
#
convertCodeFilePath=$1
fromCode=$2
toCode=$3

for i in {1..1}
do
[ -f $convertCodeFilePath ]
if [ $? -eq 0 ]
then
iconv -f $fromCode -t $toCode -c -o $convertCodeFilePath $convertCodeFilePath
if [ $? -ne 0 ]
then
echo $convertCodeFilePath "=>" convert code failed.
else
echo $convertCodeFilePath "=>" convert code success.
fi
break;
fi

[ -d $convertCodeFilePath ]
if [ $? -ne 0 ]
then
break;
fi

dir=`ls $convertCodeFilePath | sort -d`

for fileName in $dir
do
fileFullPatch=$convertCodeFilePath/$fileName

fileType=`echo $fileName |awk -F. '{print $2}'`

[ -d $fileName ]
if [ $? -eq 0 ]
then
continue
fi

if [ $fileType != 'sh' ] && [ $fileType != 'py' ] && [ $fileType != 'xml' ] && [ $fileType != 'properties' ] \
&& [ $fileType != 'q' ] && [ $fileType != 'hql' ] && [ $fileType != 'txt' ]
then
continue
fi

iconv -f $fromCode -t $toCode -c -o $fileFullPatch $fileFullPatch
if [ $? -ne 0 ]
then
echo $fileName "=>" convert code failed.
continue
else
echo $fileName "=>" convert code success.
fi
done
done
使用方法:

convertCode.sh/home/sam/data utf-8  gb18030


精简版:

#!/bin/bash

if [ "$#" != "2" ]; then

echo "Usage: `basename $0` dir filter"

exit

fi

dir=$1

filter=$2

echo $1

for file in `find $dir -name "$2"`; do

echo "$file"

iconv -f gb18030 -t utf8 -o $file $file

done
使用:

cd /home/sam/data   
~ /iconv_shell .sh  ./  *hpp
~/iconv_shell.sh  ./  *cpp