Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Iconv usage

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Iconv usage

Scene description

Convert the UTF-8 code to GBK code, use Notepad++ to create a UTF-8 (no BOM) encoding file, the file content is simple in the wind and rain on the way, save the file name is utf8.txt, after conversion, the storage file name is gbk.txt.

Use of the 1.1:iconv command

Iconv-f UTF8-t GBK gbk.txt

The use of 1.2:iconv function

# include

Size_t iconv (iconv_t cd

Char * * inbuf, size_t*inbytesleft

Char * * outbuf, size_t*outbytesleft)

Parameter description: after the iconv function, * outbuf currently points to the last position of the converted string, and * inuf currently points to the last position of the converted string, so in order to get the correct pointer position of the converted string, you need to make the following adjustments:

* outbuf = * return value of the outbuf-iconv function. Or if you define a pointer to the current string cache, the buffer pointer of the string will not be modified.

1.3 the code is as follows

Int UTF8fileToGBKfile ()

{

Int ret

Int read_fd

Int write_fd

/ / 1 Open the file and read the utf-8 file

Read_fd=open ("utf8.txt", O_RDONLY | O_CREAT)

Write_fd=open ("gbk.txt", O_WRONLY | O_CREAT)

Char utfBuffer [256] = {0}

Size_t inLen = read (read_fd, utfBuffer,256)

/ / 2 get the file handle of character set conversion, here is the conversion from utf-8 to gbk

Errno = 0

Iconv_t cd = iconv_open ("gbk", "utf-8")

Char* errMsg = NULL

If (errno! = 0)

{

ErrMsg = strerror (errno)

Cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report