SumProject/my_error/testunicodephone.cpp
2025-03-22 22:38:52 +08:00

49 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
void str_to_unicode(const char *input, uint8_t *output) {
uint16_t unicode;
uint8_t *ptr = output;
while (*input) {
// UTF-8 转 Unicode仅处理基本多语言平面字符
if ((*input & 0xE0) == 0xE0) { // 3字节UTF-8字符如中文
unicode = ((input[0] & 0x0F) << 12) | ((input[1] & 0x3F) << 6) | (input[2] & 0x3F);
input += 3;
} else if ((*input & 0xC0) == 0xC0) { // 2字节UTF-8字符如拉丁扩展字符
unicode = ((input[0] & 0x1F) << 6) | (input[1] & 0x3F);
input += 2;
} else { // 1字节ASCII字符
unicode = *input;
input += 1;
}
// 转换为大端序UTF-16BE
*ptr++ = (unicode >> 8) & 0xFF;
*ptr++ = unicode & 0xFF;
}
*ptr = '\0'; // 结束符
}
int main()
{
int n;
// string s;
uint8_t a[10005];
char s[100005];
// cin>>s;
scanf("%s",s);
int i=-1;
char sc[1005];
str_to_unicode(s,a);
for(int i=0;i<=100;i++)
{
printf("%X",a[i]);
}
while(s[++i])
{
sprintf(sc+i*4,"00%X",s[i]);
printf("00%X",s[i]);
}
printf("\n%s",sc);
}