Raspberry Pi Route (032)-Character issues (2)-How to get a GB2312 encoding of a Chinese character in C language (RPM)

Source: Internet
Author: User



In C/C + +, ASCII is supported, but GB2312 is compatible with ASCII in Chinese character coding, so the GB2312 encoding of Chinese characters can be obtained



The GB2312 is two bytes, the first byte is high eight bits, the second byte is low eight bits, such as the following program:
#include <stdio.h>
#include <string.h>
int main ()
{
Char a[5];
strcpy (A, "ah");
printf ("%xh%xh\n", (unsigned char) a[0], (unsigned char) a[1]);
return 0;
}
A[0] is the "ah" character of the high eight-bit, a[1] is low eight bits, the program run result is a[0]=b0 A[1]=A1, with 16 in the system, so "ah" word GB2312 code is B0A1 ~
The output is coerced to unsigned char, otherwise it becomes negative because the encoding is a 8-bit unsigned integer.
Of course, you can turn it into 10, for example.
int b;
b= (unsigned char) a[0]*256+ (unsigned char) a[1];



b that is the 10 binary GB2312 of the Chinese character encoded





% a,% A read in a floating point value (only C99 is valid)
% c reads a character
% d read in decimal integer
% i Read in decimal, octal, hexadecimal integer
% o read octal integer
% x,% X read in hexadecimal integer
% s reads a character string and ends with a space, tab, or newline character.
% f,% F,% e,% E,% g,% G are used to input real numbers, which can be entered in decimal or exponential form.
% p reads in a pointer
% u reads in an unsigned decimal integer
% n The number of equivalent characters of the value read so far
% [] Scanning character set
%% read% symbol 


Reprint: http://blog.csdn.net/han_kin/article/details/47838715



Raspberry Pi Route (032)-Character issues (2)-How to get a GB2312 encoding of a Chinese character in C language (RPM)


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.