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!?

Recommended Answers

All 6 Replies

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.

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.