C++分段拷贝字节

memcpy可以用来拷贝原资源到目标资源的指定长度的数据,但是不能只拷贝中间的一段数据。

如果拷贝中间一段的数据,此时可能就需要逐字节拷贝

void get_bytes(uint8_t* dst_bytes,uint8_t* source_bytes){
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 2; ++j) {
            *dst_bytes++ = *source_bytes+i*5+j+2;
        }
    }
}
    uint8_t source_bytes[]={1,2,3,4,5,
                            6,7,8,9,10,
                            11,12,13,14,15,
                            16,17,18,19,20};
    uint8_t* dst_bytes= static_cast<uint8_t *>(malloc(8 * sizeof(uint8_t)));
    get_bytes(dst_bytes,source_bytes);
    LOGD("source_bytes:");
    for (int i = 0; i < 20; ++i) {
        LOGD("%d",source_bytes[i]);
    }
    LOGD("dst_bytes:");
    for (int i = 0; i < 8; ++i) {
        LOGD("%d",dst_bytes[i]);
    }

log打印如下:

2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: ccccccccccccccc
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: source_bytes:
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 1
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 2
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 3
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 4
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 5
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 6
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 7
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 8
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 9
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 10
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 11
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 12
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 13
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 14
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 15
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 16
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 17
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 18
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 19
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: 20
2020-07-25 00:01:11.674 29026-29026/com.example.ndkapplication D/MyCTestLog: dst_bytes:
2020-07-25 00:01:11.675 29026-29026/com.example.ndkapplication D/MyCTestLog: 3
2020-07-25 00:01:11.675 29026-29026/com.example.ndkapplication D/MyCTestLog: 4
2020-07-25 00:01:11.675 29026-29026/com.example.ndkapplication D/MyCTestLog: 8
2020-07-25 00:01:11.675 29026-29026/com.example.ndkapplication D/MyCTestLog: 9
2020-07-25 00:01:11.675 29026-29026/com.example.ndkapplication D/MyCTestLog: 13
2020-07-25 00:01:11.675 29026-29026/com.example.ndkapplication D/MyCTestLog: 14
2020-07-25 00:01:11.675 29026-29026/com.example.ndkapplication D/MyCTestLog: 18
2020-07-25 00:01:11.675 29026-29026/com.example.ndkapplication D/MyCTestLog: 19

猜你喜欢

转载自blog.csdn.net/u013795543/article/details/107581976