系统相关
首页 > 系统相关> > 如何将非ASCII(土耳其语)字符压缩为C中的1字节用于Linux?

如何将非ASCII(土耳其语)字符压缩为C中的1字节用于Linux?

作者:互联网

我有一份土耳其语单词列表.我需要比较它们的长度.但由于一些土耳其字符非ASCII,我无法正确比较它们.非ASCII土耳其语字符包含2个字节.

例如:

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

int main()
{
    char s1[] = "ab";
    char s2[] = "çş";

    printf("%d\n", strlen(s1)); // it prints 2
    printf("%d\n", strlen(s2)); // it prints 4

    return 0;
}

我的朋友说可以在Windows中使用下面的代码行:

system("chcp 1254");

他说,它将特殊的土耳其字符填充到扩展的ASCII表中.但它在Linux中不起作用.

有没有办法在Linux中做到这一点?

解决方法:

一种可能性是使用宽字符串来存储单词.它不会将字符存储为一个字节,但它可以解决您的主要问题.使用您的语言获得一组功能.该计划如下所示:

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

int main()
{
    wchar_t s1[] = L"ab";
    wchar_t s2[] = L"çş";

    printf("%d\n", wcslen(s1)); // it prints 2
    printf("%d\n", wcslen(s2)); // it prints 2

    return 0;
}

标签:ascii,c-3,linux,non-ascii-characters
来源: https://codeday.me/bug/20190527/1162885.html