Error character too large for enclosing character literal type

I have a problem with my code. I see these errors in the following lines of code. for (int i = 0; i < strlen(string); i++) { encrypt_string[current_element] = string[i];

I have a problem with my code. I see these errors in the following lines of code.

    for (int i = 0; i < strlen(string); i++) {
        encrypt_string[current_element] = string[i];
        current_element++;
        if (string[i] == 'а' || string[i] == 'е' || 
            string[i] == 'и' || string[i] == 'о' || 
            string[i] == 'у' || string[i] == 'я' || string[i] == 'Я' ||
            string[i] == 'ы' || string[i] == 'ё' || 
            string[i] == 'ю' || string[i] == 'я' || 
            string[i] == 'э' || string[i] == 'a' ||
            string[i] == 'e' || string[i] == 'i' || 
            string[i] == 'o' || string[i] == 'u' || 
            string[i] == 'y') {
            encrypt_string[current_element] = 'л';
            encrypt_string[current_element+1] = 'а';
            current_element += 2;
        }
    }

I tried to google on this topic, but the advice on the Internet does not help (Char_16T, Char_32T, ETC.). Perhaps this does not work with MacOS or Clang ++ compiler.

How I can do it on Mac?

0 / 0 / 0

Регистрация: 28.03.2015

Сообщений: 13

1

28.03.2015, 22:33. Показов 17016. Ответов 6


Здравствуйте, недавно перешел в среду Xcode (перешел с VisualStudio) и столкнулся с такой проблемой:
по непонятной мне причине я не могу присвоить переменной типа char русский символ. Например, для char c = ‘ы’; выдаёт ошибку «character too large for enclosing character literal type», хотя с латинскими символами все работает как надо.
Заменив char на wchar_t, вместо буквы Ы мне выдаёт букву K, тоже не ясно, говорили должно помочь.
Помогите справится с проблемой или дайте её решение.
Что вообще надо сделать чтобы работать с русскими символами полноценно, как в виндовсе ?

Миниатюры

Xcode: не получается присвоить переменной типа char русский символ
 

Xcode: не получается присвоить переменной типа char русский символ
 

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



korvin_

Эксперт Java

4016 / 2618 / 475

Регистрация: 28.04.2012

Сообщений: 8,422

28.03.2015, 23:45

2

Лучший ответ Сообщение было отмечено shurv как решение

Решение

Цитата
Сообщение от shurv
Посмотреть сообщение

Здравствуйте, недавно перешел в среду Xcode (перешел с VisualStudio) и столкнулся с такой проблемой:

Казалось бы, при чём тут линукс?

Цитата
Сообщение от shurv
Посмотреть сообщение

по непонятной мне причине я не могу присвоить переменной типа char русский символ. Например, для char c = ‘ы’; выдаёт ошибку «character too large for enclosing character literal type», хотя с латинскими символами все работает как надо.

Дык русские символы в UTF-8 занимают 2 байта, а char вмещает только 1 байт.

Цитата
Сообщение от shurv
Посмотреть сообщение

Помогите справится с проблемой или дайте её решение.
Что вообще надо сделать чтобы работать с русскими символами полноценно, как в виндовсе ?

http://stackoverflow.com/quest… 8-encoding

Добавлено через 21 минуту

C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
 
int
main(void)
{
    wchar_t c;
 
    setlocale(LC_ALL, "ru_RU.utf8");
    c = L'ф';
    wprintf(L"%lcn", c);
 
    return 0;
}



1



550 / 383 / 125

Регистрация: 02.10.2008

Сообщений: 1,553

Записей в блоге: 1

28.03.2015, 23:47

3

У вас в коде setlocale(LC_ALL,»rus»); а разве не setlocale(LC_ALL,»ru_RU»)?
1. Дайте выхлоп в консоли команды locale
2. А если пользователь вашей проги будет использовать не UTF-8, а KOI-8?



0



Эксперт Java

4016 / 2618 / 475

Регистрация: 28.04.2012

Сообщений: 8,422

28.03.2015, 23:55

4

Цитата
Сообщение от drfaust
Посмотреть сообщение

2. А если пользователь вашей проги будет использовать не UTF-8, а KOI-8?

Это будет довольно необычно. В OSX используется только UTF-8, насколько я знаю. Если только ТС не пишет текстовый редактор, который должен уметь работать с разными кодировками, что вряд ли.



0



550 / 383 / 125

Регистрация: 02.10.2008

Сообщений: 1,553

Записей в блоге: 1

29.03.2015, 00:13

5

Цитата
Сообщение от korvin_
Посмотреть сообщение

Это будет довольно необычно.

многие консольные bsd висят по прежнему на KOI. Писать не англоязычное и кроссплатформенное — то ещё веселье…



0



korvin_

29.03.2015, 00:30

Не по теме:

Цитата
Сообщение от drfaust
Посмотреть сообщение

многие консольные bsd

Что-то мне подсказывает, что ТС с ними никогда не столкнётся.



0



0 / 0 / 0

Регистрация: 28.03.2015

Сообщений: 13

29.03.2015, 01:06

 [ТС]

7

я пишу программку которая переводит текстовый файл в какой-либо кодировке на русский язык.Спасибо вам за помощь.

Добавлено через 18 минут
korvin, очень помогли, спасибо!
Единстивинно в строке 10 забыли тире поставить, вот так все замечательно: setlocale(LC_ALL, «ru_RU.utf-8»);



0



New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.

Already on GitHub?
Sign in
to your account


Closed

patrickt opened this issue

Jan 7, 2020

· 4 comments

Comments

@patrickt

Running tree-sitter test produces the following error when compiling scanner.cc:

emcc command failed - src/scanner.cc:88:32: error: character too large for enclosing character literal type
           lexer->lookahead == 'uFEFF' ||
                               ^
src/scanner.cc:89:32: error: character too large for enclosing character literal type
           lexer->lookahead == 'u2060' ||
                               ^
src/scanner.cc:90:32: error: character too large for enclosing character literal type
           lexer->lookahead == 'u200B';

@simonrepp

I’ve encountered this myself — if I remember correctly:

  • You can comment out the affected lines for the time being if you just want to work on stuff (that’s what I did so far), these are more exotic whitespace cases which of course matter, but are irrelevant for the overall implementation and general testing.
  • It only occurrs in certain compilation scenarios (when building for the wasm target?)
  • I don’t have a solution yet!
  • If you find one I’d be much obliged :)

