Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

ฟังก์ชัน mbtowc ในภาษา C

mbtowc() แปลง Multibyte Sequence เป็น Wide Character

ไวยากรณ์

int mbtowc(wchar_t *pwc, const char *str, size_t n)

พารามิเตอร์

  • pwc: ตัวชี้ wchar_t สำหรับเก็บผลลัพธ์
  • str: ตัวชี้ไปยังไบต์แรกของ Multibyte Character
  • n: จำนวนไบต์สูงสุดที่ตรวจสอบ

ค่าคืน

  • str ไม่เป็น NULL: จำนวนไบต์ที่ใช้, 0 ถ้าเป็น NULL byte, -1 ถ้าล้มเหลว
  • str เป็น NULL: ไม่เป็น 0 ถ้า encoding มี shift state, 0 ถ้า stateless

ตัวอย่าง

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

int main() {
    char *str = "This is tutorialspoint.com";
    wchar_t mb[100];
    int len = mblen(NULL, MB_CUR_MAX);
    mbtowc(mb, str, len * strlen(str));
    wprintf(L"%ls\n", mb);
    return 0;
}

ผลลัพธ์

???