convert utf-8 topic messages to ISO_8859-1
Hi!
Hi have a problem converting from std_msg::string message with utf-8 encoded chars. If the string is hardcoded it works good, using this sample block of code inside the ros node:
iconv_t cd = iconv_open("UTF-8", "ISO_8859-1");
if (cd == (iconv_t) -1) {
perror("iconv_open failed!");
//return 1;
}
char charstr[] = "ent\xE3o conta-me l\xE1 como \xE9 que te chamas";
char *in_buf = &charstr[0];
size_t in_left = sizeof(charstr) - 1;
printf("hardcode string: %d\n", strlen(charstr));
for(int i = 0; i < strlen(charstr); i++) {
printf("(%d %c) ", charstr[i], charstr[i]);
}
printf("\n");
char output[255];
char *out_buf = &output[0];
size_t out_left = sizeof(output) - 1;
do {
if (iconv(cd, &in_buf, &in_left, &out_buf, &out_left) == (size_t) -1) {
perror("iconv failed!");
//return 1;
}
} while (in_left > 0 && out_left > 0);
*out_buf = 0;
printf("%s -> %s\n", charstr, output);
it prints (and its ok):
ent�o conta-me l� como � que te chamas -> então conta-me lá como é que te chamas
If i try to feed the conversion with the same string from a subscribed topic message (I manually publish exactly the same string), with the same code but only adapted to use the topic message, as bellow.
std::copy( data.begin(), data.end(), charstr );
charstr[data.length()] = 0;
in_buf = &charstr[0];
in_left = sizeof(charstr) - 1;
out_buf = &output[0];
out_left = sizeof(output) - 1;
do {
if (iconv(cd, &in_buf, &in_left, &out_buf, &out_left) == (size_t) -1) {
perror("iconv failed!");
//return 1;
}
} while (in_left > 0 && out_left > 0);
*out_buf = 0;
printf("%s -> %s\n", charstr, output);
its print (wrong because not converted): ent\xE3o conta-me l\xE1 como \xE9 que te chamas -> ent\xE3o conta-me l\xE1 como \xE9 que
So... the code is the same, the input supposedly the same.
How do you handle the utf-8 strings in message topics on c++ nodes?
A code sample is gold!
tks