Thread: Convert to string w/o _itoa?

  1. #1
    Registered User
    Join Date
    Jun 2004

    Convert to string w/o _itoa?

    Is there a way to convert something like an int or an unsigned short to a string without using _itoa? The reason I ask is that I'm using a std::string to hold a date. The month, day and year components of the date are unsigned shorts. I want to copy and then append these elements along with "\" to create a human readable date. The only way that I know to convert an unsigned short to a string so that I can append it to a std::string is using the _itoa function. The problem with this is that it requires me to pass in a buffer to hold the result, and this buffer must be a char* or void* (I forget which, but it doesn't matter). This would basically require me to create a useless buffer just to pass in to _itoa and then delete it. I think this is pretty harmless, but hardly elegent or efficient, is there a better way?

  2. #2
    Registered User
    Join Date
    Jun 2004
    In case it helps, here is the code that I'm working with...

    string GetFileCreationTime(char* pFileName)
    	string pFileCreationDate(11, '\0');	//buffer to hold the date
    	char* pUseless = new char[5];	//useless buffer needed for _itoa
    	WIN32_FILE_ATTRIBUTE_DATA wfad;	//file attribute data structure
    	SYSTEMTIME st;	//systemtime structure
    	ZeroMemory(&wfad, sizeof(WIN32_FILE_ATTRIBUTE_DATA));	//init memory
    	ZeroMemory(&st, sizeof(SYSTEMTIME));	//init memory
    	GetFileAttributesEx(pFileName, GetFileExInfoStandard, &wfad);	//get file information
    	FileTimeToSystemTime(&wfad.ftCreationTime, &st);	//convert to human readable time
    	pFileCreationDate = _itoa(st.wMonth, pUseless, 10);
    	pFileCreationDate.append(_itoa(st.wDay, pUseless, 10));
    	pFileCreationDate.append(_itoa(st.wYear, pUseless, 10));
    	delete [] pUseless;
    	return pFileCreationDate;

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Use a stringstream:

    #include <string>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
        string date;
        int month = 1;
        int day = 8;
        int year = 2005;
        stringstream temp;
        temp << setw(2) << setfill('0') << month << '/'
             << setw(2) << setfill('0') << day << '/'
             << year;
        // Convert stringstream to string
        date = temp.str();
        // Display string containing date
        cout << date << endl;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jun 2004
    That is some cool coding HKMP5. I didn't even know such a thing existed

    Thanks a bunch!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM