使用c中的十六进制地址获取值

时间:2018-06-26 04:46:04

标签: c hex

这是我的代码:

 File "/usr/lib/python2.7/site-packages/dask/dataframe/core.py", line 1091, in to_csv
    return to_csv(self, filename, **kwargs)
  File "/usr/lib/python2.7/site-packages/dask/dataframe/io/csv.py", line 577, in to_csv
    delayed(values).compute(get=get, scheduler=scheduler)
  File "/usr/lib/python2.7/site-packages/dask/base.py", line 156, in compute
    (result,) = compute(self, traverse=False, **kwargs)
  File "/usr/lib/python2.7/site-packages/dask/base.py", line 400, in compute
    results = schedule(dsk, keys, **kwargs)
  File "/usr/lib/python2.7/site-packages/distributed/client.py", line 2159, in get
    direct=direct)
  File "/usr/lib/python2.7/site-packages/distributed/client.py", line 1562, in gather
    asynchronous=asynchronous)
  File "/usr/lib/python2.7/site-packages/distributed/client.py", line 652, in sync
    return sync(self.loop, func, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/distributed/utils.py", line 275, in sync
    six.reraise(*error[0])
  File "/usr/lib/python2.7/site-packages/distributed/utils.py", line 260, in f
    result[0] = yield make_coro()
  File "/usr/lib64/python2.7/site-packages/tornado/gen.py", line 1099, in run
    value = future.result()
  File "/usr/lib64/python2.7/site-packages/tornado/concurrent.py", line 260, in result
    raise_exc_info(self._exc_info)
  File "/usr/lib64/python2.7/site-packages/tornado/gen.py", line 1107, in run
    yielded = self.gen.throw(*exc_info)
  File "/usr/lib/python2.7/site-packages/distributed/client.py", line 1439, in _gather
    traceback)
  File "/usr/lib/python2.7/site-packages/dask/dataframe/io/csv.py", line 439, in _to_csv_chunk
    df.to_csv(f, **kwargs)
  File "/usr/lib64/python2.7/site-packages/pandas/core/frame.py", line 1745, in to_csv
    formatter.save()
  File "/usr/lib64/python2.7/site-packages/pandas/io/formats/csvs.py", line 161, in save
    buf = f.getvalue()
  File "/usr/lib/python2.7/site-packages/dask/bytes/utils.py", line 136, in __getattr__
    return getattr(self.file, key)
AttributeError: 'S3File' object has no attribute 'getvalue'

======输出=====

#include <stdio.h>

void getTheValue();

int main() {
    char *address;
    char num[2] = "ab";

    address = (char *)num;
    printf("Address of num: %p\n", address);

    getTheValue();
    getchar();
    return 0;
}

void getTheValue() {
    unsigned char address[11];
    printf("Enter the address: ");
    scanf("%s", &address);
    printf("\naddress = %s", address);
    printf("\n");
    unsigned char *ptr
    ptr = (char *)(address);
    printf("The value at address = %c", *ptr);
}

为什么我会得到值 0 。我想要 num 的值。

请给我一些建议或发送一些链接,使我可以对此有所了解。

2 个答案:

答案 0 :(得分:3)

您的程序不会将读取的字符串转换为指针值。您只是在打印用户键入的第一个字符。

您可以使用scanf("%p", &p);从流中读取指针值:

  

7.21.6.2 fscanf函数

     

...

     

p匹配实现定义的序列集,该序列集应与fprintf函数的%p转换可能产生的序列集相同。相应的参数应为指向void的指针。输入项以实现定义的方式转换为指针值。如果输入项是在同一程序执行期间较早转换的值,则结果指针应等于该值;否则%p转换的行为是不确定的。

这是程序的修改版本:

#include <stdio.h>

void getTheValue(void);

int main() {
    void *address;
    char num[] = "ab";

    address = num;
    printf("Address of num: %p\n", address);

    getTheValue();

    getchar();
    return 0;
}

void getTheValue(void) {
    void *p;
    printf("Enter the address: ");
    scanf("%p", &p);
    printf("address = %p\n", p);
    unsigned char *ptr;
    ptr = p;
    printf("The value at address = `%c`, decimal %d, hex 0x%02x\n", *ptr, *ptr, *ptr);
}

踪迹:

~/dev/stackoverflow > ./peek
Address of num: 0x7fff5101e94c
Enter the address: 0x7fff5101e94d
address = 0x7fff5101e94d
The value at address = `b`, decimal 98, hex 0x62

答案 1 :(得分:1)

我使用strtol使它起作用。您的“值”只是地址中字符串“ 0x ....”的第一个字符-而不是首先将整个字符串转换为整数-并使用该整数作为地址。

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void getTheValue();

int main() {
  char* address;
  char num[3] = "ab";

  address = (char *)num;
  printf("Address of num: %p\n", address);

  getTheValue();
  getchar();
  return 0;
}

void getTheValue() {
  char address[11];
  printf("Enter the address: ");
  scanf("%s", &address);


  char *ptr = (char *)strtol(address, 0, 16);
  printf("The value at address = %c", *ptr);
 }

然后ptr =“ ab” ---请注意,但是我没有在输入中键入0x。

enter image description here