- 浏览: 1520972 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (532)
- 软件设计师 (7)
- PSP (5)
- NET MD (9)
- Hibernate (8)
- DIY (51)
- Work (43)
- GAME (24)
- 未分类 (44)
- iPod (6)
- MySQL (39)
- JSP (7)
- 日语能力考试 (36)
- 小说 (4)
- 豆包网 (23)
- 家用电脑 (7)
- DB2 (36)
- C/C++ (18)
- baby (9)
- Linux (13)
- thinkpad (23)
- OA (1)
- UML (6)
- oracle (24)
- 系统集成 (27)
- 脑梗塞 (6)
- 车 (8)
- MainFrame (8)
- Windows 7 (13)
- 手机 (8)
- git (12)
- AHK (2)
- COBOL (2)
- Java (9)
最新评论
-
安静听歌:
... ...
UUID做主键,好还是不好?这是个问题。 -
lehehe:
http://www.haoservice.com/docs/ ...
天气预报 -
lehehe:
[url http://www.haoservice.com/ ...
天气预报 -
liubang201010:
监控TUXEDO 的软件推荐用这个,专业,权威.并能提供报警和 ...
(转载)Tuxedo中间件简介 -
tinkame:
Next[j] =-1 当j=0时;=Max{k|0<k ...
KMP字符串模式匹配详解
glibc带了一套转码函数iconv,使用方便,可识别的码很多,如果程序需要涉及到编码之间的转换,可考虑用它。 iconv编程涉及到以下glibc库的调用: #include <iconv.h>
iconv_t iconv_open(const char *tocode, const char *fromcode);
int iconv_close(iconv_t cd);
size_t iconv(iconv_t cd,
char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft);
在使用iconv转码的时候,首先用iconv_open获取转码句柄,然后调用iconv转码,转完了后调用iconv_close关闭句柄。
iconv函数中: 参数cd是用iconv_open调用返回的转码句柄; 如果调用成功,iconv返回转换的字节数(不可逆转调用的字节数,可逆转调用的字节数不包括在内)。否则返回-1,并设置相应的errno。 1. 多字节序列无效,这时候errno为EILSEQ,*inbuf指向第一个无效的字符; 对于iconv函数,还有两种调用情况: 1. inbuf或者*inbuf为NULL,outbuf和*outbuf不为NULL,iconv会设置转换状态为初始状态,并保存转换序列到*outbuf。如果outbuf空间不足,errno会设置为E2BIG,返回(size_t)(-1); iconv命令的使用固然方便,可是如果转换过程中如果遇到问题则会停止转换,有时候我们希望跳过不能转换的字节序列继续转换。以下的一段程序能实现这种功能。 /** #ifdef DEBUG #define CONVBUF_SIZE 32767 extern int errno; void print_err(const char *fmt, ...) int print_out(const char* buf, size_t num) void print_usage() { int iconv_string(const char *from, const char *to, int conv_file_fd(const char* from, const char *to, int fd, int conv_file(const char* from, const char* to, int main(int argc, char *argv[]) 可以用内存映像文件解决文件太大内存缓冲不够的情况。相对于iconv命令,加-c选项,以忽略转换过程中可能引发的问题。 如果在命令行加了-DDEBUG选项,会编译进调试语句,如果加了-DTESTCASE选项,则仅会编译对iconv_string函数测试的情况。
iconv命令的用法。
参数inbuf指向需要转码的缓冲区;
参数inbytesleft是inbuf所保存的需要转码的字节数;
参数outbuf存放转码结果;
参数outbytesleft存放outbuf空间的大小。
iconv逐步扫描inbuf,每转换一个字符,就增加inbuf,减少inbytesleft,并将结果存入outbuf,结果字节数存入outbytesleft。遇到下列情况将停止扫描并返回:
2. 有字节留在inbuf尚未转换,errno为EINVAL;
3. outbuf空间不够,errno为E2BIG;
4. 正常转换完备。
2. inbuf或者*inbuf为NULL,outbuf或者*outbuf也为NULL,iconv设置转换状态为初始状态。
* siconv.cpp - A simple way to demostrate the usage of iconv calling
*
* Report bugs to marchday2004@yahoo.com.cn
* July 15th, 2006
*/
#include <iconv.h>
#include <stdio.h>
#include <string>
#include <stdarg.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#define TRACE(fmt, args...) fprintf(stderr, "%s:%s:%d:"fmt, \
__FILE__, __FUNCTION__, __LINE__, ##args)
#else
#define TRACE(fmt, args...)
#endif
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
{
if (num != fwrite(buf, 1, num, stdout)) {
return -1;
}
return 0;
}
print_err("Usage: siconv -f encoding -t encoding [-c] "
"input-file\n");
}
const char *src, size_t len,
::std::string& result,
int c = 0, size_t buf_size = 512)
{
iconv_t cd;
char *pinbuf = const_cast< char* >(src);
size_t inbytesleft = len;
char *poutbuf = NULL;
size_t outbytesleft = buf_size;
char *dst = NULL;
size_t retbytes = 0;
int done = 0;
int errno_save = 0;
if ((iconv_t)-1 == (cd = iconv_open(to, from))) {
return -1;
}
dst = new char[buf_size];
while(inbytesleft > 0 && !done) {
poutbuf = dst;
outbytesleft = buf_size;
TRACE("TARGET - in:%p pin:%p left:%d\n", src, pinbuf, inbytesleft);
retbytes = iconv(cd, &pinbuf, &inbytesleft, &poutbuf, &outbytesleft);
errno_save = errno;
if (dst != poutbuf) {// we have something to write
TRACE("OK - in:%p pin:%p left:%d done:%d buf:%d\n",
src, pinbuf, inbytesleft, pinbuf-src, poutbuf-dst);
result.append(dst, poutbuf-dst);
}
if (retbytes != (size_t)-1) {
poutbuf = dst;
outbytesleft = buf_size;
(void)iconv(cd, NULL, NULL, &poutbuf, &outbytesleft);
if (dst != poutbuf) {// we have something to write
TRACE("OK - in:%p pin:%p left:%d done:%d buf:%d\n",
src, pinbuf, inbytesleft, pinbuf-src, poutbuf-dst);
result.append(dst, poutbuf-dst);
}
errno_save = 0;
break;
}
TRACE("FAIL - in:%p pin:%p left:%d done:%d buf:%d\n",
src, pinbuf, inbytesleft, pinbuf-src, poutbuf-dst);
switch(errno_save) {
case E2BIG:
TRACE("E E2BIG\n");
break;
case EILSEQ:
TRACE("E EILSEQ\n");
if (c) {
errno_save = 0;
inbytesleft = len-(pinbuf-src); // forward one illegal byte
inbytesleft--;
pinbuf++;
break;
}
done = 1;
break;
case EINVAL:
TRACE("E EINVAL\n");
done = 1;
break;
default:
TRACE("E Unknown:[%d]%s\n", errno, strerror(errno));
done = 1;
break;
}
}
delete[] dst;
iconv_close(cd);
errno = errno_save;
return (errno_save) ? -1 : 0;
}
::std::string& result, int c)
{
struct stat st;
void *start;
if (0 != fstat(fd, &st)) {
return -1;
}
start = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (MAP_FAILED == start) {
return -1;
}
if (iconv_string(from, to, (char*)start,
st.st_size, result, c, CONVBUF_SIZE) < 0) {
int errno_save = errno;
munmap(start, st.st_size);
TRACE("\n");
errno = errno_save;
return -1;
}
munmap(start, st.st_size);
return 0;
}
const char* filename, int c)
{
::std::string result;
FILE *fp;
if (NULL == (fp=fopen(filename, "rb"))) {
print_err("open file %s:[%d]%s\n", filename,
errno, strerror(errno));
return -1;
}
if (conv_file_fd(from, to, fileno(fp), result, c) < 0) {
print_err("conv file fd:[%d]%s\n", errno, strerror(errno));
fclose(fp);
return -1;
}
print_out(result.data(), result.size());
fclose(fp);
return 0;
}
{
#ifdef TESTCASE
::std::string strA = "欢迎(welcome ^_^)来到(to)首都北京。";
::std::string strB = "大喊一声:We are chinese <=> 我们都是中国人。";
::std::string strC = strA.substr(0, 20) + strB.substr(0, 41);
::std::string result;
if (iconv_string("GBK", "UTF-8", strC.data(), strC.size(), result, 1) < 0)
{
TRACE("ERROR [%d]%s\n", errno, strerror(errno));
}
TRACE("CONVERSION RESULT:");
result.append("\n");
print_out(result.data(), result.size());
return 0;
#else
::std::string from, to;
::std::string input_file;
int o;
int c = 0;
while (-1 != (c = getopt(argc, argv, "f:t:c")))
{
switch(c) {
case 'f':
from = optarg;
break;
case 't':
to = optarg;
break;
case 'c':
c = 1;
break;
default:
return -1;
}
}
if (from.empty() || to.empty() || optind != (argc-1))
{
print_usage();
return -1;
}
input_file = argv[optind++];
return conv_file(from.c_str(), to.c_str(),
input_file.c_str(), c);
#endif
}
发表评论
-
(转)const参数,const返回值与const函数
2014-11-14 17:29 778原文地址:http://04 ... -
(转)在 Eclipse 中配置编译 Pro*C
2010-08-09 17:49 1995http://blog.csdn.net/gjl2004yn/ ... -
(转)Pro*C 程序概述
2010-06-30 10:24 1094http://blog.csdn.net/amandake ... -
(转)什么是Pro*C/C++
2010-06-30 10:22 2270http://blog.csdn.net/hwz119/a ... -
(转)oracle中pro*c的学习
2010-06-30 10:17 1449http://www.qqread.com/ora ... -
(转)gcc and g++编译器和gdb调试器
2010-02-23 15:09 1824http://d.download.csdn.net/d ... -
(转)GDB 命令详细解释
2010-02-23 15:05 1483http://blog.csdn.net/wei801004/ ... -
CC和gcc是一样的编译器吗?
2009-06-15 17:06 2797楼主anysisze(张)2006-07-20 21:4 ... -
gcc常用参数,备忘
2009-06-12 14:45 1434gcc常用参数,备忘收藏 1. 头文件象conio ... -
请教关于标准C中findfirst()函数的用法!谢谢
2009-06-03 17:45 5006http://www.9php.com/FAQ/cx ... -
用strtok()函数提取分隔符隔开的每个字符串
2009-05-22 14:45 4130http://www.cplusplus.com/refere ... -
printf格式控制符的完整格式
2009-03-25 10:39 3164printf格式控制符的完整格式(2006-2-17 14: ... -
iconv 的使用方法
2009-03-24 17:38 1419iconv 的使用方法 iconv---编码转换用法: ic ... -
(C语言)头文件实现的函数
2009-03-18 17:43 4409(C语言)头文件实现的函数 http://www.diy ... -
C语言库函数大全(字母a)---使用说明(转)
2009-03-18 17:43 1578C语言库函数大全(字母 ... -
Converting Strings To/From Ints:atoi, itoa, sprint
2009-03-18 16:41 1380Converting Strings To/From Int ... -
在 console mode 中使用 C/C++ 编译器
2009-03-05 17:18 1137在 console mode 中使用 C/C++ 编译器 ...
相关推荐
iconv编码转码工具
使用最新的1.15版本代码编译的iconv静态库。使用MinGW 4.9.2编译器,Codeblocks创建工程,带示例,示例为UTF8转SHIFT-JIS编码,静态库经测试可用,目前尚未发现异常。
iconv 自动转码批处理工具 写成bat,方便使用
批处理文件转码包iconv.exe
主要介绍了shell脚本中使用iconv实现批量文件转码的代码分享,本文提供了2个实现代码和各自使用方法,需要的朋友可以参考下
iconv.
iconv是文件编码格式转换工具。 提供的iconv.exe是iconv的Windows版。
我在做一个加密芯片项目...1.如要iconv_open、iconv_close这样的名字,需在iconv.h中加入#define LIBICONV_PLUG即可。 2.如要libiconv_open、libiconv_close这样的名字,需在iconv.h中移除#define LIBICONV_PLUG即可。
mysql命令行直接倒出数据,然后转换为csv文件,编码自动转换,支持windows
windows下可使用的iconv静态库,只需引入iconv.h,iconv.lib即可,使用方法见示例文件。
iconv.h iconv.lib charset.lib 编译时缺少文件时可以用
iconv是知名的开源跨平台编码转换库,iconv.exe是iconv库在windows下的命令行工具,iconv.exe的一般用法:iconv.exe -f gbk -t utf-8 gbk.txt > utf-8.txt。其中 -f gbk 指明转换前的文件编码是gbk,-t utf-8 指明...
iconv_open("utf-8", "gb2312") 调用失败
在Linux下有个iconv.h头文件,里面是对编码转换处理的封装。但在windows下是没有这个头文件的。这里有三个文件,高手编写,用在windows下:iconv.h,iconv.dll和iconv.lib,导出库,就可以用iconv.h了。用法与Linux下...
iconv 可以转换大文件编码转换,不产生乱码。里面有个简单的转码教程,更多功能需要自己挖掘
iconv库32位版和64位版都含有,其中包含编译好的: charset.lib、iconv.lib、iconv.dll、charset.dll、iconv.exe 以及头文件:iconv.h、libcharset.h、localcharset.h 还有说明:iconv.1.html、iconv.3.html 等资源
一个利用iconv函数将GBK码转为UTF-8码的函数,本函数在LINUX系统下测试...目前支持中文繁简、日文、英文、俄文等的转码。函数其实很简单,另外精选了一些本人觉得有助于理解ICONV函数及Unicode、GBK这些概念的资料。
extern size_t iconv (iconv_t cd, @ICONV_CONST@ char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft); /* Frees resources allocated for conversion descriptor ‘cd’. */ #ifndef ...
iconv_ret = iconv(cd,pin,&inlen,pout,&outlen); if (iconv_ret == (size_t)-1) { perror("iconv():"); exit(0); } iconv_close(cd); return 0; } int u2g(char *inbuf,int inlen,char *outbuf,int ...
apr-iconv linux 编译成功 apr-iconv linux 编译成功 apr-iconv linux 编译成功 apr-iconv linux 编译成功 apr-iconv linux 编译成功