Binary to UTF-8 in C
<p>I am working on an application in C where I need to show Unicode UTF-8 characters. I am getting the values as a binary byte stream as 11010000 10100100 as character array which is the Unicode character "Ф".</p>

<p>I want to store and display the character. I tried to convert the binary to a hexadecimal character array. But printing with</p>

<pre><code>void binaryToHex(char *bData) {
    char hexaDecimal[MAX];
    int temp;
    long int i = 0, j = 0;
    while (bData[i]) {
        bData[i] = bData[i] - 48;

        ++i;
    }

    --i;
    while (i - 2 &gt;= 0) {
        temp = bData[i - 3] * 8 + bData[i - 2] * 4 + bData[i - 1] * 2 + bData[i];
        if (temp &gt; 9)
            hexaDecimal[j++] = temp + 55;
        else
            hexaDecimal[j++] = temp + 48;
        i = i - 4;
    }

    if (i == 1)
        hexaDecimal[j] = bData[i - 1] * 2 + bData[i] + 48;
    else if (i == 0)
        hexaDecimal[j] = bData[i] + 48;
    else
        --j;

    printf("Equivalent hexadecimal value: ");
    char hexVal[MAX];
    // size_t len = j+1;
    int k = 0;;
    while (j &gt;= 0) {
        char *ch = (char*)hexaDecimal[j--];
        if (j % 2 == 0) {
            hexVal[k] = '\\';
            k++;
            hexVal[k] = 'x';
            k++;
        }
        printf("\nkk++Length %d ...J= %d.. ", k, j);
        hexVal[k] = ch;
        k++;
        printf("%c", ch);
    }
    printf("KKKK+=== %d", k);
    hexVal[k] = NULL;

    // printf("\nkk++Length %d",strlen(hexVal));
    printf("\nMM+-+MM %s===\n ..&gt;&gt;&gt;&gt;", hexVal);
}
</code></pre>

<p>Only showing the value as \xD0\xA4. I did string manipulation for that.
But when writing in the way</p>

<pre><code> char s[]= "\xD0\xA4";
         OR
 char *s= "\xD0\xA4";
 printf("\n %s",s);
</code></pre>

<p>producing the desired result that is printing the character "Ф". How can I get the correct string dynamically? Is there any library for this in C?</p>

<p>The code is from <a href="http://www.cquestions.com/2011/07/binary-to-hexadecimal-conversion-in.html" rel="nofollow noreferrer">http://www.cquestions.com/2011/07/binary-to-hexadecimal-conversion-in.html</a>.</p>

<p>Is there a way to print it from binary directly or from a HEX value. Or is there an alternative for that?</p>