在sprintf中编码

时间:2012-02-21 16:47:19

标签: c encoding printf

在以下代码中:

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

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

2 个答案:

答案 0 :(得分:3)

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

要在大多数编辑器和IDE中执行此操作,有一个名为ENCODING

的菜单

编辑:更专门针对Geany,它似乎是您正在运行的软件:

文件>> 设置编码>> 西欧>> Western (1252)

答案 1 :(得分:1)

#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;
}