c/c++语言开发共享将原始数据打印到固定长度的hex输出

我有一个结构,指向结构的指针,我希望将前n个字节作为长hex数字打印,或者作为hex字节的字符串。

基本上我需要printf等效的gdb的检查内存命令,x / nxb。

如果可能的话,我仍然希望使用printf作为程序的记录器function,只是它的变体。 如果我可以在不循环数据的情况下这样做,那就更好了。

    刚刚接受了Eric Postpischil的建议并制作了以下内容:

    struct mystruc { int a; char b; float c; }; int main(int argc, char** argv) { struct mystruc structVar={5,'a',3.9}; struct mystruc* strucPtr=&structVar; unsigned char* charPtr=(unsigned char*)strucPtr; int i; printf("structure size : %zu bytesn",sizeof(struct mystruc)); for(i=0;i 

    它将字节打印为结构拉伸时的fas。

    更新:感谢洞察Eric :)我已经更新了代码。

    试试这个。 假设您在pstruct有指向struct的指针。

     unsigned long long *aslong = (unsigned long long *)pstruct; printf("%08x%08x%08x%08x%08x%08x%08x%08x", aslong[0], aslong[1], aslong[2], aslong[3], aslong[4], aslong[5], aslong[6], aslong[7], ); 

    正如Eric指出的那样,这可能会无序地打印字节。 所以它或者是这个,或者使用unsigned char *和(使用带有64个参数的printf或使用循环)。

    需要了解更多c/c++开发分享将原始数据打印到固定长度的hex输出,也可以关注C/ C++技术分享栏目---计算机技术网(www.ctvol.com)!

      以上就是c/c++开发分享将原始数据打印到固定长度的hex输出相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/979092.html

      (0)
      上一篇 2021年12月12日
      下一篇 2021年12月12日

      精彩推荐