2

This is the code I wrote to convert integer to string.

#include <iostream>
using namespace std;
int main()
{
    string s;
    int b=5;
    s.push_back((char)b);
    cout<<s<<endl;
}

I expected the output to be 5 but it is giving me blank space.

I know there is another way of doing it using stringstream but I want to know what is wrong in this method?

Gibreel Abdullah
  • 335
  • 1
  • 3
  • 12
  • 4
    http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c – selyunin Jun 15 '16 at 14:54
  • It's probably not a space. Check out an ASCII table: https://www.bing.com/search?q=ascii+table –  Jun 15 '16 at 15:05
  • I doubt very much that 'ENQ' is redundant.. these control characters are used in communication to control the flow of information and acknowledgement of messages between devices, although what current protocols (http, ftp, etc. ) use I do not know. As far as I remember, and it is 40+ years ago, ENQ was used to bid for a communications channel, STX indicated the start of the message, ETX the end of the message, ACK that it had been received correctly, NAK that it had not been received correctly, SYN to sychronise etc. ASCII '0' is thus Hex 30, a space is Hex 20, and letters start not unreasonab – John Walker Apr 14 '18 at 17:47
  • For conversion like these, I have a bookmark https://www.converttypes.com/ – Firzok Nadeem Jun 24 '20 at 19:31

6 Answers6

6

Character code for numbers are not equal to the integer the character represents in typical system.

It is granteed that character codes for decimal digits are consecutive (N3337 2.3 Character sets, Paragraph 3), so you can add '0' to convert one-digit number to character.

#include <iostream>
using namespace std;
int main()
{
    string s;
    int b=5;
    s.push_back((char)(b + '0'));
    cout<<s<<endl;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
4

You are interpreting the integer 5 as a character. In ASCII encoding, 5 is the Enquiry control character as you lookup here.

The character 5 on the other hand is represented by the decimal number 53.

Sebastian Hoffmann
  • 2,815
  • 1
  • 12
  • 22
2

As others said, you can't convert an integer to a string the way you are doing it.

IMHO, the best way to do it is using the C++11 method std::to_string.

Your example would translate to:

using namespace std;
int main()
{
    string s;
    int b=5;
    s = to_string(b);
    cout<<s<<endl;
}
2

The problem in your code is that you are converting the integer 5 to ASCII (=> ENQ ASCII code, which is not "printable"). To convert it to ASCII properly, you have to add the ASCII code of '0' (48), so:

char ascii = b + '0';

However, to convert an integer to std::string use:

std::stringstream ss; //from <sstream>
ss << 5;
std::string s = ss.str ();

I always use this helper function in my projects:

template <typename T>
std::string toString (T arg)
{
    std::stringstream ss;
    ss << arg;
    return ss.str ();
}
Mattia F.
  • 1,720
  • 11
  • 21
0

Also, you can use stringstream, std::to_string doesn't work for me on GCC

Zeps
  • 19
  • 1
  • 3
0

If we were writing C++ from scratch in 2016, maybe we would make this work. However as it choose to be (mostly) backward compatible with a fairly low level language like C, 'char' is in fact just a number, that string/printing algorithms interpret as a character -but most of the language doesn't treat special. Including the cast. So by doing (char) you're only converting a 32 bit signed number (int) to a 8 bit signed number (char).

Then you interpret it as a character when you print it, since printing functions do treat it special. But the value it gets printed to is not '5'. The correspondence is conventional and completely arbitrary; the first numbers were reserved to special codes which are probably obsolete by now. As Hoffman pointed out, the bit value 5 is the code for Enquiry (whatever it means), while to print '5' the character has to contain the value 53. To print a proper space you'd need to enter 32. It has no meaning other than someone decided this was as good as anything, sometime decades ago, and the convention stuck.

If you need to know for other characters and values, what you need is an "ASCII table". Just google it, you'll find plenty.

You'll notice that numbers and letters of the same case are next to each other in the order you expect, so there is some logic to it at least. Beware, however, it's often not intuitive anyway: uppercase letters are before lowercase ones for instance, so 'A' < 'a'.

I guess you're starting to see why it's better to rely on dedicated system functions for strings!

Francesco Dondi
  • 1,064
  • 9
  • 17