In any case, thanks for the report! Glad about your interest in this, let me know if you have more questions and/or issues!

@patrickt

I fixed it by removing the character syntax:

  inline bool is_horizontal_whitespace(TSLexer *lexer) {
    return lexer->lookahead == ' ' ||
           lexer->lookahead == 't' ||
           lexer->lookahead == 0xFEFF ||
           lexer->lookahead == 0x2060 ||
           lexer->lookahead == 0x200B;
  }

@maxbrunsfeld

Yeah, I don’t think 'uFEFF' is a valid C/C++ expression. Single quoted character literals are of type char, which is almost always an 8-bit value. The largest numerical char value is 0xFF.

In more recent versions of C++, you can use a U prefix (e.g. U'uFEFF'). I think the normal way to write numbers like this is to just use integer syntax, like @patrickt said ☝️ .

@simonrepp

Very cool, thanks patrick for the fix and max for the additional insight, appreciate it!

Commited in c059967

Locales

Hello all,

Recently, I have been trying to write a small program to practice C++. Since I’m from a French speaking region, I would like my program to be able to adjust to different locales.

To test my understanding of locale basics, I have written this code to test whether a char «a» is a letter or not (in the locale):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<locale>

using namespace std;

bool letter(char a); // Test if "a" is a letter in the locale's language.

int main(void)
{
	cout.imbue(locale("fr_CA.utf8")); //French canadian locale.
	cout << letter('è'); // A letter in French...

	return 0;
}

bool letter(char a)
{
	locale userLocale("fr_CA.utf8"); // Construct locale object using the French canadian locale.

	bool isAlpha = use_facet< ctype<char> >(userLocale).is(ctype_base::alpha, a);

	return isAlpha;
}

The program compiles fine, but returns «0» (thus, «è» is not a letter for the program).

What am I doing wrong here? It seems like the program is ignoring the locale.

FYI, I am programming on Ubuntu using GCC. The fr_CA.UTF-8 locale is installed on my computer but is not my default locale, which is en_CA.UTF-8.

Thank you!

