[Original] Linux and Windows communication in garbled problem: UTF-8 to gb2312

Source: Internet
Author: User

========================================================== ==========
This article is the original khler, and the original author information and the original link of this article must be kept completely and completely reprinted.
E-mail:Khler@163.com
QQ: 23381103
MSN: pragmac@hotmail.com

Original address:Http://www.cnblogs.com/khler/archive/2010/09/27/1836559.html
========================================================== ==========

By default, Linux uses UTF-8 encoding, Windows uses gb2312 encoding, when Linux and Windows communication, Chinese characters will appear garbled, the reason is that the use of different character sets. Therefore, the solution is a unified encoding method! In Linux, we can convert the string to gb2312 and then send it to Windows. The function code is as follows:

 


# Include <stddef. h>
# Include <iconv. h>
# Include <assert. h>
# Include <stdio. h>
# Include <cstring>

/*************************************** *******************************
* Function name: utf8togb2312
* Function Description: UTF-8 String Conversion gb2312 Function
* Access Table: None
* Modified table: None
* Input parameters:
* Char * srcstr, UTF-8 string
* Output parameters:
* Char * desbuff: the cache for receiving gb2312 conversion results;
* Int desbufflength: Specifies the cache size for receiving the conversion result.
* Return value: conversion size
* Other Instructions: None
* Modification date version number modifier modification content
*-----------------------------------------------
* 2010.09.27 V1.0 hyh create
*
**************************************** *******************************/
Int utf8togb2312 (char * srcstr, char * desbuff, size_t desbufflength)
{
Assert (strlen (srcstr)> 0 );
Size_t ilen = strlen (srcstr );

Iconv_t CD;
Cd = iconv_open ("gb2312", "UTF-8 ");
Assert (CD! = 0 );
Iconv (Cd, & srcstr, & ilen, & desbuff, & desbufflength );
Iconv_close (CD );
Return desbufflength;
}

 

Note that desbufflength is used to input the cache size for receiving the conversion result, telling the iconv () function How much memory we have prepared for it. At first I saw that iconv () passed its address and thought it was used to receive the converted length. Therefore, if it was set to 0, it was passed in, the result is always 0, and the string is not converted.

 

Converting gb2312 to utf8 is easy:

 

 

/*************************************** *******************************
* Function name: gb2312toutf8
* Function Description: gb2312 String Conversion UTF-8 Function
* Access Table: None
* Modified table: None
* Input parameters:
* Char * srcstr, gb2312 string
* Output parameters:
* Char * desbuff: the cache that receives the UTF-8 transformation result;
* Int desbufflength: Specifies the cache size for receiving the conversion result.
* Return value: conversion size
* Other Instructions: None
* Modification date version number modifier modification content
*-----------------------------------------------
* 2010.09.27 V1.0 hyh create
*
**************************************** *******************************/
Int gb2312toutf8 (char * srcstr, char * desbuff, size_t desbufflength)
{
Assert (strlen (srcstr)> 0 );
Size_t ilen = strlen (srcstr );

Iconv_t CD;
Cd = iconv_open ("UTF-8", "gb2312 ");
Assert (CD! = 0 );
Iconv (Cd, & srcstr, & ilen, & desbuff, & desbufflength );
Iconv_close (CD );
Return desbufflength;
}

 

In my own Linux Ubuntu 9.10, the socket service program successfully communicates with the Windows client program, and solves the problem of garbled characters in socket communication with windows in Linux.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.