CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 1999
    Posts
    70

    atoi / atof - How to make a Number

    Hi,
    I want to make a CString to for example an Ascii-Code (it should be numbers),
    a friend told me, that I should use atoi/atof. How must I use this??

    I thanks for every Help!!

    Black Racer


  2. #2
    Join Date
    May 1999
    Location
    CA, USA
    Posts
    586

    Re: atoi / atof - How to make a Number

    No... That's if you have an integer representation in a char string which you want to convert to its numerical equivalent... What you're looking for is _toascii which takes a char at a time, so you'd have to use CString::GetAt() and pass each character in your CString one at a time to _toascii() to get your string equivalent in ASCII.

    Rail

    Recording Engineer/Software Developer
    Rail Jon Rogut Software
    [email protected]
    http://home.earthlink.net/~railro/

  3. #3
    Join Date
    Jul 1999
    Posts
    70

    Re: atoi / atof - How to make a Number

    can you show me an short eample??

    I have the Name "Black Racer" there, and I would create an SerialNumber.

    So I work with the name:


    CxyzDlg::ONClick()
    {
    CString serial1, serial2, serial3, serial4, serial5;

    serial1 = m_nameedit; //m_nameedit is the text in TextBox1
    serial1.TrimRight(); //you know
    serial2 = serial1.Mid(2, 2);
    serial3 = serial1.Left(3);
    serial4 = serial1.Right(1);
    serial5 = serial2 + serial3 + serial4;

    if(m_serialedit == serial5)
    {
    MessageBox("Right Code", MB_OK + MB_ICONINFORMATION);
    }
    else
    {
    MessageBox("Wrong Code", MB_OK + MB_ICONINFORMATION);
    }

    }

    So, the problem is, that Serial5 should be a number.

    How can I do that???

    Black Racer


  4. #4
    Join Date
    Jun 1999
    Posts
    47

    Re: atoi / atof - How to make a Number

    Here is a function that does exactly what you asked:

    double str2float(CString str)
    {
    CString strFloat = str;
    CString strInt = "";
    CString strZecim = "0";
    BOOL bZecim = FALSE;
    for(int c=0; c<strFloat.GetLength(); c++){
    if(strFloat[c] == ',' && !bZecim){
    bZecim = TRUE;
    strZecim = "";
    }
    if(!bZecim){
    if(strFloat[c] >= '0' && strFloat[c] <= '9')
    strInt += strFloat[c];
    }
    else{
    if(strFloat[c] >= '0' && strFloat[c] <= '9')
    strZecim += strFloat[c];
    }
    }
    strFloat = strInt + "." + strZecim;
    return (double)atof(strFloat);
    }


    Regards,
    Dani Zilcsak
    mailto[email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured