#Unicode string to character

21 messages · Page 1 of 1 (latest)

frigid cloak
#

I'm trying to convert a unicode 1 character string ♙ into a integer rapresentation of it but i can't seem to get it workign

misty turretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

frigid cloak
#

this is what im currently doing

#
int check_piece_color(string piece)
{
    int p = (int)piece[0];
    return (p > 9818) ? 0 : 0;
}
#

it always returns -300

#

i also tried using wchar_t instead of int

#

but it seems that piece[0] is nothing

normal narwhal
#

an std::string variable is simmilar to std::vector<char> , but with one extra '\0' at the end of the elements

#

if you chose utf-8 to encode the bytes into text, different symbols might be encoded with 1, 2, 3 or 4 bytes

#

what you are trying to do here is to read the first byte (because you wrongly assume that your symbol is encoded with 1 byte) and check its code

#

this is wrong

#

first, dump the raw contents of this string variable to see what it looks like:

for(char c : piece)
{
  
  cout<<(int)((unsigned char)c)<<"\n";
}
#

this will show you the code of each byte of your string, each byte being from 0 to 255

#

C++ standard library does not offer anything for utf-8 support, std::string is just a bunch of raw bytes

normal narwhal
#
#include <iostream>
#include <fstream>

int main()
{
    unsigned char whiteChessPawn_utf8[] = {0xe2, 0x99, 0x99};
    
    std::ofstream writer("result_utf8.txt", std::ios::binary);
    writer.write((const char*)&whiteChessPawn_utf8[0], 3);
}
#

and here is the raw binary contents, the same as the char array

#

@frigid cloak
do you understand now?

misty turretBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.