Convert string to integer display

Source: Internet
Author: User

Method of use./change 23456 10, by the last specified in the binary display

#include <stdio.h>

#include <stdlib.h>
#include <unistd.h>
int Mystrlen (char *s)
{
int len=0;
Char *temp=s;
while (* (temp)! = ') '
{
++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);
}

Convert string to integer display

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.