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

数字转换技巧
本文提供了几个实用的数字转换程序,包括十进制到十六进制的转换、ASCII字符到十进制数字的转换以及数字和字符串之间的相互转换。这些程序使用C/C++语言实现,为开发者提供了基本的数据类型转换思路。

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

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

  1. intmain(intargc,_TCHAR*argv[])
  2. {
  3. inta=100000000,i=0,b;
  4. intc[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("Thenumberofhexis:");
  14. for(intindex=i;index>=0;index--)
  15. printf("%x",c[index]);
  16. printf("");
  17. return0;
  18. }

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

  1. #include<iostream>
  2. #include<stdio.h>
  3. usingnamespacestd;
  4. intmain(){
  5. char*str="asd";
  6. constintN=strlen(str);
  7. int*array=newint[N-1];
  8. inti=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. char*i_to_a(intvalue){
  4. chartemp[30];
  5. char*result;
  6. inti=0,j=0;
  7. while(value!=0){
  8. temp[i]=value%10+48;
  9. value/=10;
  10. i++;
  11. }
  12. result=(char*)malloc(sizeof(char)*(i+1));
  13. for(j=0;j<i;j++){
  14. result[j]=temp[i-1-j];
  15. }
  16. result[j]='\0';
  17. returnresult;
  18. }
  19. boola_to_i(char*a,int*num){
  20. char*p=a;
  21. inti=0;
  22. *num=0;
  23. //checkthestring
  24. if((*p=='+')||(*p=='-'))
  25. i=1;
  26. while(*(p+i)!='\0'){
  27. if(*(p+i)>='0'&&*(p+i)<='9'){
  28. i++;
  29. }
  30. else{
  31. printf("Illegall!\n");
  32. return1;
  33. }
  34. }
  35. if(i==1){
  36. printf("Illegall!\n");
  37. return1;
  38. }
  39. //startconverting,herethestringisinarightformulation
  40. while(*p!='\0'){
  41. if((*p=='+')||(*p=='-')){
  42. p++;
  43. }
  44. else{
  45. *num=*num*10+*p-'0';
  46. p++;
  47. }
  48. }
  49. if(*a=='-'){
  50. *num=-(*num);
  51. }
  52. else{
  53. }
  54. return0;
  55. }
  56. intmain(){
  57. inti=6553567;
  58. char*p=i_to_a(i);
  59. printf("%s\n",p);
  60. free(p);
  61. intnum=0;
  62. if(!a_to_i("0000",&num))
  63. printf("num=%d\n",num);
  64. return0;
  65. }

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

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. voidmyitoa_first(longlonga,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. voidmyitoa_second(char*p){
  15. char*p1=p;
  16. char*p2=p+strlen(p)-1;
  17. inta;
  18. while(p1<p2)
  19. {
  20. a=*p2;
  21. *p2=*p1;
  22. *p1=a;
  23. p1++;
  24. p2--;
  25. }
  26. }
  27. voidmyitoa(longlonga,char*p){
  28. myitoa_first(a,p);
  29. myitoa_second(p);
  30. }
  31. intmyatoi(char*pp){
  32. char*p=pp;
  33. intsum=0;
  34. boolflag=0;
  35. if(*p=='+')
  36. {
  37. flag=0;
  38. p++;
  39. }
  40. elseif(*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. returnsum;
  60. }
  61. intmain(){
  62. char*p=(char*)malloc(32*sizeof(char));
  63. longlonga=12345;
  64. myitoa(a,p);
  65. printf(p);
  66. puts("");
  67. char*pp="-1234567s89";
  68. intaa=myatoi(pp);
  69. printf("a=%d\n",aa);
  70. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值