Re: g_locale_to_utf8() chokes on =?iso-8859-1?q?'=F6'?=



Chris Martin <c martin sheffield ac uk> writes:

To get round this problem, here is a little function which will
convert a string of bytes with the values 1 to 255 into a UTF-8 string
which will pass muster (g_utf8_validate() accepts it).

static void
utf8_cnvt(gchar * source, int source_lgth, gchar * dest)
{
  gchar *p, *q;
  int i;

  if (source_lgth == -1)
    source_lgth = strlen(source);

  for (p = source, q = dest, i = 0; i < source_lgth; p++, i++)
    if (isascii(*p)) {
      *q++ = *p;
    } else {
      *q++ = 0xc0 | (((guchar) (*p) >> 6) & 0x03);
      *q++ = 0x80 | ((guchar) (*p) & 0x3f);
    }
  *q = '\0';
}

Why not use?

 result = g_convert (string, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);

(result will be NULL if conversion failed; pass in &error for the last
 argument if you want a string for the reason for failure.)

Frequently, you'd want g_locale_to_utf8() instead of this, however,
unless you *know* that the source encoding is ISO-8859-1.

Regards,
                                        Owen



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]