【问题标题】:Get long from unsigned char* buffer via memcpy通过 memcpy 从 unsigned char* 缓冲区获取 long
【发布时间】:2020-07-26 22:59:09
【问题描述】:

在我用二进制 .exe 数据定义并填充缓冲区之后 --

unsigned char *buffer ;         /*buffer*/
buffer = malloc(300) ; /*allocate space on heap*/
fread(buffer, 300, 1, file) ;

那我如何获取缓冲区位置 121--124 的字节 作为长值?

我试过了

long Hint = 0;
memcpy(Hint,  buffer[121], 4);
printf("Hint=x%x\n", Hint);

但我得到的只是 memcpy 异常结束

【问题讨论】:

    标签: gcc memcpy


    【解决方案1】:

    这是一个简单的方法(我将数字放在缓冲区中作为示例):

    unsigned char *buffer ;         /*buffer*/
    buffer = (unsigned char*) malloc (300) ; /*allocate space on heap*/
    
    for(int i=0;i<300;i++) /*initialize buffer with numbers for the demo*/
        buffer[i] =  i;
    
    long Hint = 0;
    long *h = (long *)&buffer[121];
    Hint = *h;
    printf("Hint=0x%x\n", Hint);
    

    输出将是:

    Hint=0x7c7b7a79
    

    121-124 是十六进制数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      相关资源
      最近更新 更多