如何用可视化的C / C ++字节 [英] How to visualize bytes with C/C++

查看:135
本文介绍了如何用可视化的C / C ++字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一些C ++培训工作我的方式。到目前为止好,但我需要一些帮助加强一些我学习的概念。我的问题是我怎么去为可视化创建对象的字节模式。例如,我将如何打印出的字节模式结构,多头,整数等?

我的理解是在我的头上,并能理解我的学习材料的图表,我只是希望能够从内部我的一些研究项目programaticially显示字节模式。

我意识到这是pretty微不足道,但任何答案,将极大地帮助我锤了这些概念。

感谢。

编辑:我主要使用X code代表我的其他开发项目,但对虚拟机的Windows7和Fedora Core。在工作​​中我使用XP与Visual Studio 2005。
(我不能评论,因为我还在这里的n00b:D)

我用开卷的解决方案是什么,我期待的。我也想,也许我可以只使用DOS调试命令,我也想看看大块内存了。同样,这只是为了帮助我加强我所学习。再次感谢的人!


解决方案

您可以使用一个函数,如本,打印字节:

 无效print_bytes(常量无效*的对象,为size_t大小)
{
  const的无符号char * const的字节=对象;
  为size_t我;  的printf([);
  对于(i = 0; I<大小;我++)
  {
    的printf(%02X,字节[I]);
  }
  的printf(] \\ n);
}

用法是这样的,例如:

  INT X = 37;
浮Y = 3.14;print_bytes(安培; X,sizeof的X);
print_bytes(安培; Y,sizeof的Y);

此示出了字节只是作为原料的数值,以十六进制其通常用于存储器转储喜欢这些

在运行的英特尔(R)至强(R)CPU随机(甚至可能是虚拟的,就我所知)的Linux机器上,这将显示:


[25 00 00 00]
[C3 F5 48 40]

这轻而易举地也表明Intel系列CPU的:真的是小尾数 <。 / p>

I'm working my way through some C++ training. So far so good, but I need some help reinforcing some of the concepts I am learning. My question is how do I go about visualizing the byte patterns for objects I create. For example, how would I print out the byte pattern for structs, longs, ints etc?

I understand it in my head and can understand the diagrams in my study materials, I'd just like to be able to programaticially display byte patterns from within some of my study programs.

I realize this is pretty trivial but any answers would greatly help me hammer in these concepts.

Thanks.

Edit: I use mostly XCode for my other development projects, but have VMs for Windows7 and fedora core. At work I use XP with visual studio 2005. ( I can't comment as I am still a n00b here :D)

I used unwind's solution which is about what I am looking for. I am also thinking that maybe I could just use the dos DEBUG command as I'd also like to look at chunks for memory too. Again, this is just to help me reinforce what I am learning. Thanks again people!

解决方案

You can use a function such as this, to print the bytes:

void print_bytes(const void *object, size_t size)
{
  const unsigned char * const bytes = object;
  size_t i;

  printf("[ ");
  for(i = 0; i < size; i++)
  {
    printf("%02x ", bytes[i]);
  }
  printf("]\n");
}

Usage would look like this, for instance:

int x = 37;
float y = 3.14;

print_bytes(&x, sizeof x);
print_bytes(&y, sizeof y);

This shows the bytes just as raw numerical values, in hexadecimal which is commonly used for "memory dumps" like these.

On a random (might even be virtual, for all I know) Linux machine running a "Intel(R) Xeon(R)" CPU, this prints:

[ 25 00 00 00 ]
[ c3 f5 48 40 ]

This handily also demonstrates that the Intel family of CPU:s really are little endian.

这篇关于如何用可视化的C / C ++字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