CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,368

    Re: mbtowc() - How do I convert it and how do I output the result to a wchar_t variab

    Quote Originally Posted by Arianax View Post
    Weird... It is returning as value 224 (e0 in Hex).

    That translates to the character which is certainly not ...
    You may want to try the code example from https://en.cppreference.com/w/cpp/st...ltibyte/mbtowc
    Victor Nijegorodov

  2. #17
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,800

    Re: mbtowc() - How do I convert it and how do I output the result to a wchar_t variab

    mbtowc() uses the specified number of bytes to convert.

    Have you changed the size of output array?

    What is the hex value of tester?
    What are the 3 hex values in output?

    What wchar_t value are you expecting?

    tester is a 32-bit type, receiver is 16-bits. So obviously all values allowed in tester won't convert exactly to receiver.
    Last edited by 2kaud; November 4th, 2023 at 05:15 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #18
    Join Date
    May 2019
    Posts
    53

    Re: mbtowc() - How do I convert it and how do I output the result to a wchar_t variab

    Referring BACK to my post where I simplified the code for you as an example (So you don't need to scroll around too much)...:

    Here is the code I'm working from now:
    Code:
    char32_t tester = U'ฐ';
    
                        int returned = NULL;
                        std::mbstate_t state {};
                        char output[MB_LEN_MAX] {};
    
                        returned = c32rtomb(output, tester, &state);
    
                        printf("[c32rtomb()]: Character Returned is: %s \n\n", output);
    
    
                        wchar_t receiver;
    
                        mbtowc(NULL, NULL, 0); //Reset the function
                        
                        auto resultant = mbtowc(&receiver, output, MB_LEN_MAX);
    If I do..:

    Code:
    wchar_t bufferGo[MB_CUR_MAX]; //Using a pointer to wchar_t array here instead of just a standard wchar_t variable...
    
    auto resultant = mbtowc(bufferGo, output, length);
    
    
    printf("Decimal Value Returned is: %u", output);
    printf("Hex Value Returned is: %x", output);
    
    printf("Decimal Value Returned is: %u \n", bufferGo);
    printf("Hex Value Returned is: %x", bufferGo);
    I get the values:

    output is equal to 6683936
    output hex is equal to 65fd20

    bufferGo is equal to 6683920
    bufferGo hex is equal to 65fd10

    output is the result of c32rtomb().
    bufferGo is the result of mbtowc().

    The numbers are almost the same, but, of course, I don't get anything printed to the console when I wprintf() bufferGo...

    What am I doing wrong?

    EDIT:
    Something funny is going on...

    If I do:
    Code:
    printf("bufferGo box 0 contains: %x", bufferGo[0]);
    printf("bufferGo box 1 contains: %x", bufferGo[1]);
    printf("bufferGo box 2 contains: %x", bufferGo[2]);
    I get the following:

    bufferGo box 0 contains: e0
    bufferGo box 1 contains: 6dcd
    bufferGo box 2 contains: 7ff9

    BUT when I do:

    Code:
    printf("Number of elements in 'bufferGo' is: %u \n", (sizeof(bufferGo) / sizeof(wchar_t)));
    I get the following:
    Number of elements in 'bufferGo' is: 1

    ALSO, If I do:

    Code:
    wchar_t bufferGo[returned]; // instead of wchar_t bufferGo[MB_CUR_MAX);
    Then I can output each element in bufferGo by using a for(int i=0; i < sizeof(bufferGo) / sizeof(wchar_t); i++){} loop...

    BUT If I do the same thing using MB_CUR_MAX or even the number 3 for the definition of bufferGo then the loop only outputs ONE element, not THREE.

    What is going on?



    EDIT: Sorry @2kaud I'll provide the hex codes and info you requested if this post doesnt give you a good lead.
    Last edited by Arianax; November 5th, 2023 at 11:30 AM.

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,800

    Re: mbtowc() - How do I convert it and how do I output the result to a wchar_t variab

    The value of MB_CUR_MAX is the maximum number of bytes in a multibyte character for the current locale
    We already know that your locale isn't set. So you shouldn't use that constant in your code as it has a value of 1. That's why using this only outputs 1 value and not three.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured