Read binary from file; see unicode character in CLI or at least in MessageBoxW.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SwissProgrammer
    New Member
    • Jun 2020
    • 220

    #1

    Read binary from file; see unicode character in CLI or at least in MessageBoxW.

    I have been reading Stroustrup's C++11 fourth edition and studying it. But, I can't seem to make the following work.


    I have a binary, utf-8 file with the proper BOM.

    In that file is the letter "e" which I want to read as a const wchar_t* or at least as a wchar_t .

    This is simplistic (ASCII characters) for now. I later want to be able to view the unicode symbols.

    Windows 32 bit / C or C++ / CodeBlocks 17.12 / gcc 5.1

    The BOM is an unsigned char BOM01[3]{ 0xef, 0xbb, 0xbf }
    The file contents = "hello - こんにちは - abc"



    But, if you just tell me how to convert from binary to const wchar_t* that would answer the following question.


    Code:
                std::FILE *fp;
                unsigned char data [128] ;
    
                fp = std::fopen(wnd_title, "r");    // Open the file.
    
    /// The following three are the byte order mark   "BOM"
                    fread(&data[0], 1, 1, fp);
                                        //  Decimal or unsigned char =          [239]
                                        //  Hex =                               [ef]
                                        //  bitset<8>(data[0]).to_string() =    [11101111]   OK   this works
    
                    fread(&data[1], 1, 1, fp);
                                        //  Decimal or unsigned char =          [187]
                                        //  Hex =                               [bb]
                                        //  bitset<8>(data[1]).to_string() =    [10111011]   OK   this works
    
                    fread(&data[2], 1, 1, fp);
                                        //  Decimal or unsigned char =          [191]
                                        //  Hex =                               [bf]
                                        //  bitset<8>(data[2]).to_string() =    [10111111]   OK   this works
    
    
                    fread(&data[3], 1, 1, fp);
                                        //  Decimal or unsigned char =          [104]
                                        //  Hex =                               [68]    Hex of Character = "h"
                                        //  bitset<8>(data[3]).to_string() =    [01101000]   OK   this works
    
                    fread(&data[4], 1, 1, fp);
                                        //  Decimal or unsigned char =          [101]
                                        //  Hex =                               [65]    Hex of Character = "e"
                                        //  bitset<8>(data[4]).to_string() =    [01100101]   OK   this works
    
    //	etcetera
    
    
                fclose(fp);     // Close the file.
    
    
    
    // [B]I want to get the const wchar_t*[/B] .
    
    // In different attempts I get the following.
    
    [CODE]
    std::cout << "Letter in item number 4  = [" <<  char(data[4]) << "]   ASCII letter ''e''\n\n";   // Not what I wanted.
    
    std::cout << "Now trying to get the const wchar_t* (or Unicode character) of this.\n\n";
    
    std::cout << "Decimal version of item 4      = [" <<  wchar_t(data[4]) << "]        Decimal [101]\n";   // Not what I wanted.
    
    std::cout << "Hexidecimal version of item 4  = [" <<  (wchar_t*)(wchar_t(data[4])) << "]       Hex [ox65]\n";   // Not what I wanted.
    
    std::cout << "Hexidecimal version of item 4  = [" <<  (const wchar_t*)((wchar_t*)(wchar_t(data[4]))) << "]       Hex [ox65]\n";   // Not what I wanted.
    
    std::cout << "Hexidecimal version of item 4  = [" <<  LPCWSTR(data[4]) << "]       Hex [ox65]\n";   // Not what I wanted.
    
    std::cout << "bitset<8>(data[4]).to_string() = [" << bitset<8>(data[4]).to_string() << "]   binary\n";   // Not what I wanted.
    
    std::cout << "bitset<8>(data[4]).to_string() = [" << (const wchar_t*)((wchar_t*)(wchar_t(bitset<8>(data[4]))))<< "]   ???\n\n";
    //  error: invalid cast from type 'std::bitset<8u>' to type 'wchar_t'

    // Since none of those worked, how do I convert any of them to a const wchar_t* ? I want to read the binary of a file one byte at a time and show in a MessageBoxW the const wchar_t* of that.

    // I KNOW THIS IS CURRENTLY IN ASCII. I know that. Later I want to be able to get the utf8 or unicode symbol of the unicode characters beyond ASCII. But for now I am attempting to do this in ASCII for simplicity.

    // Here are some of my attempts for that.

    const wchar_t* SWc001;
    SWc001 = (const wchar_t*)((wcha r_t*)(wchar_t(d ata[4])));

    MessageBoxW ( nullptr, SWc001, L"SWc001 Wide_Character_ String0321", MB_OK ) ; // blank


    MessageBoxW ( nullptr, LPCWSTR(data[4]), L"LPCWSTR(da ta[4]) Wide_Character_ String0321", MB_OK ) ; // blank

    [/CODE]



    Later in the binary file I have the following unicode symbol "" which I will want to see via wchar or const wchar_t* once I understand how to get the wchar or const wchar_t* of some ASCII symbols.

    Code:
                    //   Beginning of UNICODE binary   11100011   for symbol   こ
                    fread(&data[11], 1, 1, fp);
                                        //  Decimal or unsigned char =          [227]
                                        //  Hex =                               [e3]    Hex to Character = "Not ASCII"
                                        //  bitset<8>(data[11]).to_string() =   [11100011]
                    fread(&data[12], 1, 1, fp);
                                        //  Decimal or unsigned char =          [129]
                                        //  Hex =                               [81]    Hex to Character = "Not ASCII"
                                        //  bitset<8>(data[12]).to_string() =   [10000001]
                    fread(&data[13], 1, 1, fp);
                                        //  Decimal or unsigned char =          [147]
                                        //  Hex =                               [93]    Hex to Character = "Not ASCII"
                                        //  bitset<8>(data[13]).to_string() =   [10010011]

    Thank you.
Working...