面试题: 十进制INT数字转化为16进制字符串、实现字符串和数字的相互转化

来源:互联网 发布:应届生找工作迷茫知乎 编辑:程序博客网 时间:2024/05/31 13:15

自己练练了手,写了两个程序:

 第一个:十进制的数字转化为16进制

 

  1. int main(int argc, _TCHAR* argv[])
  2. {
  3.     int a = 100000000, i = 0, b;
  4.     int c[8] = {0};
  5.     printf("%10.4x ", a);
  6.     while (a) {
  7.         b = a - (a>>4<<4);
  8.         c[i] = b;
  9.         i++;
  10.         a = a>>4;
  11.     }
  12.     i = i - 1;
  13.     printf("The number of hex is: ");
  14.     for (int index = i; index >= 0; index--)
  15.         printf("%x", c[index]);
  16.     printf(" ");
  17.     return 0;
  18. }

 第二个,将ascII字符转化为十进制数字,  不是数字的转化!

 

 

  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4. int main() {
  5.  char *str = "asd";
  6.  const int N = strlen(str);
  7.  int *array = new int[N-1];
  8.  int i = 0;
  9.  while (*str != '/0') {
  10.   array[i] = (int)(*str); //直接转化存储即可
  11.   cout << array[i] << endl;
  12.   i++;
  13.   str++;  
  14.  } 
  15. }

3、数字的转化为字符串,字符串转为数字:

  1. #include<stdio.h>     
  2. #include<stdlib.h>   
  3.       
  4. char *i_to_a(int value) {  
  5.    char temp[30];
  6.    char* result;   
  7.    int i=0,j=0;   
  8.    while(value!=0){   
  9.     temp[i]=value%10+48;   
  10.     value/=10;   
  11.     i++;   
  12.    }   
  13.    result =(char*)malloc(sizeof(char)*(i+1));   
  14.    for(j=0;j<i;j++){   
  15.     result[j]=temp[i-1-j];   
  16.    }   
  17.    result[j]='/0';   
  18.    return result;   
  19. }  
  20. bool a_to_i(char *a, int *num) {
  21.  char *p = a;
  22.  int i = 0;
  23.  *num = 0;
  24.  //check the string 
  25.  if ( (*p == '+') || (*p == '-') )
  26.   i = 1;
  27.  while (*(p + i) != '/0') {
  28.   if (*(p+i)>='0' && *(p+i)<='9') {
  29.    i++;
  30.   } 
  31.   else {
  32.    printf("Illegall!/n");
  33.    return 1;
  34.   }
  35.  }
  36.  if (i == 1) {
  37.   printf("Illegall!/n");
  38.   return 1;
  39.  }
  40.  //start converting, here the string is in a right formulation 
  41.  while (*p != '/0') {
  42.   if ( (*p == '+') || (*p == '-') ) {
  43.    p++;
  44.   }
  45.   else {
  46.    *num = *num * 10 + *p -'0';
  47.    p++;
  48.   }
  49.  }
  50.  if (*a == '-') {
  51.   *num = -(*num);
  52.  }
  53.  else {
  54.  }
  55.  return 0;
  56. }
  57. int main() {       
  58.  int i=6553567;   
  59.  char* p = i_to_a(i);   
  60.  printf("%s/n",p);   
  61.  free(p);   
  62.  int num = 0;
  63.  if ( !a_to_i("0000", &num))
  64.  printf("num = %d/n", num);
  65.  return   0;   
  66. }     

 

4 数字的转化为字符串,字符串转为数字(2):

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void myitoa_first(long long a, char *p) {
  5.     if (a == 0)
  6.     {
  7.         *p = '/0';
  8.     }
  9.     else {      
  10.         myitoa_first(a/10, p+1);
  11.         *p = a%10 + '0';        
  12.     }
  13. }
  14. void myitoa_second(char *p) {
  15.     char *p1 = p;
  16.     char *p2 = p + strlen(p) - 1;   
  17.     int a;
  18.     while (p1 < p2)
  19.     {
  20.         a = *p2;
  21.         *p2 = *p1;
  22.         *p1 = a;
  23.         p1++;
  24.         p2--;
  25.     }
  26. }
  27. void myitoa(long long a, char *p) {
  28.     myitoa_first(a, p);
  29.     myitoa_second(p);
  30. }
  31. int myatoi(char *pp) {
  32.     char *p = pp;
  33.     int sum = 0;
  34.     bool flag  = 0;
  35.     if (*p == '+')
  36.     {
  37.         flag = 0;
  38.         p++;
  39.     }
  40.     else if (*p == '-')
  41.         {
  42.             flag = 1;
  43.             p++;
  44.         }
  45.     while (*p != '/0')
  46.     {
  47.         if (*p >= '0' && *p <= '9')
  48.         {       
  49.             sum = sum *10 +*p - '0';
  50.             p++;
  51.         }
  52.         else
  53.             break;
  54.     }
  55.     if (flag)
  56.     {
  57.         sum *= -1;
  58.     }
  59.     return sum;
  60. }
  61. int main() {
  62.     char *p = (char *)malloc(32 * sizeof(char) );
  63.     long long a = 12345;
  64.     myitoa(a, p);
  65.     printf(p);
  66.     puts("");
  67.     char *pp = "-1234567s89";
  68.     int aa = myatoi(pp);
  69.     printf("a = %d/n", aa);
  70. }