The ascii code of 'á' is 160,
but
int('á'); gives -31
Yes I know, I can use the formula 129+(int)fabs(int(mychar));
but are there any other solution?
i tried to use
unsigned int('á') but its also ü31 which is a puzzle for me!?
The ascii code of 'á' is 160,
but
int('á'); gives -31
Yes I know, I can use the formula 129+(int)fabs(int(mychar));
but are there any other solution?
i tried to use
unsigned int('á') but its also ü31 which is a puzzle for me!?
your -31 is a sign issue, not a math one. Plain char can be signed or unsigned depending on the compiler and platform, so the byte 0xE1 gets promoted to int as -31 when char is signed. Also, a-acute is not ASCII. In Windows-1252 it is byte 0xE1 (decimal 225); in DOS CP437 it is 0xA0 (decimal 160); and as Unicode it is U+00E1. If your editor saves UTF-8, you will actually see two bytes for that character: 0xC3 0xA1. Mixing these assumptions is what produces the surprises you are seeing. (doc.bccnsoft.com)
Practical fixes:
unsigned code = static_cast<unsigned>(static_cast<unsigned char>(c)); That guarantees 0..255.std::array<std::size_t,256> or std::map<unsigned char, std::size_t>.read() or get() so you do not trigger locale rules or skip whitespace.Example you can drop in to get stable byte values:
std::array<std::size_t,256> freq{};
std::ifstream in(path, std::ios::binary);
unsigned char b;
while (in.read(reinterpret_cast<char*>(&b), 1)) ++freq[b];
// print one example byte value safely
std::cout << static_cast<unsigned>(b) << "\n"; Portability note: hard-coding 160 vs 225 will break across machines because it depends on the file’s encoding and the console/code page. Pick an encoding and stick to it: if your input is UTF-8, decode to Unicode code points before case-folding or classification; if it is Windows-1252, treat bytes as that code page (or convert to UTF-8/UTF-16 up front). ’s advice to go through unsigned char is the right first step; also double-check your Post #6 code, where the stream is closed inside the read loop.
Jump to Post— Narue 5,707Are you sure that the literal character is using ASCII?
#include <iostream> #include <string> int main() { char a = 'á'; char b = char(160); std::cout<< a <<" == "<< std::char_traits<char>().to_int_type(a) <<'\n'; std::cout<< b <<" == "<< std::char_traits<char>().to_int_type(b) <<'\n'; }My guess is that your text editor …
Jump to Post— Narue 5,707>I dont think so.
Alright then. If you're absolutely sure you're using extended ASCII, the problem is probably sign extension when converting to int. Tryint((unsigned char)c).
Are you sure that the literal character is using ASCII?
#include <iostream>
#include <string>
int main()
{
char a = 'á';
char b = char(160);
std::cout<< a <<" == "<< std::char_traits<char>().to_int_type(a) <<'\n';
std::cout<< b <<" == "<< std::char_traits<char>().to_int_type(b) <<'\n';
} My guess is that your text editor is using Unicode for printed characters (where the value of 'á' is 0x00E1), and the problem is your assumption of extended ASCII between the text editor and the C++ runtime.
I dont think so.
It was just an example, but read data (chars) from a text file
(I tried ANSI and MS DOS format text files)
and I 129+abs(int(mychar)) is also wrong
because int('é') =-23 for me !?
>I dont think so.
Alright then. If you're absolutely sure you're using extended ASCII, the problem is probably sign extension when converting to int. Try int((unsigned char)c) .
If I solve somehow the problem, for example try to use negative codes,
and compile my program to an exe on my computer, the exe will work on other omputers fine?
int((unsigned char)c) IS STILL NEGATIVE!!!???
my program:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <map>
using namespace std;
typedef map<char,unsigned long int> gyakorisagok;
void print_help()
{
cout << "Example: betugyakorisag.exe szoveg1.txt szoveg2.txt szoveg3.txt > betugyakorisag.txt" << endl;
}
int main (int argc, char* argv[])
{
if (argc == 1)
{
print_help();
return 0;
}
gyakorisagok tablazat;
char c;
int ascii;
for (int i = 1; i < argc; i++)
{
ifstream input;
input.open(argv[i]);
while (true)
{
input >> c;
if (input.eof()) break;
ascii = int(c);
if ((ascii >= 65) && (ascii <= 90)) ascii += 32;
tablazat[char(ascii)] += 1;
input.close();
}
unsigned long int summa = 0;
gyakorisagok::iterator it;
for (it = tablazat.begin(); it != tablazat.end(); ++it)
{
summa += it->second;
}
for (it = tablazat.begin(); it != tablazat.end(); ++it)
{
cout << setiosflags(ios::fixed) << setprecision(2) << "\"" << it->first << "\" " << it->second*100./summa << "% (" << it->second << ") " << int((unsigned char)it->first) << endl;
}
return 0;
} I don't know what to tell you. If I could reproduce your problem, I'd be able to help more, but as it is you're on your own. Sorry.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.