sprintf 中的编码

发布于 2025-01-07 17:02:09 字数 282 浏览 0 评论 0原文

在以下代码中:

char test[50];
sprintf(test, "áéíóú");

有没有办法让 sprintf 将输入字符解释为 Windows-1252 而不是 Unicode? 我的意思是,让测试包含0xE1E9EDF3FA...而不是0xC3A1C3A9C3ADC3B3C3BA...

In the following code:

char test[50];
sprintf(test, "áéíóú");

is there a way to make sprintf interpret input characters as Windows-1252 instead of Unicode?
I mean, to make test contain 0xE1E9EDF3FA... instead of 0xC3A1C3A9C3ADC3B3C3BA...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2025-01-14 17:02:09

您必须在文本编辑程序中对其进行编辑。这是包含源代码的实际文件的问题。

为此,在大多数编辑器和 IDE 中,都有一个名为 ENCODING

EDIT 的菜单:更具体地说,对于 Geany,它似乎是您正在运行的软件,请转到:

文档 >>> 设置编码 >> 西欧 >>> 西方 (1252)

You have to edit this from inside your text editing program. This is a matter of the actual file that contains your source code.

To do that in most editors and IDEs there is a menu called ENCODING

EDIT: More specifically for Geany, which appears to be the software you are running go to:

Document >> Set Encoding >> West European >> Western (1252)

ぶ宁プ宁ぶ 2025-01-14 17:02:09
#include <stdio.h>
#include <stdlib.h>

size_t utf2bin(unsigned char *dst, unsigned char *src, size_t dstlen);

int main (void)
{
unsigned char src[] = {0xC3, 0xA1, 0xC3, 0xA9, 0xC3, 0xAD, 0xC3, 0xB3, 0xC3, 0xBA, 0};
unsigned char dst[100];
size_t ret;

// ret = mbstowcs(dst,src, sizeof dst);
// ret = wcstombs(dst,src, sizeof dst);
ret = utf2bin(dst,src, sizeof dst);

printf("Src=%s.\n", src );
printf("Dst=%s.\n", dst );

return 0;
}

/* This code does not _interpret_ the utf8 code-points, only converts
** them to 8bit "characters" as used in the consumer-grade "operating systems" supplied by Microsoft.
**
** Warning: only two byte codes are handled here. Longer codes will produce erroneous output.
*/
size_t utf2bin(unsigned char *dst, unsigned char *src, size_t dstlen)
{
size_t pos;
for ( pos = 0; pos< dstlen; pos++ ) {
        if ((*src & 0xe0) == 0xc0) {
                dst[pos] = ((src[0] & 3) << 6) | (src[1] & 0x3f);
                src += 2;
                }
        else dst[pos] = *src++;
        }
if (pos && pos >= dstlen) pos--;
dst[pos] = 0;
return pos;
}
#include <stdio.h>
#include <stdlib.h>

size_t utf2bin(unsigned char *dst, unsigned char *src, size_t dstlen);

int main (void)
{
unsigned char src[] = {0xC3, 0xA1, 0xC3, 0xA9, 0xC3, 0xAD, 0xC3, 0xB3, 0xC3, 0xBA, 0};
unsigned char dst[100];
size_t ret;

// ret = mbstowcs(dst,src, sizeof dst);
// ret = wcstombs(dst,src, sizeof dst);
ret = utf2bin(dst,src, sizeof dst);

printf("Src=%s.\n", src );
printf("Dst=%s.\n", dst );

return 0;
}

/* This code does not _interpret_ the utf8 code-points, only converts
** them to 8bit "characters" as used in the consumer-grade "operating systems" supplied by Microsoft.
**
** Warning: only two byte codes are handled here. Longer codes will produce erroneous output.
*/
size_t utf2bin(unsigned char *dst, unsigned char *src, size_t dstlen)
{
size_t pos;
for ( pos = 0; pos< dstlen; pos++ ) {
        if ((*src & 0xe0) == 0xc0) {
                dst[pos] = ((src[0] & 3) << 6) | (src[1] & 0x3f);
                src += 2;
                }
        else dst[pos] = *src++;
        }
if (pos && pos >= dstlen) pos--;
dst[pos] = 0;
return pos;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文