获取指定长度字节的数据并打印出来

一 . 获取指定长度字节的数据并打印出来

1.定义一个结构体

typedef struct _byte_3_context {
    char byte_1;
    char byte_2;
    char byte_3;
}byte_3_ctx;

2.获取到具体的字节数据

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"rar"];
    NSData *rarD = [NSData dataWithContentsOfFile:filePath];
    byte_3_ctx * rarP1 = (byte_3_ctx *)rarD.bytes;
    int c = [self intValueWithContext:rarP1];
    NSLog(@"%x", c);

// - 还可以修改某个字节的值
    char *offset = (char *)(rarD.bytes + 2);
    *offset = 0xff;

3.调用方法 将结构体内的数据转换成为一个 int 类型的整数

+(int)intValueWithContext:(byte_3_ctx *)ctx{
    NSLog(@"%d, %d", ctx->byte_1, (ctx->byte_1 << 2 * 8));
    int result = (ctx->byte_1 <<  2 * 8) + (ctx->byte_2 << 1 * 8) + (ctx->byte_3 << 0 * 8);
    return result;
}

4.查看转化前后的结果
rarD.bytes = 0x52, 0x61, 0x72, 0x21 …;
rarP1 = 0x52, 0x61, 0x72, 0x21 …;
*rarP1 = 0x726152;
c = 5398898 = 0x526172;

二 .仿CE 的内存搜索

char address[40] = {
        0x10, 0x11, 0x12, 0x13, 0x14,
        0x20, 0x21, 0x22, 0x23, 0x24,
        0x30, 0x31, 0x32, 0x33, 0x34,
        0x40, 0x41, 0x42, 0x43, 0x44,
        0x50, 0x51, 0x52, 0x53, 0x54,
        0x60, 0x61, 0x62, 0x63, 0x64,
        0x70, 0x71, 0x72, 0x73, 0x74,
        0x80, 0x81, 0x82, 0x83, 0x84,

    };
    char *p = address;
    for (int i = 0; i < 40; i++) {
        int *c = (int *)(p + i);
        if (*c == 0x84838281) {
            NSLog(@"%x", *c);
            break;
        }else{
            NSLog(@"%d : %x \n", i, *c);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_27074387/article/details/79806618