How do I convert, in DOS programming, strings to int and visa versa? nt

This is a discussion on How do I convert, in DOS programming, strings to int and visa versa? nt within the C++ Programming forums, part of the General Programming Boards category; nt...

  1. #1
    Somed00d
    Guest

    Question How do I convert, in DOS programming, strings to int and visa versa? nt

    nt

  2. #2
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Smile its easy!!!

    no problem!

    #include <stdio.h>

    void main()
    {
    int lala = 0;
    char mystring[255];
    sprintf(mystring,"79");
    sscanf(mystring,"%i",&lala);
    printf("%i",lala); // This will print 79
    }

    SPH

  3. #3
    Unregistered
    Guest
    Oh, I was looking for a function to convert it. I am using cout and cin, etc.

    What I am trying to do is input a number, apply commas to it every 3 digits, and store that as a string and output it to the screen.

    Then take that string, remove the commas (I have an algorithm for that already), and then convert it to an integer so I can do operations to the number.

  4. #4
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Exclamation write it

    Write a class with a streampos, if u want one I made u can use it:

    // NOTE There may be a few bugs... made thsi in a hurry
    Code:
    #include <windows.h>
    #include <fstream.h>
    #include <stdio.h>
    
    #define SEEK_BEG 10 
    #define SEEK_POS 5
    
    class string
    {private:
    char *str;
    int cstreampos;
    public:
    	bool checkwhite(char t);
    	void clear();
    	void seek(int to,int op);
    	int tell();
    	string operator<<(char *add);
    	string operator<<(float addf);
    	string operator<<(string unused);
    
    	string operator>>(char *get);
    	string operator>>(float &getf);
    	string operator>>(string unused);
    
    	void getLine(char *out,unsigned int size,char end);
    	char *GetDat();
    };
    
    bool string::checkwhite(char t)
    {
    	switch(t)
    	{
    	case ' ': return true;
    	case '\r': return true;
    	case '\n': return true;
    	case '\t': return true;
    	case '\0': return true;
    	default: return false;
    	}
    }
    
    void string::clear()
    {
    	ZeroMemory((void*)this,sizeof(string));
    	str = new char[1];
    	sprintf(str,"");
    }
    
    void string::seek(int to,int op)
    {
    	if( (op == SEEK_POS) || (op == NULL) )
    	{
    		cstreampos = to;
    	}
    	else if( op == SEEK_END )
    	{
    		cstreampos = strlen(str);
    	}
    	else if( op == SEEK_BEG )
    	{
    		cstreampos = 0;
    	}
    }
    
    int string::tell()
    {
    	return cstreampos;
    }
    
    string string::operator <<(char *add)
    {
    	char *bin = str;
    	str = new char[strlen(add)+strlen(bin)];
    	for( unsigned int sloop = 0 ; sloop<strlen(bin) ; sloop++ )
    	{
    		str[sloop] = bin[sloop];
    	}
    	for( sloop = strlen(bin) ; sloop<strlen(add)+strlen(bin) ; sloop++ )
    	{
    		str[sloop] = add[sloop-strlen(bin)];
    		cstreampos++;
    	}
    	str[strlen(add)+strlen(bin)] = '\0';
    	delete[] bin;
    	string unused;
    	ZeroMemory((void*)&unused,sizeof(string));
    	return unused;
    }
    
    string string::operator <<(float addf)
    {
    	MessageBox(NULL,"A float","af",MB_OK);
    	char *bin = str;
    	str = new char[sizeof(float)+strlen(bin)];
    	char *add = (char*)&addf;
    	for( unsigned int sloop = 0 ; sloop<strlen(bin) ; sloop++ )
    	{
    		str[sloop] = bin[sloop];
    	}
    	for( sloop = strlen(bin) ; sloop<sizeof(float)+strlen(bin) ; sloop++ )
    	{
    		str[sloop] = add[sloop-strlen(bin)];
    		cstreampos++;
    	}
    	str[sizeof(float)+strlen(bin)] = '\0';
    	delete[] bin;
    	string unused;
    	ZeroMemory((void*)&unused,sizeof(string));
    	return unused;
    }
    
    string string::operator >>(char *get)
    {
    	sprintf(get,"");
    	for( unsigned int sloop = cstreampos ; checkwhite(str[sloop]) ; sloop++ );
    	for( ; !checkwhite(str[sloop]) ; sloop++ )get[sloop-cstreampos] = str[sloop];
    	string unused;
    	ZeroMemory((void*)&unused,sizeof(string));
    	return unused;
    }
    
    string string::operator >>(float &getf)
    {
    	ZeroMemory((void*)&getf,sizeof(float));
    	char *get = (char*)&getf;
    	for( unsigned int sloop = cstreampos ; checkwhite(str[sloop]) ; sloop++ );
    	for( ; (sloop-cstreampos)<sizeof(float) ; sloop++ )get[sloop-cstreampos] = str[sloop];
    	string unused;
    	ZeroMemory((void*)&unused,sizeof(string));
    	return unused;
    }
    
    void string::getLine(char *out,unsigned int size,char end)
    {
    	sprintf(out,"");
    	for( unsigned int sloop = cstreampos ; ((sloop-cstreampos)<size) && (str[sloop] != end ) ; sloop++ )out[sloop-cstreampos] = str[sloop];
    }
    
    char *string::GetDat()
    {
    	char *ret = new char[strlen(str)];
    	ZeroMemory((void*)ret,strlen(str));
    	sprintf(ret,str);
    	return ret;
    }

    SPH
    // I also expect this will also jsut give you the idea, and u write ur own


  5. #5
    Unregistered
    Guest
    Whoa, thanks.

    That's a bit in-depth for me, though. Aren't there functions included with borland C++ builder 5 or something?

  6. #6
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Post yeah sorta

    Well... you can use strstream things, I dont suggest it, but if u must use C++ then look them up in MSDN or in Borland's Help files. Its a dirived istream, I think you should stick with sscanf and stream classes . If u want I would be happy to tutor you in C++ a little.

    SPH

  7. #7
    Unregistered
    Guest
    There are a whole host of routines in the cstdlib library...
    this one converts an interger to an alpha

    $include <cstdlib>
    char *_itoa( int value, char *string, int radix );
    //value = number to be changed
    //string = pointer to an alpha array
    //radix = base of value

    int atoi( const char *string );

    will convert alpha to an interger

  8. #8
    Unregistered
    Guest
    thx

  9. #9
    Unregistered
    Guest

    Question Need help also, but converting array..?

    I need to do the same thing, as in I am reading a file (ascii)
    which contains only numbers... it is read in as such:

    <blah blah>
    inputFilePtr = fopen(argv[1], "r");
    FileBuff = calloc(FileLength +1, sozeof(char));
    ...
    fread(FileBuff, FileLength, 1, input);


    The input file is like:
    3 1
    234 20 46.234 .0000032 233.332
    2
    1 40
    2
    2349087E-4 32409e-4 2309e-7 84384e-5 329e-6....
    ....
    ....

    I have pointers to the beginning and end of this array,
    and I can print it out using a switch and strtod to print
    it out as the actual numbers (and it works)... the real
    problem is that I need to print out (to a file) every other
    number in scientific notation. (I need to print it out in
    non scientific notation, but I can handle that...I think)

    Anyone have any ideas? It has been years since I have done
    C, and I think I am going about this the wrong way

    Any help would be appreciated.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21