zoukankan      html  css  js  c++  java
  • C语言二进制拼接 (整数和byte类型的字符串拼接)

    #include <iostream>
    #include <cstring>
    #include <cstdio>

    using namespace std;

    typedef unsigned char Byte;

    Byte * intToBytes(const int& N) {

    Byte* byte = new Byte[4];

    byte[0] = (N >> 24) & 0xFF;
    byte[1] = (N >> 16) & 0xFF;
    byte[2] = (N >> 8) & 0xFF;
    byte[3] = N & 0xFF;

    return byte;
    }

    // 将一个byte数组、一个整数、一个整数以及一个byte数组组合为一个byte数组,并返回。
    Byte * Cat(Byte *a, int b, int c, Byte *d)
    {
    Byte* res = new Byte[12];
    memcpy(res, a, 2);// 2 为数组a的长度
    memcpy(res + 2, intToBytes(b), 4);// 2 为int的长度
    memcpy(res + 6, intToBytes(c), 4);
    memcpy(res + 10, d, 2);// 2 为数组a的长度
    return res;
    }

    int main()
    {
    Byte a[2]={253,27};
    int b=16;
    int c=18;
    Byte d[2]={222,92};

    Byte * res = Cat(a, b, c, d);
    cout<<res<<endl;
    // for (int i = 0; i < 12; i++)
    // printf("%c", res[i]);
    return 0;
    }

  • 相关阅读:
    P2572 [SCOI2010]序列操作
    python学习笔记2
    嗯,python
    ETROBOT——审题
    条件编译
    第三章单片机简介
    模拟输入输出
    arduino库函数1
    arduino相关文献阅读
    Arduino的小灯亮起来~~~
  • 原文地址:https://www.cnblogs.com/ruigelwang/p/12504168.html
Copyright © 2011-2022 走看看