中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C/C++
csapp show-bytes
作者:未知 时间:2005-09-13 23:28 出处:Blog.ChinaUnix.net 责编:chinaitpower
              摘要:csapp show-bytes

csapp show-bytes

/* $begin show-bytes */
#include

typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer start, int len)
{
    int i;
    for (i = 0; i < len; i++)
 printf(" %.2x", start[i]);
    printf("\n");
}

void show_int(int x)
{
    show_bytes((byte_pointer) &x, sizeof(int));
}

void show_float(float x)
{
    show_bytes((byte_pointer) &x, sizeof(float));
}

void show_pointer(void *x)
{
    show_bytes((byte_pointer) &x, sizeof(void *));
}
/* $end show-bytes */


/* $begin test-show-bytes */
void test_show_bytes(int val)
{
    int ival = val;
    float fval = (float) ival;
    int *pval = &ival;
    show_int(ival);
    show_float(fval);
    show_pointer(pval);
}
/* $end test-show-bytes */

void simple_show()
{
/* $begin simple-show */
int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show */
}

void float_eg()
{
/* $begin float-show */
int x = 3490593;
float f = (float) x;
show_int(x);
show_float(f);
/* $end float-show */
}

void string_eg()
{
/* $begin show-string */
char *s = "ABCDEF";
show_bytes(s, strlen(s));
/* $end show-string */
}

void show_twocomp()
{
/* $begin show-twocomp */
    short int x = 12345;
    short int mx = -x;
   
    show_bytes((byte_pointer) &x, sizeof(short int));
    show_bytes((byte_pointer) &mx, sizeof(short int));
/* $end show-twocomp */
}

int main(int argc, char *argv[])
{
    int val = 12345;

    if (argc > 1) {
      if (argv[1][0] == '0' && argv[1][1] == 'x')
 sscanf(argv[1]+2, "%x", &val);
      else
 sscanf(argv[1], "%d", &val);
      printf("calling test_show_bytes\n");
      test_show_bytes(val);
    } else {
      printf("calling show_twocomp\n");
      show_twocomp();
      printf("Calling simple_show\n");
      simple_show();
      printf("Calling float_eg\n");
      float_eg();
      printf("Calling string_eg\n");
      string_eg();
    }
    return 0;
}

http://csapp.cs.cmu.edu/

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有