ratojakuf
ratojakuf

Reputation: 738

How to print bit representation of unicode character

I try to get binary utf-8 representation of unicode character like on image :

enter image description here

but this works only with <128 characters:

enter image description here

Here is my code:

#include <string>
#include <iostream>
#include <windows.h>

std::string contoutf8(std::wstring str)
{    
    int utf8_size = WideCharToMultiByte(CP_UTF8, 0, str.c_str(),
                    str.length(), nullptr, 0, nullptr, nullptr);
    std::string utf8_str(utf8_size, '\0');
    WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), 
                    &utf8_str[0], utf8_size, nullptr, nullptr);
    return utf8_str;
}

std::string contobin(std::string str)
{
    std::string result;
    for(int i=0; i<str.size(); ++i)
        for(int j=0; j < 8; ++j)
            result.append((1<<j) & str[i] ? "1" : "0");
    return result;
}

int main()
{
    std::wstring str  = L"\u20AC";
    std::string utf8 = contoutf8(str);
    std::string bin  = contobin(utf8);

    std::cout << bin;
}

I checked many combination of code(above is the last code) but non of them give binary representation in format 11... which signal that this is unicode character.

Upvotes: 0

Views: 480

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490788

Rather than convert to binary on your own, you might consider using an std::bitset, something like this:

#include <bitset>

std::string contoutf8(std::wstring str)
{    
    int utf8_size = WideCharToMultiByte(CP_UTF8, 0, str.c_str(),
                    str.length(), nullptr, 0, nullptr, nullptr);
    std::string utf8_str(utf8_size, '\0');
    WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), 
                    &utf8_str[0], utf8_size, nullptr, nullptr);
    return utf8_str;
}

int main()
{
    std::wstring str  = L"\u20AC";
    std::string utf8 = contoutf8(str);

    std::copy(utf8.begin(), utf8.end(), std::ostream_iterator<std::bitset<8>>(std::cout, "\t"));
}

Upvotes: 2

Richard Hodges
Richard Hodges

Reputation: 69942

Two problems:

  1. reverse bit pattern (binary reads left to right bit 7 to 0).

  2. sign extension

std::string contobin(std::string str)
{
    std::string result;
    for(int i=0; i<str.size(); ++i)
        for(int j=8; j--;) {
            result.append((1<<j) & uint8_t(str[i]) ? "1" : "0");
        }
    return result;
}

Upvotes: 1

Related Questions