Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2118856
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2012-03-22 16:06:44

1. 发现在u-boot中有些十六进制数,如0x30000000 0x00200000 不直观,搞个小程序直观的显示一下。
其中make_human_readable_str 是从 busybox-1.13.0/coreutils/ls.c拿过来的。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef int smallint;
  4. const char* make_human_readable_str(unsigned long long size, unsigned long block_size, unsigned long display_unit)
  5. {
  6.     /* The code will adjust for additional (appended) units */
  7.     static const char unit_chars[]= { '\0', 'K', 'M', 'G', 'T', 'P', 'E' };
  8.     static const char fmt[] = "%llu";
  9.     static const char fmt_tenths[] = "%llu.%d%c";

  10.     static char str[21] ; /* Sufficient for 64 bit unsigned integers */

  11.     unsigned long long val;
  12.     int frac;
  13.     const char *u;
  14.     const char *f;
  15.     smallint no_tenths;

  16.     if (size == 0)
  17.         return "0";

  18.     /* If block_size is 0 then do not print tenths */
  19.     no_tenths = 0;
  20.     if (block_size == 0)
  21.     {
  22.         no_tenths = 1;
  23.         block_size = 1;
  24.     }

  25.     u = unit_chars;
  26.     val = size * block_size;
  27.     f = fmt;
  28.     frac = 0;

  29.     if (display_unit)
  30.     {
  31.         val += display_unit/2;    /* Deal with rounding */
  32.         val /= display_unit;    /* Don't combine with the line */
  33.         /* will just print it as ulonglong (below) */
  34.     }
  35.     else
  36.     {
  37.         while ((val >= 1024) && (u < unit_chars + sizeof(unit_chars) - 1))
  38.         {
  39.             f = fmt_tenths;
  40.             u++;
  41.             frac = (((int)(val % 1024)) * 10 + 1024/2) / 1024; //这句写得太牛了
  42.             val /= 1024;
  43.         }
  44.         if (frac >= 10)
  45.         {        /* We need to round up here. */
  46.             ++val;
  47.             frac = 0;
  48.         }
  49.         /* Sample code to omit decimal point and tenths digit. */
  50.         if (no_tenths)
  51.         {
  52.             if (frac >= 5)
  53.             {
  54.                 ++val;
  55.             }
  56.             f = "%llu%*c" /* fmt_no_tenths */;
  57.             frac = 1;
  58.         }
  59.     }

  60.     /* If f==fmt then 'frac' and 'u' are ignored. */
  61.     snprintf(str, sizeof(str), f, val, frac, *u);

  62.     return str;
  63. }

  64. int str2num(char str[])
  65. {
  66.     unsigned long n = 0;
  67.     int i;
  68.     for(i=0; str[i]!='\0'; i++)
  69.     {
  70.         if(str[i]>='0' && str[i]<='9')
  71.             n = n*16 + str[i] - '0';
  72.         if(str[i]>='a' && str[i]<='f')
  73.             n = n*16 + str[i] - 'a' + 10;
  74.         if(str[i]>='A' && str[i]<='F')
  75.             n = n*16 + str[i]- 'A' + 10;
  76.     }
  77.     return n;
  78. }

  79. int main(int argc, char *argv[])
  80. {
  81.     unsigned long num = 0;
  82.     if( argc < 2)
  83.     {
  84.         printf("usage: %s \n", argv[0]);
  85.         return -1;
  86.     }

  87.     //支持输入'0x'打头的十六进制数
  88.     if(argv[1][0]=='0' && argv[1][1]=='x' || argv[1][1]=='X')
  89.     {
  90.         printf("Hex:%s\n", argv[1]);
  91.         num = str2num(&argv[1][2]);    
  92.     }
  93.     else
  94.     {
  95.         printf("Hex:Ox%s\n", argv[1]);
  96.         num = str2num(argv[1]);
  97.     }
  98.     printf("Dec:%ld\n", num);
  99.     printf("readable: %s\n", make_human_readable_str(num,1,0));
  100.     return 0;
  101. }
实验一下:
root@ubuntu:~/test# ./test 0x30000000
Hex:0x30000000
Dec:805306368
readable: 768.0M
还不错,呵呵。





阅读(3538) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~