添加cJSON域utf8转unicode函数

This commit is contained in:
gb 2023-02-07 16:51:53 +08:00
parent 01dddf987e
commit 48770dd341
2 changed files with 41 additions and 0 deletions

View File

@ -253,6 +253,44 @@ static const char *parse_string(cJSON *item,const char *str)
item->type=cJSON_String; item->type=cJSON_String;
return ptr; return ptr;
} }
char* cJSON_utf8_2_unic(const char* utf8)
{
char* unic = (char*)malloc(strlen(utf8) * 3 + 8);
unsigned char * cur = unic;
while (*cur = *utf8++)
{
if ((*cur & 0x0f0) == 0x0e0)
{
if (((unsigned char)utf8[0] & 0x0c0) == 0x80 &&
((unsigned char)utf8[1] & 0x0c0) == 0x80)
{
char* hex = "0123456789ABCDEF";
unsigned short us = *cur & 0x0f;
us <<= 6;
us += utf8[0] & 0x3f;
us <<= 6;
us += utf8[1] & 0x3f;
*cur++ = '\\';
*cur++ = 'u';
cur[3] = hex[us & 0x0f];
us >>= 4;
cur[2] = hex[us & 0x0f];
us >>= 4;
cur[1] = hex[us & 0x0f];
us >>= 4;
cur[0] = hex[us & 0x0f];
cur += 3;
utf8 += 2;
}
}
cur++;
}
*cur++ = 0;
return unic;
}
/* Render the cstring provided to an escaped version that can be printed. */ /* Render the cstring provided to an escaped version that can be printed. */
static char *print_string_ptr(const char *str,printbuffer *p) static char *print_string_ptr(const char *str,printbuffer *p)

View File

@ -135,6 +135,9 @@ extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_en
extern void cJSON_Minify(char *json); extern void cJSON_Minify(char *json);
// convert e681a2 to \u6062, call 'free' to free the returned value
extern char* cJSON_utf8_2_unic(const char* utf8);
/* Macros for creating things quickly. */ /* Macros for creating things quickly. */
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())