Use the multi-Byte Character Set cross-platform (PC, Android, IOS, WP) encoding/decoding method, androidios

Source: Internet
Author: User

Use the multi-Byte Character Set cross-platform (PC, Android, IOS, WP) encoding/decoding method, androidios

With the development of mobile terminals, cross-platform communication architecture design has become an important consideration. Data Communication between PCs, Android, IOS, and WP across multiple platforms, it is necessary to solve the problem of character encoding/decoding.

Multi-Byte Character Set MBCS is not the preferred character set for cross-platform and International Recommendation character sets.

Anyone who writes VC knows that in the past, the default Character Set of VC ++ 6.0 was a multi-Byte Character Set, while the default Character Set of VS2005 and later was Unicode, multi-byte strings are no longer supported by default in VS2013.

However, for many earlier server projects, the multi-Byte Character Set is still used, but the multi-byte character set can still be used for cross-platform Data Communication character encoding/decoding.

Based on the popular science attitude, first explain the multi-byte character set...

Single-byte SBCS: single-Byte character set, which uses a byte to encode characters. For example, ASCII.

Multi-byte MBCS: multi-Byte character set, which uses multiple bytes to encode characters. Such as Unicode and GBK.

When using multi-byte character sets in VC, GBK encoding is actually used in Chinese Windows, and GBK encoding is an extension of GB2312. It is compatible with GB2312 and adds more than Chinese characters, in fact, the GB series includes three versions: GB2312, GBK, and GB18030.

The following describes how to use cross-platform (PC, Android, IOS, and WP) encoding/decoding methods for multi-byte character sets. Only the encoding and decoding of character sets are concerned:

Server:

VC Project Settings, using the multi-Byte Character Set;

Use the standard library std: string for string access;

Android encoding/decoding:

// Decoding
ByteBuffer buffer = ByteBuffer. allocate (nDataLen); buffer. put (m_buffer); buffer. flip (); String szPacket = Charset. forName ("gbk "). decode (buffer ). toString ();
// Encoding
SzPacket. getBytes (Charset. forName ("gbk "));

IOS terminal encoding/decoding:

// Decoding
NSString * szString = [[NSString alloc] initWithData: data encoding: CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000)];
// Encoding
[SzString dataUsingEncoding: CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000)];

The WP code is not at hand. If you need it, you can trust me.

Compared with the encoding/Decoding of Android and IOS, Android is much more powerful in decoding methods. errors that cannot be processed in strings are automatically ignored, while IOS directly returns nil.

Therefore, you must be especially careful when handling IOS strings. For example, if the std: string is incorrect, substr will cause IOS to fail to parse the string.

Generally, multi-byte character sets can be used for cross-platform Data Communication. Currently, the mainstream Android, IOS, and WP platforms support the GB character sets well.

 

Record, for better yourself!

Related Article

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.