C language processing incoming parameters

 

   /* Loop through argv parsing options. */
    while ((c = getopt(argc, argv, ":f:a:l:h")) != -1) {
        switch (c) {
        case 'f':
            filename = strdup(optarg);
            break;
        case 'a':
            ddr_addrp = strdup(optarg);
            if(ddr_addrp[0]=='0'&&(ddr_addrp[1]=='x'||ddr_addrp[1]=='X'))
                ddr_addr = strtol(ddr_addrp,&stop,16);
            else
                ddr_addr = atoi(ddr_addrp);
            break;
        case 'l':
            filesizep = strdup(optarg);
            if(filesizep[0]=='0'&&(filesizep[1]=='x'||filesizep[1]=='X'))
                filesize = strtol(filesizep,&stop,16);
            else
                filesize = atoi(filesizep);
            break;
        case 'h':
            printf("Usage: %s -f [filename] -a [DDR address] -l [filesize].\n",argv[0]);
            return -1;
        case ':':
            printf("-%c miss option argument.\n",optopt);
            return -1;
        case '?':
            printf("-%c is invalid option.\n",optopt);
            return -1;
        }
    }

 

C language is indeed profound, travel the world for so many years in C language, it was found that he is still a rookie, many have not been able to control the weapon, we introduced a mythical beasts, super-powerful today, but few people use it well.

Prototype:

#include <string.h>
char *strdup(const char *s); 

Function Description:

  strdup () function is commonly used as a string copy c language library functions, general and free () function in pairs.

strdup () internally calls malloc () to allocate memory for variables, it is not necessary to use the returned string, need to use free () to release the corresponding memory space, otherwise it will cause a memory leak. The return value of the function is to return a pointer to the allocated character string copy space; if the allocation fails space, NULL value is returned.

 

 

   In the process, sometimes you need to convert a hexadecimal string to a decimal number. such as:

    char *ptr = "0x11";

    int n = 0;

    // we want n equal to 0x11, or 17

    Usually we in C, would like to convert a string to an integer number, usually using the following method:

char *ptr="123";    
int n=0;
n=atoi(ptr);
printf("%d/n",n);

//输出:123

 

However, only the library function atoi decimal string into an integer int, as in the following example:

#include <stdlib.h>
#include <stdio.h>//atoi头文件

int main(void)
{
   int n;
   char *str = "12345.67";

   n = atoi(str); //int atoi(const char *nptr);
   printf("string = %s integer = %d/n", str, n);
   return 0;
}
/*输出:
  string = 12345.67 integer = 12345
*/

 

So, use the atoi function "0x11" into a decimal integer 17 is not acceptable. If so, will output the following results:

   n int;
   char * str = "0x11";

   n = atoi (str); // returns (obviously not the result we want) the value of n is equal to 0

method 1:

#include <stdio.h>

int main()
{
    char   szValue[]  =   "0x11";   
    int    nValude    =   0;       
    sscanf(szValue,"%x",&nValude);   
    printf("%d/n",nValude);
    return 0;    
}
/*输出:
  17
*/

The main use sscanf this library function:

    Function name: sscanf

    Function: performing formatted input string from the

    Usage: int sscanf (char * string, char * format [, argument, ...]); //% x is the type we want to format that output in hexadecimal

 

Method 2:

#include <stdio.h>
#include <stdlib.h> // strtol header

int main ()
{
    char * P = "0x11";   
    char * STR;   
    int I = (int) strtol (P, & STR, 16) ; // hex
    the printf ( "% D / n-", I);
    return 0;    
}
/ * output:
  17
* /

 

    Here the main use strtol this library function, its use is:

    Function name: strtol

    Function: the string is converted to a long integer

    Usage: Long strtol (char * str, char ** endptr is, int Base); // Base indicates that we want to convert to a few decimal

 


int main(void)
{
   char *string = "0x11", *endptr;
   long lnumber;

   /* strtol converts string to long integer */
   lnumber = strtol(string, &endptr, 16);
   printf("string = %s  long = %ld/n", string, lnumber);

   return 0;
}
/*输出:
  string = 0x11 long = 17
*/

Guess you like

Origin www.cnblogs.com/idyllcheung/p/11446841.html