Cross-platform (PC, Android, IOS, WP) encoding/decoding methods using multibyte character sets

Source: Internet
Author: User

With the development of mobile, cross-platform has become a communication architecture design important considerations, PC, Android, IOS, WP and other cross-platform communication between the data, it is necessary to solve the problem of character encoding/decoding.

The multi-byte character set MBCS is not a cross-platform preferred character set, and the recommended character set for cross-platform and internationalization is definitely Unicode.

People who write VC know that in the previous vc++6.0 the default character set is a multibyte character set, and VS2005 and later the default character set is unicode,vs2013 in the default is no longer support for multibyte strings.

However, for many older service-side projects, the multibyte character set is still used, although the use of multibyte character sets can still enable the character encoding/decoding of cross-platform data communication.

In the attitude of popular science, first explain the multi-byte character set ...

Single-byte sbcs:single-byte character set, which encodes a character with a byte. such as: ASCII.

Multibyte mbcs:multi-byte character set, which encodes characters with multiple byte. such as UNICODE,GBK.

When using multi-byte character set in VC, the GBK encoding is actually used on Chinese windows, GBK encoding is an extension of GB2312, forward compatible GB2312 and adding more than 2W Chinese characters, in fact the GB series contains GB2312, GBK, GB18030 three versions.

The following is a cross-platform (PC, Android, IOS, WP) encoding/decoding method using multibyte character sets, focusing only on the codec of the character set:

Service side:

VC project settings, using multibyte character set;

string access using standard library std::string;

Android-encoded/decoded:

Decoding
Bytebuffer buffer = bytebuffer.allocate (Ndatalen); Buffer.put (M_buffer); Buffer.flip (); String Szpacket = Charset.forname ("GBK"). Decode (buffer). toString ();
Coding
Szpacket.getbytes (Charset.forname ("GBK"));

IOS-encoded/decoded:

Decoding
NSString *szstring = [[NSString alloc] Initwithdata:data encoding:cfstringconvertencodingtonsstringencoding ( kcfstringencodinggb_18030_2000)];
Coding
[Szstring datausingencoding:cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000)];

WP end of the code is not at hand, there is a need to private messages to me.

Android is much stronger than encoding/decoding for iOS, and the error characters that cannot be handled in a string are automatically ignored, and iOS returns nil directly.

So be very careful with the string handling of iOS, such as std::string's incorrect substr will cause iOS to be unresolved.

In general, the use of multi-byte character sets to achieve cross-platform data communication is no problem, the current mainstream Android, IOS, WP three platforms for the GB system of character set support is good.

Record, for the better of myself!

Cross-platform (PC, Android, IOS, WP) encoding/decoding methods using multibyte character sets

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.