The following code is available:

#include <iostream> #include "big_int.h" using namespace std; int main() { char *num = "124141414141444134"; big_int a; cout << (int)num[0] << endl; a = num; cout << a; } 

The problem is that the output (first cout ) is not the 0th character, i.e. 1 , and the number 49 ...

  • one
    Well, you yourself asked to print an int , that is, not the symbol '1' but the numeric value of the constant '1' . And this is 49 on your platform. Remove the cast to (int) and the character '1' will be output. - AnT

1 answer 1

The number 49 corresponds to the character code '1' in the ASCII table.

That is, inside the car, the symbol '1' is stored in the form of this code, And, for example, in the EBCDIC table, the symbol '1' corresponds to the code 241 .

If you want to output exactly 1, then you should either write just

 cout << num[0] << endl; 

or

 cout << num[0] - '0' << endl; 

Keep in mind that string literals have the type of constant character arrays. Therefore, you must add the const qualifier in the declaration.

 const char *num = "124141414141444134"; 
  • Wow, thank you very much, but there is no low-level or bit operation for this business? For the sake of interest, I ask, suddenly come in handy. - kot_mapku3
  • @ kot_mapku3 What do you mean? - Vlad from Moscow
  • Well, he himself converts the character '0' to int and then performs the subtraction operation, although the '0' in ASC2 is code 48 - kot_mapku3
  • @ kot_mapku3 When the binary minus operator is used, the operands are cast to the Int type, if they are ranked lower than the int type. Therefore, it is a standard technique to translate any numeric character into an integer value, which it means by writing c - '0' - Vlad from Moscow
  • So binary ... Thank you) - kot_mapku3