pg-*_*ban 4 c macos xcode character-encoding
我刚刚学习了C并得到了一个任务,我们必须将纯文本转换为莫尔斯代码并返回.(我大部分都熟悉Java,所以请遵守我使用的条款).
为此,我有一个包含所有字母字符串的数组.
char *letters[] = {
".- ", "-... ", "-.-. ", "-.. ", ".", "..-." etc
Run Code Online (Sandbox Code Playgroud)
我写了一个函数来返回所需字母的位置.
int letter_nr(unsigned char c)
{
return c-97;
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,但是作业规范要求处理瑞典语的变形字母åäö.瑞典语字母与最后用这三个字母的英语相同.我尝试检查这些,如下:
int letter_nr(unsigned char c)
{
if (c == 'å')
return 26;
if (c == 'ä')
return 27;
if (c == 'ö')
return 28;
return c-97;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我尝试测试这个函数时,我得到了所有这三个函数的相同值:98.这是我的主要测试函数:
int main()
{
unsigned char letter;
while(1)
{
printf("Type a letter to get its position: ");
scanf("%c", &letter);
printf("%d\n", letter_nr(letter));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?
gnu*_*nud 10
字符常量的编码实际上取决于您的语言环境设置.
最安全的选择是使用宽字符和相应的功能.您将字母表声明为const wchar_t* alphabet = L"abcdefghijklmnopqrstuvwxyzäöå",并将单个字符声明为L'ö';
这个小的示例程序适用于我(也在UNIX控制台上使用UTF-8) - 试试吧.
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(int argc, char** argv)
{
wint_t letter = L'\0';
setlocale(LC_ALL, ""); /* Initialize locale, to get the correct conversion to/from wchars */
while(1)
{
if(!letter)
printf("Type a letter to get its position: ");
letter = fgetwc(stdin);
if(letter == WEOF) {
putchar('\n');
return 0;
} else if(letter == L'\n' || letter == L'\r') {
letter = L'\0'; /* skip newlines - and print the instruction again*/
} else {
printf("%d\n", letter); /* print the character value, and don't print the instruction again */
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
示例会话:
Type a letter to get its position: a
97
Type a letter to get its position: A
65
Type a letter to get its position: Ö
214
Type a letter to get its position: ö
246
Type a letter to get its position: Å
197
Type a letter to get its position: <^D>
Run Code Online (Sandbox Code Playgroud)
据我所知,在Windows上,这不适用于Unicode BMP之外的字符,但这不是问题.
| 归档时间: |
|
| 查看次数: |
2956 次 |
| 最近记录: |