Hi!

I'm trying to read wstring from file.

This is an file content example:
"83
118
269
...
"
The number 83 represent "S", 118 represent "v", 269 represent "č", and so on..

So, I tried to read the numbers into int. But the problem is, that I don't know how to convert int to wstring, to get actual letter(convert: int value of 83 to letter "S").

tnx

Dani AI

Generated

Good work getting a cast working — and nice troubleshooting from and about single-byte versus wide characters. Two separate facts make this question trickier than a plain cast: (1) what the integers in the file actually represent (Unicode code points vs. bytes in a legacy code page), and (2) what encoding the program or terminal expects for output. If those numbers are Unicode code points (269 == U+010D, the c-caron), a portable approach is to turn each code point into UTF-8 and emit a std::string. That avoids platform differences in sizeof(wchar_t) and console code pages.

A simple, robust encoder for a single code point is shown below. It handles the full Unicode range up to U+10FFFF and produces UTF-8 bytes that can be printed on modern terminals or written to UTF-8 files.

#include <cstdint>
#include <string>
#include <vector>
#include <iostream>

static std::string to_utf8(uint32_t cp) {
    std::string s;
    if (cp <= 0x7F) {
        s.push_back(static_cast<char>(cp));
    } else if (cp <= 0x7FF) {
        s.push_back(static_cast<char>(0xC0 | (cp >> 6)));
        s.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
    } else if (cp <= 0xFFFF) {
        s.push_back(static_cast<char>(0xE0 | (cp >> 12)));
        s.push_back(static_cast<char>(0x80 | ((cp >> 6) & 0x3F)));
        s.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
    } else if (cp <= 0x10FFFF) {
        s.push_back(static_cast<char>(0xF0 | (cp >> 18)));
        s.push_back(static_cast<char>(0x80 | ((cp >> 12) & 0x3F)));
        s.push_back(static_cast<char>(0x80 | ((cp >> 6) & 0x3F)));
        s.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
    }
    return s;
}

// usage:
// std::vector<uint32_t> codes = {83, 118, 269};
// build a string by concatenating to_utf8(cp) for each cp and print it.

Notes and caveats: validate each read integer (0 <= cp <= 0x10FFFF and not a lone surrogate 0xD800–0xDFFF). If the target API expects UTF-16 (Windows APIs or some GUI toolkits), convert code points to char16_t and emit surrogate pairs for cp > 0xFFFF (high = 0xD800 + ((cp-0x10000)>>10), low = 0xDC00 + ((cp-0x10000)&0x3FF)). For full production-grade handling (locale-aware I/O, normalization, collation), prefer a library such as ICU or Boost.Locale. Finally, ensure the terminal or consumer actually supports the encoding chosen (UTF-8) and that font and console settings allow the glyphs to render.

Recommended Answers

All 6 Replies

If you perform the command
char c=83
you will get that c is equal to 'S'

edit: sorry didnt see the wstring :O only works on char

You will find the information in any ascii chart, such as this one. Since C/C++ and other languages represent letters are numbers there is no conversion from one to the other except for typecasting. If you want to display the letter 'S' all you do is typecast it to char, for example

int letter = 83;
cout << (char)letter;

Ancient Dragon - is there a difference between the wstring and the char?
I saw somewhere that by using the boost library you can transform the number into its wstring
and if im not mistaken its more characters than the ascii table can provide ( like his 269 one )

thanks

yes I also need the c-caron, s-caron, ... letters. for example, number 269 will not work on char

you could copy the ints into wchar_t variables or std::wstring. What characters are displayed on the screen will depend on what language(s) are installed in the computer. For MS-Windows also look into setlocale()

Ancient Dragon - is there a difference between the wstring and the char?

Yes, wchar_t is 2 or more bytes per character while char is only 1 byte per character. std::wstring is an array of wchar_t characters. std::wstring and std::string are not easily convertable from one to the other. Furthermore, the size of wchar_t is not constant from one os to another. On MS-Windows sizeof(wchar_t) is 2 while on *nix it is 4. I read a couple years ago that UNICODE standards committee is considering expanding it to 8 so that it can hold graphics characters used in some languages.

It works! tnx

solution:

wstring letters =L"";
int number=269;
letters += (wchar_t)number;
wcout<<letters;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.