funkadelic
funkadelic

Reputation: 321

Encoding in sprintf

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...

Upvotes: 4

Views: 1862

Answers (2)

wildplasser
wildplasser

Reputation: 44250

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

Upvotes: 1

Lefteris
Lefteris

Reputation: 3256

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)

Upvotes: 3

Related Questions