Integer to string conversion and vice-versa in c++

While programming, we often need to convert an integer into string and vice-versa. Some of the examples scenarios to do so are as follows:
  • One may want to concatenate a string and integer number 
  • Read input from a file that contains multiple integers and report the sum. 

There are multiple ways to do it. C provides inbuilt functions stoi (read as string to integer) and sprintf to do this task. C++ provides an object-oriented approach for the same. It provides better methods to do it via sstream library. 

Concatenation of a string and an integer: The following piece of code concatenates a string and an integer and returns the result as a string. It first declares an object of class stringstream and simply reads in a number into the object. There is nothing special that we need to do for the same.
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
string concatenate(const string &str,int num) {
    stringstream s; // stringstream class is defined in sstream
    s<<num;      //integer num is assigned to string s
    string ret = str + s.str(); // s.str() return string object from s
    return ret;
}

String to integer conversion: Following piece of code (function) takes two strings as input, converts them into integers by assigning to objects of istringstream type, and returns their sum.
int sum(const string &str1,const string &str2) {    istringstream s1(str1); // istringstream is defined in sstream    istringstream s2(str2);    int in1,in2;    s1>>in1;    s2>>in2;    return in1+in2;}
The following piece of code calls the above two functions to perform concatenation and calculations:
int main() {   cout << concatenate("my birthday is on this month of ", 25)<<endl;   cout<<"sum = "<<sum("1","2")<<endl;   cout << concatenate("sum is ",sum("1","2") );}

Also read:

Thermometer code

What is thermometer code: Thermometer code resembles the output produced by a thermometer. In thermometer code, a value representing number ‘N’ has the lowermost ‘N’ bits as ‘1’; others as 0. So, to move from N to ‘N+1’, just change the rightmost ‘0’ to ‘1’. Figure 1 below shows graphically the thermometer codes for values from ‘0’ to ‘7’. As is evident, each value resembles a reading in thermometer. This is how, thermometer code got its name. Flash ADCs, time-to-digital converters (TDC) are some of the circuits that utilize thermometer code.

A thermometer code is a series of zeroes followed by a series of ones. A 8-symbol thermometer code will have 7 bits that need to represent all symbols.
Thermometer code with 7 symbols







Characteristics of thermometer code:
  • Each symbol in thermometer code is a sequence of 0s followed by a sequence of 1s
  • There cannot be 0s in-between two 1s. For example, a symbol 01011 is invalid in thermometer code
  • For an n-bit binary code, the corresponding thermometer code will have 2n – 1 symbols; hence, as many bits will be needed to represent thermometer code for the same.

How to convert from binary to thermometer code: Given below is the VHDL code for a 3-bit binary to thermometer converter. A simple case statement can be utilized for the same.
                                  
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
entity bin2therm2bit is
                port (
                                binary_input : in std_logic_vector (1 downto 0);
                                therm_output : out std_logic_vector (6 downto 0)
                );
end bin2therm6bit;

architecture Behavioral of bin2therm6bit is
begin
                process (binary_input)
                begin
                                label1 : case binary_input is
                                                when "000" => therm_output <= "0000000";
                                                when "001" => therm_output <= "0000001";
                                                when "010" => therm_output <= "0000011";
                                                when "011" => therm_output <= "0000111";
                                                when "100" => therm_output <= "0001111";
                                                when "101" => therm_output <= "0011111";
                                                when "110" => therm_output <= "0111111";
                                                when "111" => therm_output <= "1111111";
                                                when others => therm_output <= “xxxxxxx”;
                                end case;
                end process;
end Behavioral;

Hope you’ve found this post useful. Let us know what you think in the comments.

Also read: