字符串拷贝与进制转换的简单应用

来源:互联网 发布:mac mini好用吗 编辑:程序博客网 时间:2024/06/05 18:32

源码中需要将整数分成前后两个部分,可以用strncpy()函数来实现

#include<stdio.h> #include<string.h> int main() {      char str1[10]={0},str2[10]={0};     char temp[10]={0};     char *string="abcdefghi";      int i =12345678;     sprintf(temp,"%d",i);     strncpy(str1,temp,4);     strncpy(str2,temp+4,4);      printf("str1 is %s,str2 is %s\n",str1,str2);      strncpy(str1,string,3);     str1[3]='\0';     strncpy(str2,string+4,5);      str2[5]='\0';     printf("str1 is %s,str2 is %s\n",str1,str2);      return 0; } 运行结果:str1 is 1234,str2 is 5678str1 is abc,str2 is efghi


项目应用场景:将一个十进制数转换成十六进制,然后将十六进制分成前后两部分,再分别以十进制数显示出来

#include<stdio.h> #include<string.h> #include<stdlib.h>int main() {      char str1[10]={0},str2[10]={0};     char temp[10]={0},str[10]={0};     char *stopstring;     int front=0,rear=0; int next_lockid = 18913516; ltoa(next_lockid,temp,16);       sprintf(str,"%08s",temp);     printf("temp is %s,str is %s\n",temp,str);     strncpy(str1,str,4);     strncpy(str2,str+4,4);      front = strtol(str1,&stopstring,16);     rear = strtol(str2,&stopstring,16);     printf("str1 is %s,str2 is %s\n",str1,str2);      printf("front is %d,rear is %d\n",front,rear);      return 0; } 运行结果:temp is 12098ec,str is 012098ecstr1 is 0120,str2 is 98ecfront is 288, rear is 39148
        通常在<stdlib.h>头文件中包含函数atoi(),该函数是标准的C语言函数,其功能是:把字符串转换成32位以下的任意进制数字。  
   
        而与其相反的itoa()函数则非标准C语言扩展函数。由于它不是标准C语言函数,所以不能在所有的编译器中使用。但是,大多数的编译器(如Windows上)在<stdlib.h>头文件中包含这个函数,而在linux系统中则不支持。      
        由于项目是在linux下编程,所以itoa()函数不能直接用,需要自己实现itoa()函数,下面是实现的itoa()函数源码:


  

#include<stdio.h> #include<string.h> #include<stdlib.h>char* _itoa(int value, char* string, int radix){    char tmp[33];    char* tp = tmp;    int i;    unsigned v;    int sign;    char* sp;     if (radix > 36 || radix <= 1)    {         return 0;    }     sign = (radix == 10 && value < 0);    if (sign)        v = -value;    else        v = (unsigned)value;    while (v || tp == tmp)    {        i = v % radix;        v = v / radix;    if (i < 10)        *tp++ = i+'0';    else        *tp++ = i + 'a' - 10;    }     if (string == 0)    string = (char*)malloc((tp-tmp)+sign+1);    sp = string;     if (sign)        *sp++ = '-';    while (tp > tmp)        *sp++ = *--tp;        *sp = 0;    return string;} 


    另附上实现的atol()函数,以备不时之需

#include "stdio.h"#include "conio.h"#include <ctype.h>#include <stdlib.h>

long  atol(const char *nptr){    int c; /* current char */    long total; /* current total */    int sign; /* if ''-'', then negative, otherwise positive */ /* skip whitespace */    while ( isspace((int)(unsigned char)*nptr) )        ++nptr;     c = (int)(unsigned char)*nptr++;    sign = c; /* save sign indication */    if (c == '-' || c == '+')        c = (int)(unsigned char)*nptr++; /* skip sign */     total = 0;     while (isdigit(c)) {        total = 10 * total + (c - '0'); /* accumulate digit */        c = (int)(unsigned char)*nptr++; /* get next char */    }     if (sign == '-')        return -total;    else        return total; /* return result, negated if necessary */}

    atoi()函数可以通过调用atol()函数获取

int atoi(const char *nptr){    return (int)atol(nptr);}



0 0
原创粉丝点击