Last edited on

What encoding do you use? I believe you need a single-byte encoding for both your file and locale to work properly.

MiiNiPaa,

Thanks for your reply. My .cpp file is encoded in UTF-8, which is not single-byte, according to this source: https://en.wikipedia.org/wiki/SBCS.

When I try using the two single-byte encodings in Eclipse, the letter ‘è’ is transformed as a weird sign for missing characters.

I’m not exactly sure I understand what you mean…

Try to use wide characters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<locale>

using namespace std;

bool letter(wchar_t a); // Test if "a" is a letter in the locale's language.

int main(void)
{
	wcout.imbue(locale("fr_CA.utf8")); //French canadian locale.
	wcout << letter(L'è'); // A letter in French...

	return 0;
}

bool letter(wchar_t a)
{
	locale userLocale("fr_CA.utf8"); // Construct locale object using the French canadian locale.

	bool isAlpha = use_facet< ctype<wchar_t> >(userLocale).is(ctype_base::alpha, a);

	return isAlpha;
}

coder777,

Thanks for the comment. From what your telling me, it seems like I am not using the right tool to reach my goal… Would you have any suggestion as to what I could use to achieve this?

I could I test for French, Russian or Japanese characters (depending on the user’s locale)?

@MiiNiPaa: thanks for your suggestion, but sadly using wide chars is not working…

Last edited on

coder777,

Ok. The links you provided are really interesting, I will definitely look into that. I thought there was a straightforward way to do this…

I will keep your last suggestion in mind.

You don’t need boost to do what you’re trying to do, linux actually supports Unicode in C++ as the language intended (Windows is the outlier here)

The problem with the first program is that the character literal (something enclosed by single quotes) must be a single byte, and è is not a single byte in UTF-8, which is what linux saves your source code file in. assuming normal configuration.

In fact, clang gives an error here:

1
2
test.cc:11:17: error: character too large for enclosing character literal type
        cout << letter('è'); // A letter in French... 

and gcc gives a somewhat worse-worded warning:

1
2
3
test.cc:11:17: warning: multi-character character constant [-Wmultichar]
  cout << letter('è'); // A letter in French...
                 ^

MiiNiPaa is correct, you have to use a wide character or, alternatively, a string

Also, don’t use that hideous use_facet

The following works for me:

using wchar_t:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <locale>
#include <clocale>

bool letter(wchar_t a)
{
    return std::isalpha(a, std::locale("fr_CA.utf8"));
}

int main()
{
    // either this:
//    std::setlocale(LC_ALL, "fr_CA.utf8");
    // or this:
    std::ios::sync_with_stdio(false);

    std::wcout.imbue(std::locale("fr_CA.utf8"));
    std::wcout << "letter(" << L'è' << ") returns "
              << std::boolalpha << letter(L'è') << 'n';
}

using UTF-8 strings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <locale>
#include <clocale>
#include <algorithm>
#include <codecvt>

// test if every character in a UTF-8 string is a letter
bool letters(std::string str)
{
    // this part of C++11 didn't come to gcc until version 5.0
    // (Microsoft and Clang had it in 2010 though)
    std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
    std::wstring wstr = conv.from_bytes(str.data());
    return std::all_of(wstr.begin(), wstr.end(),
             [](wchar_t c){return std::isalpha(c, std::locale("fr_CA.utf8"));});
}

int main()
{
    // either this:
//    std::setlocale(LC_ALL, "fr_CA.utf8");
    // or this:
    std::ios::sync_with_stdio(false);

    std::cout.imbue(std::locale("fr_CA.utf8"));
    std::cout << "letters(" << "è" << ") returns "
              << std::boolalpha << letters("è") << 'n';
}

By the way, you’re not using anything french-canadian in here, any UTF-8 locale will work. Here’s how these programs run on coliru with American English UTF-8:

wide char:
http://coliru.stacked-crooked.com/a/0bce63e9beb5ce05
strings:
http://coliru.stacked-crooked.com/a/2d88de701da9b81f

Last edited on

Topic archived. No new replies allowed.

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Error cleared paper out zebra
  • Error chain 1 can get none temp info or temp value abnormal power it off
  • Error cleared head open zebra zm400
  • Error clear vue comment directive
  • Error certificate manager tool re register failed 1

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии