libtconv-0.1 manual
tconv_mbwc
unsigned int tconv_mbwc(tconv *tcP, wchar_t *wcP, const char * *mbPP, unsigned int max);
declared in tconv.h, include tconv.h

tcP handle of an existing tconv instance,
wcP location of a buffer where to put the wide-character string,
mbPP location of a handle pointing at the source, multi-byte string,
max number of characters to convert.

This function is actually the first step to be taken in character encoding conversion process. It translates multi-byte strings into wide-character strings. The wcP pointed buffer needs to be big enough to store number of characters indicated by max. If max is set to 0 (zero), the function translates multi-byte sequences, until it detects the NULL-terminating byte, which is also written into the output buffer.

If the conversion process succeeds, the returned value indicates the number of converted characters, without the NULL-terminating byte (if any). When it fails, the tconvError value is returned.

To perform the conversion, internal input character encoding is used.

Example
/* C source */

#include <stdio.h>
#include "tconv.h"

void main(){
  /* 0xc485: UTF8 letter 'a' with a tail below */
  const char mbB[]= { 0xc4,0x85,'b','c','d','e',0 };
  const char *mbP = mbB;
  tconv *tcP;
  wchar_t wcB[80];
  int iRet;
  int fDesc;

  tcP = tconv_init("pl_PL.utf8", NULL);
  iRet = tconv_mbwc(tcP, wcB, & mbP, 0);
  if(iRet == tconvError)
    printf("%i tconvError at %s\n", iRet, mbP);
  else {
    printf("The tconv_mbwc returned %i\n", iRet);
    /* Reopening the same file will reset the
     supported characters width. */
    fDesc = dup(fileno(stdout));
    close(stdout);
    stdout = fdopen(fDesc,"w");
    wprintf(L"Result is: %ls\n", wcB);
    fDesc = dup(fileno(stdout));
    close(stdout);
    stdout = fdopen(fDesc,"w");
  }

  printf("The End\n");
  tconv_free(tcP); 
}

WARNING! According to C standards, the result of calling printf() and wprintf() one after another, on the same stream (stdout) is known as "undefined behaviour". The stream should be closed and opened again to reset its character width support.

Copyright 2015 TinyIT All rights reserved
Last modified 2015-06-04