libtconv-0.1 manual
tconv::mb_wc
unsigned int tconv::mb_wc(wchar_t *wcP, const char * *mbPP, unsigned int max = 0);
declared in tconv.h, include tconv.h

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, optional, defaults to 0.

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 tconv::error value is returned.

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

Example
// C++ source

#include <cstdio>   // freopen
#include <iostream> // cout wcout
#include "tconv.h"

using namespace std;

int main(){
  // UTF8 0xc485 = 'a' with a tail below
  static const char mbB[] = { 0xc4, 0x85, 'b',
    'c', 'd', 'e', 0 };
  const char *mbP = mbB;
  wchar_t wcB[16];
  int iRet, fDesc;
  tconv tc("pl_PL.UTF8", NULL);

  iRet = tc.mb_wc(wcB, & mbP);
  if(iRet == tconv::error)
    cout << iRet << " tconv::error at "
      << mbP << endl;
  else {
    cout << "The tconv::mb_wc returned "
      << iRet << endl;
    // Reopen the same stream to reset
    // its character width support.
    stdout = freopen(NULL,"w",stdout);
    wcout << L"Result is: "
      << wcB << endl;
    stdout = freopen(NULL,"w",stdout);
  }
  cout << "The End" << endl;

  return 0;
}

WARNING! According to C++ standards, the result of calling 'cout' and 'wcout' 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