CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    converting char[2] to an int, is it possible ?

    Hi !
    I have a char array that contains digits such as {'0', '1'}. I want to convrt it to an int (1) like in the following code"
    Code:
     int main()
    {
       char myInts[3] = {'0','2','\0'};
       int myChars = myInts;
       return 0;
    }
    surely
    Code:
    int myChars = myInts;
    is an error but is there a function that can make the convertion ?
    Thanks !

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: converting char[2] to an int, is it possible ?

    Take a look at the atoi function (ansi to integer) it converts an string (that contains legal digits in ansi code) to an integer:

    Code:
    int main()
    {
       char myInts[3] = {'0','2','\0'};
       int myChars = atoi(myInts);
       return 0;
    }
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  3. #3
    Join Date
    May 2001
    Location
    israel
    Posts
    112

    Re: converting char[2] to an int, is it possible ?

    Thanks a lot !

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: converting char[2] to an int, is it possible ?

    'atoi' has some drawbacks...take a look at the following FAQ for alternatives..

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