有没有办法将符文打印为单个字符?
Is there a way to print Runes as individual characters?
程序的目的:符文密码
编辑:
注意 - 我在下面链接到我自己的 Github 页面
(这只是为了目的(不是开玩笑;它只是为了展示它的目的——我需要帮助的地方(并得到了帮助,再次感谢大家!)
最终编辑:
我现在(感谢非常非常有用提供的答案 Amazing People)完成了我一直在做的项目在;并且 - 对于未来的读者,我还提供了完整的代码。
再一次,如果没有我从下面的人那里得到的所有帮助,这 不会 是可能的,感谢他们 - 再一次!
代码
(缩短了一点)
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#define UNICODE_BLOCK_START 0x16A0
#define UUICODE_BLOCK_END 0x16F1
int main(){
setlocale(LC_ALL, "");
wchar_t SUBALPHA[]=L"ᛠᚣᚫᛞᛟᛝᛚᛗᛖᛒᛏᛋᛉᛈᛇᛂᛁᚾᚻᚹᚷᚳᚱᚩᚦᚢ";
wchar_t DATA[]=L"hello";
int lenofData=0;
int i=0;
while(DATA[i]!='[=10=]'){
lenofData++; i++;
}
for(int i=0; i<lenofData; i++) {
printf("DATA[%d]=%lc",i,DATA[i]);
DATA[i]=SUBALPHA[i];
printf(" is now Replaced by %lc\n",DATA[i]);
} printf("%ls",DATA);
return 0;
}
Output:
DATA[0]=h is now Replaced by ᛠ
DATA[1]=e is now Replaced by ᚣ
DATA[2]=l is now Replaced by ᚫ
DATA[3]=l is now Replaced by ᛞ
DATA[4]=o is now Replaced by ᛟ
ᛠᚣᚫᛞᛟ
下面继续提问
(注意已经解决,参见已接受的答案!)
在Python3中很容易打印符文:
for i in range(5794,5855):
print(chr(i))
产出
ᚢ
ᚣ
(..)
ᛝ
ᛞ
如何在 C 中做到这一点?
- 使用变量(char、char 数组[]、int、...)
有没有办法将 ᛘᛙᛚᛛᛜᛝᛞ 打印为单个字符?
当我尝试时,它只是打印出关于多字符字符常量的两个警告 'ᛟ'
。
我试过将它们作为一个 char 数组,一个“字符串”(例如 char s1 = "ᛟᛒᛓ";)
- 然后打印出 s1 的第一个
(ᛟ)
字符: printf("%c", s1[0]);
现在,这对其他人来说似乎是错误的。
我是如何想到这个的一个例子:
将符文打印为“单个字符”:
打印例如 'A'
printf("%c", 65); // 'A'
我该怎么做,(如果可能的话)但使用符文?
我已经尝试过将它的数字值打印为 char,这会导致问号,以及 - 其他“未定义”结果。
由于我不太记得到目前为止我尝试过的所有事情,我会尽力制定这个 post。
如果有人发现一个非常简单(也许 him/her - 甚至是显而易见的)解决方案(或 trick/workaround)-
如果你能指出来,我会非常高兴!谢谢!
这已经困扰我很长一段时间了,虽然它在 python
中有效 - 而且它在 c
中有效(据我所知)如果你只是“打印”它(不是通过任何变量)但是,例如:printf("ᛟ");
这有效,但正如我所说,我想做同样的事情,但是,通过变量。 (例如,char runes[]="ᛋᛟ";)
然后:printf("%c", runes[0]); // to get 'ᛋ' as the output
(或类似的,它不需要是 %c
,也不需要是 char array/char 变量)我只是想了解如何 - 做以上,(希望不要太难读)
我正在 Linux,并使用 GCC。
外部链接
要保存 8 位范围之外的字符,您需要 wchar_t
(不一定是 Unicode)。虽然 wchar_t
是基本的 C 类型,但您需要 #include <wchar.h>
才能使用它,并使用宽字符版本的字符串和 I/O 函数(例如下面显示的 putwc
).
您还需要确保已激活支持宽字符的语言环境,该语言环境应与终端仿真器所使用的语言环境相同(如果您正在写入终端)。通常,这将是默认语言环境,使用字符串 ""
.
选择
这里有一个与您的 Python 代码等效的简单代码:
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
int main(void) {
setlocale(LC_ALL, "");
/* As indicated in a comment, I should have checked the
* return value from `putwc`; if it returns EOF and errno
* is set to EILSEQ, then the current locale can't handle
* runic characters.
*/
for (wchar_t wc = 5794; wc < 5855; ++wc)
putwc(wc, stdout);
putwc(L'\n', stdout);
return 0;
}
(直播时间 ideone。)
作为(宽)字符的字符串存储在堆栈中
如果你想将符文 (wchar_t) 添加到字符串中,你可以按以下方式进行:
使用wcsncpy:(对char有点矫枉过正,感谢chqrlie的注意)
#define UNICODE_BLOCK_START 0x16A0 // see wikipedia link for the start
#define UUICODE_BLOCK_END 0x16F0 // true ending of Runic wide chars
int main(void) {
setlocale(LC_ALL, "");
wchar_t buffer[UUICODE_BLOCK_END - UNICODE_BLOCK_START + sizeof(wchar_t) * 2];
int i = 0;
for (wchar_t wc = UNICODE_BLOCK_START; wc <= UUICODE_BLOCK_END; wc++)
buffer[i++] = wc;
buffer[i] = L'[=10=]';
printf("%ls\n", buffer);
return 0;
}
关于宽字符(和 Unicode)
更好地理解什么是 wide char, you have to think of it as a set of bits set that exceed the original range used for character which was 2^8 = 256
or, with left shifting、1 << 8
)。
当你只需要打印键盘上的内容时就足够了,但是当你需要打印亚洲字符或其他 unicode 字符时,这就不够了,这就是为什么 Unicode standard was created. You can find more about the very different and exotic characters that exist, along with their range (named unicode blocks), on wikipedia ,在你的情况下 runic
.
Range U+16A0..U+16FF - Runic (86 characters), Common (3 characters)
注意: 你的 Runic 宽字符结束于 0x16F1,略早于 0x16FF(0x16F1 到 0x16FF 未定义)
您可以使用以下函数将宽字符打印为位:
void print_binary(unsigned int number)
{
char buffer[36]; // 32 bits, 3 spaces and one [=11=]
unsigned int mask = 0b1000000000000000000000000000;
int i = 0;
while (i++ < 32) {
buffer[i] = '0' + !!(number & (mask >> i));
if (i && !(i % 8))
buffer[i] = ' ';
}
buffer[32] = '[=11=]';
printf("%s\n", buffer);
}
你在循环中调用:
print_binary((unsigned int)wc);
它将让您更好地理解您的宽字符在机器级别是如何表示的:
ᛞ
0000000 0000001 1101101 1100000
注意: 你需要注意细节:不要忘记最后的 L'[=17=]'
并且你需要使用 %ls
来获得输出printf
.
程序的目的:符文密码
编辑:
注意 - 我在下面链接到我自己的 Github 页面 (这只是为了目的(不是开玩笑;它只是为了展示它的目的——我需要帮助的地方(并得到了帮助,再次感谢大家!)
最终编辑:
我现在(感谢非常非常有用提供的答案 Amazing People)完成了我一直在做的项目在;并且 - 对于未来的读者,我还提供了完整的代码。
再一次,如果没有我从下面的人那里得到的所有帮助,这 不会 是可能的,感谢他们 - 再一次!
代码
(缩短了一点)
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#define UNICODE_BLOCK_START 0x16A0
#define UUICODE_BLOCK_END 0x16F1
int main(){
setlocale(LC_ALL, "");
wchar_t SUBALPHA[]=L"ᛠᚣᚫᛞᛟᛝᛚᛗᛖᛒᛏᛋᛉᛈᛇᛂᛁᚾᚻᚹᚷᚳᚱᚩᚦᚢ";
wchar_t DATA[]=L"hello";
int lenofData=0;
int i=0;
while(DATA[i]!='[=10=]'){
lenofData++; i++;
}
for(int i=0; i<lenofData; i++) {
printf("DATA[%d]=%lc",i,DATA[i]);
DATA[i]=SUBALPHA[i];
printf(" is now Replaced by %lc\n",DATA[i]);
} printf("%ls",DATA);
return 0;
}
Output:
DATA[0]=h is now Replaced by ᛠ DATA[1]=e is now Replaced by ᚣ DATA[2]=l is now Replaced by ᚫ DATA[3]=l is now Replaced by ᛞ DATA[4]=o is now Replaced by ᛟ ᛠᚣᚫᛞᛟ
下面继续提问
(注意已经解决,参见已接受的答案!)
在Python3中很容易打印符文:
for i in range(5794,5855):
print(chr(i))
产出
ᚢ ᚣ (..) ᛝ ᛞ
如何在 C 中做到这一点?
- 使用变量(char、char 数组[]、int、...)
有没有办法将 ᛘᛙᛚᛛᛜᛝᛞ 打印为单个字符?
当我尝试时,它只是打印出关于多字符字符常量的两个警告 'ᛟ'
。
我试过将它们作为一个 char 数组,一个“字符串”(例如 char s1 = "ᛟᛒᛓ";)
- 然后打印出 s1 的第一个
(ᛟ)
字符:printf("%c", s1[0]);
现在,这对其他人来说似乎是错误的。
我是如何想到这个的一个例子:
将符文打印为“单个字符”:
打印例如 'A'
printf("%c", 65); // 'A'
我该怎么做,(如果可能的话)但使用符文?
我已经尝试过将它的数字值打印为 char,这会导致问号,以及 - 其他“未定义”结果。
由于我不太记得到目前为止我尝试过的所有事情,我会尽力制定这个 post。
如果有人发现一个非常简单(也许 him/her - 甚至是显而易见的)解决方案(或 trick/workaround)-
如果你能指出来,我会非常高兴!谢谢!
这已经困扰我很长一段时间了,虽然它在 python
中有效 - 而且它在 c
中有效(据我所知)如果你只是“打印”它(不是通过任何变量)但是,例如:printf("ᛟ");
这有效,但正如我所说,我想做同样的事情,但是,通过变量。 (例如,char runes[]="ᛋᛟ";)
然后:printf("%c", runes[0]); // to get 'ᛋ' as the output
(或类似的,它不需要是 %c
,也不需要是 char array/char 变量)我只是想了解如何 - 做以上,(希望不要太难读)
我正在 Linux,并使用 GCC。
外部链接
要保存 8 位范围之外的字符,您需要 wchar_t
(不一定是 Unicode)。虽然 wchar_t
是基本的 C 类型,但您需要 #include <wchar.h>
才能使用它,并使用宽字符版本的字符串和 I/O 函数(例如下面显示的 putwc
).
您还需要确保已激活支持宽字符的语言环境,该语言环境应与终端仿真器所使用的语言环境相同(如果您正在写入终端)。通常,这将是默认语言环境,使用字符串 ""
.
这里有一个与您的 Python 代码等效的简单代码:
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
int main(void) {
setlocale(LC_ALL, "");
/* As indicated in a comment, I should have checked the
* return value from `putwc`; if it returns EOF and errno
* is set to EILSEQ, then the current locale can't handle
* runic characters.
*/
for (wchar_t wc = 5794; wc < 5855; ++wc)
putwc(wc, stdout);
putwc(L'\n', stdout);
return 0;
}
(直播时间 ideone。)
作为(宽)字符的字符串存储在堆栈中
如果你想将符文 (wchar_t) 添加到字符串中,你可以按以下方式进行:
使用wcsncpy:(对char有点矫枉过正,感谢chqrlie的注意)
#define UNICODE_BLOCK_START 0x16A0 // see wikipedia link for the start
#define UUICODE_BLOCK_END 0x16F0 // true ending of Runic wide chars
int main(void) {
setlocale(LC_ALL, "");
wchar_t buffer[UUICODE_BLOCK_END - UNICODE_BLOCK_START + sizeof(wchar_t) * 2];
int i = 0;
for (wchar_t wc = UNICODE_BLOCK_START; wc <= UUICODE_BLOCK_END; wc++)
buffer[i++] = wc;
buffer[i] = L'[=10=]';
printf("%ls\n", buffer);
return 0;
}
关于宽字符(和 Unicode)
更好地理解什么是 wide char, you have to think of it as a set of bits set that exceed the original range used for character which was 2^8 = 256
or, with left shifting、1 << 8
)。
当你只需要打印键盘上的内容时就足够了,但是当你需要打印亚洲字符或其他 unicode 字符时,这就不够了,这就是为什么 Unicode standard was created. You can find more about the very different and exotic characters that exist, along with their range (named unicode blocks), on wikipedia ,在你的情况下 runic
.
Range U+16A0..U+16FF - Runic (86 characters), Common (3 characters)
注意: 你的 Runic 宽字符结束于 0x16F1,略早于 0x16FF(0x16F1 到 0x16FF 未定义)
您可以使用以下函数将宽字符打印为位:
void print_binary(unsigned int number)
{
char buffer[36]; // 32 bits, 3 spaces and one [=11=]
unsigned int mask = 0b1000000000000000000000000000;
int i = 0;
while (i++ < 32) {
buffer[i] = '0' + !!(number & (mask >> i));
if (i && !(i % 8))
buffer[i] = ' ';
}
buffer[32] = '[=11=]';
printf("%s\n", buffer);
}
你在循环中调用:
print_binary((unsigned int)wc);
它将让您更好地理解您的宽字符在机器级别是如何表示的:
ᛞ
0000000 0000001 1101101 1100000
注意: 你需要注意细节:不要忘记最后的 L'[=17=]'
并且你需要使用 %ls
来获得输出printf
.