C++分段拷贝字节

8 篇文章 0 订阅

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值