Okay, as the name implies, I would like to change an integer (ascii code number), such as 65, to a string, '65', then back to integer, 65. Now, the reason for doing so, is because I'm working with ascii code, and want to return a string of a fixed length. As known, ascii ranges from 1 - 256. I only want to capture the values from 32 and up. Here are my issues:
1) Check the length of the integer; ( such as the python expression len(str(65)) == 2 )
2) If the length is 2, then make the string be '0' + number;
3) Combine all the strings into one;
4) Convert that back into a number
I have only recently begun c++ programming, after 3~4 months of python programming (Gotten really good at it), and c++ is really confusing.
In a more technical version of what I want to do, assume that my input is 65, 87, 121, 54
Then it'll return:
65087121054
Any help would be greatly appreciated. All I know to do is this:

#include <iostream>
#include <conio.h>

char current;
char pass[100];
int passcount(0);

/* For converting the getch() to ascii code and append to the char array:
pass[passcount] = char(current);
*/

Alright, got it figured out. You'll have to excuse my extra variables. It's going to be a password prompt thing. here it is:

// Make the program take user input
// Replace the password with '*'
#include <iostream>
#include <conio.h>

// Initialize all variables
int current; // Buffer for getch()
char buffer[3];
char pass [100]; // Actual password text to be stored
char passnum [300]; // Password ASCII code to be stored
int passcount; // Counter for array
int passnumcount; // Counter for array
int finalvalue;

int main()
{
  std::cout << "Please enter a password: \n";
  while ((current!= 13) and (passcount <= 99)) {
    current = getch(); // Capture user input
    pass[passcount] = char(current); // Add the letter to the array of password
    passcount++; // Add the current pointer
    // Now convert the int to a string
    itoa(current, buffer, 10);
    if (!buffer[2]) {
      buffer[2] = buffer[1];
      buffer[1] = buffer[0];
      buffer[0] = '0';
    }
    passnum[passnumcount] = buffer[0];
    passnum[passnumcount+1] = buffer[1];
    passnum[passnumcount+2] = buffer[2];
    passnumcount = passnumcount + 3;
    char buffer[3];
    //std::cout << buffer[0] << " " << buffer[1] << " " << buffer[2] << "\n";
  }
  std::cout << passnum << "\n";
  finalvalue = atoi(passnum);
  std::cout << passnum << "\n";
}

Do you know how to use the % operator? You can take your integers and split them into 3 digits which can be converted into characters.

u r coding in c++..
thare is standard string template class..

try

string temp;
string finalstring;
int a = 65;
char buff[5];
temp = itoa(a,buff,10);
//calcualte the length of temp and append 0 or what ever u want like
temp.length() ; //will give u length of string;

finalstring.append(temp);
finalstring.append("0");//u can replace 0 by others as u required

a = 95;
temp = itoa(a,buff,10);
.
.
finalstring.append(temp);
.
.
.
//and so on..
//then 

int finalint; 
finalint = atoi(finalstring.c_str());

hope this will help

Look up the functions atoi and itoa available in the math.h header file.
However if you want to do it on your own. The to convert string to int, take the last character (one's place) and multiply it by 10 raised to 0, then add it to the the second last digit multiplied by 10 raised to 1 and so on...

So, if you want to convert "162" to int then, this will be the formula:
2 X 1 + 6 X 10 + 1 X 100

% operator returns the remainder.
Let x = the integer number
So to convert int to string find the remainder of the number when x is divided by 10(this is your units digit = a),
then find the remainder when x-a divided by 100 (this is your ten's digit = b)
and then find the remainder when x - a - b is divided by 1000 (this is your hunredth's digit) and so on...

Hope this helps

This article has been dead for over six months. Start a new discussion instead.