我有一个有效的hexdump函数,但打印单个变量似乎有点过分.所以我想为它制作一个宏.到目前为止,这几乎完美:
#define PRINT_FMT2(val,des,fmt) printf(#des" = "fmt": "SRC_STRN,val)
// SRC_STRN is a macro that evals to string literal containing file and line#
#include <stdint.h>
#define PRINT_HEX(x) PRINT_FMT2((*((uint32_t*)(&(x)))),x,"%x")
// the lack of parens around 2nd arg to PRINT_FMT2 is okay cause it gets #'d
Run Code Online (Sandbox Code Playgroud)
唯一的问题是我不能将它与函数的返回值一起使用.我收到了错误lvalue required as unary '&' operand
.我认为这意味着我不能使用这种方法将ptr转换为uint-ptr.还有另一种有效的方法吗?
编辑:我想提一提的是,大多数人掩盖的是,我想看看我的价值位保留,并转换为unsigned int类型十六进制格式.请注意,您可以使用具有l值的宏,float
并且它会吐出正确的big-endian表示.大多数常规的十六进制转储例程会在写入内存时打印出这些位,这显然取决于字节顺序.我不清楚我的意思是不好的.
无论如何,我最终编写了一组可以与r值一起使用的重载函数.最后我只想在32位浮点数上使用它.
inline uint32_t tohex(float f) {
uint32_t* u = (uint32_t *)&f; return *u;
}
Run Code Online (Sandbox Code Playgroud) 运行xxd 1.py
返回此输出:
jrfar /cygdrive/c/Users/jrfar/Documents/stack_overflow $
xxd 1.py
00000000: 776f 7264 735f 6e75 6d5f 6469 6374 203d words_num_dict =
00000010: 207b 2731 273a 276f 6e65 272c 2027 3227 {'1':'one', '2'
00000020: 3a27 7477 6f27 2c20 2733 273a 2774 6872 :'two', '3':'thr
00000030: 6565 272c 2027 3427 3a27 666f 7572 272c ee', '4':'four',
00000040: 200a 2735 273a 2766 6976 6527 7d0a 6e75 .'5':'five'}.nu
00000050: 6d20 3d20 696e 7428 696e 7075 7428 2745 m = int(input('E
00000060: 6e74 6572 2061 206e …
Run Code Online (Sandbox Code Playgroud) 我在hexdump代码中有一些数据.左手是DEC,右手是hexdump代码.
16 = 10
51 = 33
164 = A4 01
388 = 84 03
570 = BA 04
657 = 91 05
1025 = 81 08
246172 = 9C 83 0F
Run Code Online (Sandbox Code Playgroud)
如何计算到DEC的任何hexdump?在perl中,我尝试使用ord()命令但不起作用.
更新 我不知道它叫什么.它看起来像7bits数据.我尝试在excel中构建公式,如下所示:
DEC = hex2dec(X) + (128^1 * hex2dec(Y-1)) + (128^2 * hex2dec(Z-1)) + ...
Run Code Online (Sandbox Code Playgroud) 我在 C 中有一个代码,它只是打印 hello world,就像这样
#include <stdio.h>
int main(void)
{
printf("Hello, world\n");
}
Run Code Online (Sandbox Code Playgroud)
为了在 ubuntu 中编译代码,我使用了命令
make filename
,它给了我这样的汇编代码:
.text
.file "hello.c"
.globl main
.align 16, 0x90
.type main,@function
main: # @main
.cfi_startproc
# BB#0:
pushq %rbp
.Ltmp0:
.cfi_def_cfa_offset 16
.Ltmp1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
.Ltmp2:
.cfi_def_cfa_register %rbp
subq $16, %rsp
movabsq $.L.str, %rdi
movb $0, %al
callq printf
xorl %ecx, %ecx
movl %eax, -4(%rbp) # 4-byte Spill
movl %ecx, %eax
addq $16, %rsp
popq %rbp
retq
.Lfunc_end0: …
Run Code Online (Sandbox Code Playgroud)