⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unicode.c

📁 realview22.rar
💻 C
字号:
/*
 * Unicode / Shift-JIS example, to demonstrate C library support
 *
 * Copyright 2002 ARM Limited. All rights reserved.
 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <wchar.h>

#pragma import __use_sjis_ctype

wchar_t wstring1[] = L"慡妏僇僞僇僫 (Full width katakana)";
wchar_t wstring2[] = L"敿妏独哦 (Half width katakana)";
wchar_t wstring3[] = L"傂傜偑側 (Hiragana)";
wchar_t wstring4[] = L"娍帤 (Kanji)";

void display(wchar_t *wstr)
{
    char string[256];
    wcstombs(string, wstr, sizeof(string));
    printf("%s\n", string);
}

int main(void)
{
    char string[256];
    wchar_t wstr[256];
    int i, j, k, wi;
    mbstate_t state = {0};
    char str_e[] = "Unicode example";
    char str_j[] = "Unicode 僒儞僾儖傾僾儕働乕僔儑儞";
    
    /*
     * Display the above strings.
     */

    printf("\n");
    printf(str_e);
    printf("\n");
    printf(str_j);
    printf("\n\n");

    display(wstring1);
    display(wstring2);
    display(wstring3);
    display(wstring4);

    /*
     * Ask the user to type a string. Show its length in both bytes
     * and characters, and split the characters up to be displayed
     * one per line with both SJIS and Unicode hex representations.
     * Then reconstitute it to show that round-trip conversion
     * worked.
     */
    printf("\nTo test multibyte character support, please enter some text containing\n"
           "both single-byte (ASCII) and/or double-byte (Japanese) characters.\n");
    printf("[僔儞僌儖僶僀僩暥帤乮ASCII乯偲僟僽儖僶僀僩暥帤乮擔杮岅側偳乯偑\n"
           "崿偠偭偨僥僉僗僩傪擖椡偟偰偔偩偝偄丅儅儖僠僶僀僩丒僒億乕僩傪僥僗僩偱偒傑偡丅]\n");

    printf("\nEnter string: ");

    fflush(stdout);
    fgets(string, sizeof(string), stdin);
    string[strcspn(string, "\r\n")] = '\0';   /* \n isn't interesting */

    printf("\nYour string is %d bytes long, and %d characters long.\n",
           strlen(string), mbstowcs(NULL, string, strlen(string)+1));

    printf("[擖椡偝傟偨暥帤楍偼 %d 僶僀僩丄%d 暥帤偱偟偨丅]\n\n",
           strlen(string), mbstowcs(NULL, string, strlen(string)+1));

    /*
     * Now convert gradually to wide characters using mbrtowc().
     */
    i = j = wi = 0;
    while (1) {
        wchar_t wc;
        size_t ret = mbrtowc(&wc, string+i, 2, &state);
        if (ret == (size_t)-2 || ret == (size_t)-1) {
            printf("\nThere was a problem decoding the multibyte string.\n");
            printf("\n[儅儖僠僶僀僩暥帤楍傪僨僐乕僪拞偵栤戣偑敪惗偟傑偟偨]\n");
            return 1;
        } else if (ret == 0) {
            break;                     /* we hit \0, end of string */
        } else {
            i += ret;
            printf("Shift-JIS:");
            for (k = j; k < i; k++)
                printf(" %02X", (unsigned char)string[k]);
            printf("%*sUnicode: %04X  %.*s\n", 8-3*(i-j), "",
                   (unsigned short)wc, i-j, string+j);
            wstr[wi++] = wc;
            j = i;
        }
    }
    wstr[wi] = L'\0';

    printf("\nYour string will now be converted back into the Shift-JIS\n"
           "multibyte encoding from Unicode. This string should look the\n"
           "same as the one you typed in:\n");
    printf("[懳徾暥帤楍傪 Unicode 偐傜 Shift-JIS 偵栠偟偰傒傑偡丅\n"
           "嵟弶偵擖椡偝傟偨暥帤楍偲摨偠偱偁傞偙偲傪妋擣偟偰偔偩偝偄丅]\n\n");

    display(wstr);

    printf("\n\nShift-JIS test concluded.\n");
    printf("[Shift-JIS 僥僗僩偼偙傟偱廔傢傝偱偡]\n");

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -