Convert string to integer display

Source: Internet
Author: User

Tag: Io for Ar res Har string EF return

Usage./change 23456 10, displayed in the hexadecimal format specified at the end

# Include <stdio. h>

# Include <stdlib. h>
# Include <unistd. h>
Int mystrlen (char * s)
{
Int Len = 0;
Char * temp = s;
While (* (temp )! = '\ 0 ')
{
++ Len;
++ Temp;
}
Return Len;
}
Int str2int (char * s)
{
Int sum = 0, Len, I;
Len = mystrlen (s );
For (I = 0; I <Len; I ++)
{
If (* (S + I) <'0') | (* (S + I)> '9 '))
{
Printf ("Warning: Can't convert it into int \ n ");
Exit (1 );
}
Int K = len-I;
Int POW = 1;
While (k> 1)
{
Pow * = 10;
-- K;
}
Sum = sum + (* (S + I)-'0') * POW;
}
Return sum;
}
Int main (INT argc, char * argv [])
{

Int result, SW;
If (argc! = 3)
{
Printf ("Usage: argv [0] Number 8 | 10 | 16 \ n ");
Exit (1 );
}
Char * s = argv [1];
Char * S2 = argv [2];
// Printf ("% d, % d \ n", mystrlen (s), str2int (s ));
Sw = str2int (S2 );
Result = str2int (s );

Switch (SW)
{
Case 8:
Printf ("% O \ n", result );
Break;
Case 10:
Printf ("% d \ n", result );
Break;
Case 16:
Printf ("% 0x \ n", result );
Break;
Default:
Printf ("Usage: argv [0] Number 8 | 10 | 16 \ n ");
Exit (1 );
}
Exit (0 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.