Convertir un Char* en WCHAR* ? [résolu]

Convertir un Char* en WCHAR* ? [résolu] - C++ - Programmation

Marsh Posté le 05-08-2004 à 18:25:24    

:hello:
 
Je n'arrive pas à trouver de fonction pour convertir un tableau de char en WCHAR... :/
 
Car la fonction Gdiplus::Graphics::DrawString me demande un WCHAR* comme string à afficher sur l'ecran... Et en castant c'est pas vraiment ca... :/ Je pense qu'il faut un truc qui convertit en unicode ou autre, mais je n'ai pas trouvé...
 
C'etait ma question du soir du boulot, bien fatigué... :ange: Si quelqu'un connait une fonction magique, je suis preneur ! :)
 
Mercÿte :jap:


Message édité par Jesus Army le 06-08-2004 à 13:00:55
Reply

Marsh Posté le 05-08-2004 à 18:25:24   

Reply

Marsh Posté le 06-08-2004 à 13:00:16    

Bon beh pour ceux que ca interesse, j'ai fini par trouver sur msdn, et c'est la fonction mbstowcs. Et sans les s ca ne se charge que d'un seul caractère. ;)
 
http://msdn.microsoft.com/library/ [...] stowcs.asp
 

Code :
  1. Converts a sequence of multibyte characters to a corresponding sequence of wide characters.
  2. size_t mbstowcs(
  3.    wchar_t *wcstr,
  4.    const char *mbstr,
  5.    size_t count
  6. );
  7. Parameters
  8. wcstr
  9.     The address of a sequence of wide characters.
  10. mbstr
  11.     The address of a sequence of null terminated multibyte characters.
  12. count
  13.     The maximum number of multibyte characters to convert.
  14. Return Value
  15. If mbstowcs successfully converts the source string, it returns the number of converted multibyte characters. If the wcstr argument is NULL, the function returns the required size of the destination string. If mbstowcs encounters an invalid multibyte character, it returns –1. If the return value is count, the wide-character string is not null-terminated.
  16.     Security Note   Ensure that wcstr and mbstr do not overlap, and that count correctly reflects the number of multibyte characters to convert.
  17. Remarks
  18. The mbstowcs function converts up to a maximum number of count multibyte characters pointed to by mbstr to a string of corresponding wide characters that are determined by the current locale. It stores the resulting wide-character string at the address represented by wcstr. The result is similar to a series of calls to mbtowc. If mbstowcs encounters the single-byte null character ('\0') either before or when count occurs, it converts the null character to a wide-character null character (L'\0') and stops. Thus the wide-character string at wcstr is null-terminated only if a null character is encountered during conversion. If the sequences pointed to by wcstr and mbstr overlap, the behavior is undefined.
  19. If the wcstr argument is NULL, mbstowcs returns the required size of the destination string.
  20. Requirements
  21. Routine  Required header  Compatibility
  22. mbstowcs  <stdlib.h>  ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
  23. For additional compatibility information, see Compatibility in the Introduction.
  24. Libraries
  25. All versions of the C run-time libraries.
  26. Example
  27. // crt_mbstowcs.c
  28. /* illustrates the behavior of the mbstowcs function
  29. */
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. int main( void )
  33. {
  34.     int i;
  35.     char    *pmbnull  = NULL;
  36.     char    *pmbhello = (char *)malloc( MB_CUR_MAX );
  37.     wchar_t *pwchello = L"Hi";
  38.     wchar_t *pwc      = (wchar_t *)malloc( sizeof( wchar_t ));
  39.     printf( "Convert to multibyte string:\n" );
  40.     i = wcstombs( pmbhello, pwchello, MB_CUR_MAX );
  41.     printf( "  Characters converted: %u\n", i );
  42.     printf( "  Hex value of first" );
  43.     printf( " multibyte character: %#.4x\n\n", pmbhello[0] );
  44.     printf( "Convert back to wide-character string:\n" );
  45.     i = mbstowcs( pwc, pmbhello, MB_CUR_MAX );
  46.     printf( "  Characters converted: %u\n", i );
  47.     printf( "  Hex value of first" );
  48.     printf( " wide character: %#.4x\n\n", pwc[0] );
  49. }
  50. Output
  51. Convert to multibyte string:
  52.   Characters converted: 1
  53.   Hex value of first multibyte character: 0x0048
  54. Convert back to wide-character string:
  55.   Characters converted: 1
  56.   Hex value of first wide character: 0x0048

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed