How to ASCIIfy filenames in Mbed?

Hi there,

For some time i was investigating how to use non-ASCII characters in my Mbed code.
I found a working way to output a wide range of special characters and also emojis on the screen attached to my board and also in the serial monitor which is great.

However i am facing another challenge: the user might input location names like “Bad Gottleuba-Berggießhübel” and my code should convert that string to ASCII characters only to generate a portable filename Mbed is happy with.

I used iconv in PHP for such purposes and this PHP code

<?php
echo iconv('UTF-8', 'ASCII//TRANSLIT', "Bad Gottleuba-Berggießhübel\n");
?>

outputs “Bad Gottleuba-Berggiesshubel”, exactly what i would need now in Mbed.

I found iconv.h also exists in C/C++ so came up with this code:

#include <iostream>
#include <fstream>
#include <iconv.h>

int main()
{
    char src[] = "Bad Gottleuba-Berggießhübel";
    char dst[100];
    size_t srclen = 50;
    size_t dstlen = 100;
 
    fprintf(stderr,"in: %s\n",src);
 
    char * pIn = src;
    char * pOut = ( char*)dst;
 
    iconv_t conv = iconv_open("ASCII//TRANSLIT","UTF-8");
    iconv(conv, &pIn, &srclen, &pOut, &dstlen);
    iconv_close(conv);
 
    fprintf(stderr,"out: %s\n",dst);
}

Above code works on onlinegdb.com though it outputs “Bad Gottleuba-Berggiessh?bel” (notice the ü character has failed to get converted unlike in PHP – could actually remove ? characters after iconv to get acceptable filenames)

However the challenge is that in Mbed OS i can not make iconv.h work at all.

  • Using ARMC 6.15 compiler it does not even compile, i get:

error: use of undeclared identifier ‘iconv_open’

  • Using ARM_GCC compiler (8 2018-q4-major) it compiles 100%, but then it does not link by telling:

undefined reference to `iconv_open’

I have read somewhere that GNU´s implementation prefixes iconv function names with lib, so iconv_open is called libiconv_open… however doing so i get:

‘libiconv_open’ was not declared in this scope

All this is a huge problem as the majority of the world uses more than just 27 alphabetical letters found in ASCII, so the potential user base needing non-ASCII characters is far larger than the base of english users. Transliteration with C/C++ would be an acceptable compromise, but that does not seem to work with Mbed OS.
I am out of ideas how to generate filenames that contain ASCII characters only…or maybe i just miss something in my code.

Can anyone help me how to do this in Mbed?

I think you’re on the right track but you will have to compile libiconv yourself as I don’t believe it’s included with the ARM toolchains. You might be able to use its build system to cross-compile for arm and then include the .a file into your project (e.g. like this guy was trying to do). Or maybe you can just drop the iconv source files into your project and then let Mbed build them.

@MultipleMonomials,

Thanks for the instructions. However i still can not make iconv.h work with Mbed.
This is what i do on my Windows 10 computer:

  1. Install Oracle’s VM VirtualBox
  2. Install a Ubuntu 20.04 as a virtual machine
  3. Install latest Mbed Studio on that
  4. Since ARMC6.15 seems to break printf() an (Un)BufferedSerial(), i use gcc-arm-none-eabi-10-2020-q4-major instead as compliler in the past few months or so (Mbed Studio is configured to work with that)
  5. Downloaded & installed libiconv as described here https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
  6. After doing so i can finally #include <iconv.h>, the code compiles 100%, but then in the linking phase i finally get these:

undefined reference to `iconv_open’

undefined reference to `iconv’

undefined reference to `iconv_close’

This really puzzles me as linting does not show any error, i can also jump to the definitions of these functions by hovering over them and tapping F12…

The same happens also under Windows 10 environment (after copying iconv.h file from Ubuntu to Win10).

I got stuck here maybe because Linux is something completely new to me or maybe libiconv has some quirks. Can you or anyone please give it a try on your side to see if it is just me or is it really broken in some way?

Thanks in advance!

P.S.: Also tried to incorporate the source files of libiconv to my project but also that way ends up without success for some reason.

yeah you can’t just download it, you actually need to compile it with ARM-GCC. The source files are what contain the actual implementations of iconv_open etc. that your program needs to use.