bool latin1ToUtf8(char* inText, std::string& outText) { outText = ""; if(!inText) return false; int32_t inLen = strlen(inText); if(!inLen) return false; int32_t outLen = inLen << 1; uint8_t* outBuf = new uint8_t[outLen]; int32_t res = isolat1ToUTF8(outBuf, &outLen, (uint8_t*)inText, &inLen); if(res < 0) { delete[] outBuf; return false; } outBuf[outLen] = '\0'; outText = (char*)outBuf; delete[] outBuf; return true; }
//-------------------------------------------------- // Helper function that converts ISO Latin1 to UTF8 // ascii - input data in ISO Latin1 // utf8out - output buffer for UTF8 text // outlen - output buffer length // returns output buffer //-------------------------------------------------- EXP_OPTION char* ascii2utf8(const char* ascii, char* utf8out, int* outlen) { int inlen = strlen(ascii); memset(utf8out, 0, *outlen); isolat1ToUTF8((unsigned char*)utf8out, outlen, (const unsigned char*)ascii, &inlen); return utf8out; }
xmlChar* toUTF8 (const char* str, int len) { int out_size = 2*len; int out_len; xmlChar* out = calloc (out_size, sizeof (xmlChar)); while (1) { int in_len = len; out_len = out_size; if (isolat1ToUTF8 (out, &out_len, BAD_CAST str, &in_len) < 0) { // Conversion error free (out); return NULL; } if (in_len >= len) break; out_size *= 2; out = realloc (out, out_size * sizeof (xmlChar)); } if (out_len >= out_size) // drats, no space to store a terminator out = realloc (out, (out_size + 1) * sizeof (xmlChar)); out[out_len] = '\0'; return out; }
void APar_PrintName(char *name) { memset(twenty_byte_buffer, 0, sizeof(char)*20); isolat1ToUTF8((unsigned char*)twenty_byte_buffer, 10, (unsigned char*)name, 4); if (UnicodeOutputStatus == WIN32_UTF16) { APar_fprintf_UTF8_data(twenty_byte_buffer); } else { fprintf(stdout, "%s", twenty_byte_buffer); } }
/** * Convert a string from ISO-8859-1 to UTF-8 * Allocates double of ISO string size for UTF-8 string size * Input * @strISO: a pointer to an array of ISO Latin 1 chars * Output * @*strUTF: a pointer to an array of UTF-8 chars * * Returns: * -2 if any of char pointers are NULL * Otherwise, returns isolat1ToUTF8 return * Notes: strUTF may be freed by the caller if return >= 0 */ int convertISO88591toUTF8 (unsigned char *strISO, unsigned char **strUTF) { int lenISO, lenUTF, rc; if (strISO == NULL || strUTF == NULL) return(-2); lenISO = strlen((char *)strISO); lenUTF = 2 * lenISO; *strUTF = BAD_CAST malloc ((size_t) (lenUTF + 1)); rc = isolat1ToUTF8 (*strUTF, &lenUTF, strISO, &lenISO); (*strUTF)[lenUTF] = '\0'; if (rc < 0) free(*strUTF); return rc; }