Mac OS 使用iconv 将gb18030编码文本转换为utf-8格式文本

本文介绍了如何在Mac OS X系统中利用iconv工具将gb18030编码的中文文本转换为utf-8格式。通过在Terminal中运行特定命令,可以实现文本编码的转换。此外,还提到了一个用shell脚本进行递归转换目录下所有文件到utf-8的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Mac OS X comes with iconv utility that can convert text between encodings. Run the following command in Terminal to convert a gb2312 chinese text file to utf-8:
    iconv -f cp936 -t utf-8 chinese-gb2312.txt > chinese-utf8.txt
  • To list the encodings that iconv supports:
    iconv -l

转载自:http://notepad2.blogspot.com/2012/07/mac-os-x-convert-gb2312gbkgb18030.html

以下是自己写的使用shell递归转换一个文件目录下的文件格式到utf-8格式的脚本


#!/bin/bash  
   
function encode() 
{
	iconv -f cp936 -t utf-8 "$1" > test
	# iconv -f iso8859-15 -t utf8 "$1" > test;
	cat test > "$1";
}

function walk()  
{  
  for file in `ls $1`  
  do  
    local path=$1"/"$file  
    if [ -d $path ]  
     then  
      echo "DIR $path"  
      walk $path  
    else  
      echo "FILE $path"  
      encode $path
    fi  	
  done  
}  
   
if [ $# -ne 1 ]  
then  
  echo "USAGE: $0 TOP_DIR"  
else  
  walk $1  
fi  







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值