2012-02-21 37 views
3

在下面的代碼:編碼中的sprintf

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

有沒有一種方法,使的sprintf解釋輸入字符Windows-1252統一的呢? 我的意思是,要測試包含0xE1E9EDF3FA ...而不是0xC3A1C3A9C3ADC3B3C3BA ...

+0

否 - 'sprintf'只是將它給出的任何輸入內容複製到你告訴它的地方。這將取決於您使用的編輯器來決定如何對重音字符進行編碼 - 您必須(至少)告訴我們您正在使用哪種編輯器來幫助任何人進行編碼。 – 2012-02-21 16:48:35

+0

我正在使用Geany 0.18 – funkadelic 2012-02-21 16:49:42

+0

嗯...'fraid我無法幫助你,但也許別人已經使用它,並將能夠... – 2012-02-21 16:52:29

回答

3

你必須從你的文本編輯程序中進行編輯。這是包含您的源代碼的實際文件的問題。

要做到這一點大部分編輯器和IDE有一個叫做編碼菜單

編輯:更具體的Geany,這似乎是你正在運行轉到軟件:

文件 >>設置編碼 >>西歐 >>西方(1252)

+0

我不知道它取決於編輯器......謝謝! – funkadelic 2012-02-21 17:55:05

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