Giter Club home page Giter Club logo

bytestream's People

Contributors

pabloalbiol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

bytestream's Issues

Structure. Fields are passed in, and fields that are not passed in are also passed into the byte stream

1.0 Question.

Structure real column . Problem with field pass in

#include "../ByteStream.h"
#include <iostream>
#include <cstring>
#include <endian.h>
using namespace std;

struct message
{
    // 50 +x
    char message_head[2];                   ///<  消息头  byte类型 @ < 
    uint16_t message_len_byteOrder;         ///<  流水账号到验证码的长度
    uint32_t serial_number_byteOrder;       ///<  流水号[0, +)
    uint16_t message_body_type_byteOrder;   ///<  消息体类型。0x0000:utf-8_Json 0x0001: 字节序列
    uint8_t  agreement_version_number;      ///<  协议版本号 0x01:版本1  0x02:版本2
    uint8_t  encryption_type;               ///<  加密类型,0x00:未加密  0x01:SM4 128国密
    uint32_t random_number_byteOrder;       ///<  随机数  不加密0x00 加密时
    uint64_t time_byteOrder;                ///<  unix时间戳   单位秒
    char reserve[24];                       ///<  预留备用  0x00 填充
    std::string message_body;                    ///<  消息体 字符串类型
    uint16_t check_code_byteOrder;          ///<  CRC16校验码
};


void show_uint8(unsigned char* name_, const unsigned int& len_)
{
    /*!
    * @brief 按字节打印
    * 0x20-0x7E打印字符,其他\x11显示
    */
    std::cout << len_ << "字节流方式打印: " << std::endl;
    for(int i=0; i<len_; i++){
        if(0x20 <= name_[i] && name_[i]<= 0x7E){
            std::cout <<  name_[i];
        }else{
            std::cout << "\\x" << std::hex << (int)name_[i];
        }
    }
    std::cout << std::endl << "打印结束" << std::endl;
}

int main(int argc, char *argv[])
{
    ByteStream stream(120);
    unsigned char buf[120];
    memset(buf,0,120);
    
    message sendMsg;
    sendMsg.message_head[0] = '@';
    sendMsg.message_head[1] = '<';
    sendMsg.message_len_byteOrder = htobe16(0x1616);
    sendMsg.serial_number_byteOrder = htobe32(0x32323232);
    sendMsg.message_body_type_byteOrder = htobe16(0x1616);
    sendMsg.agreement_version_number=0x08;
    sendMsg.encryption_type = 0x08;
    sendMsg.random_number_byteOrder=htobe32(0x32323232);
    sendMsg.time_byteOrder = htobe64(0x6464646464646464);
    memset(sendMsg.reserve, 0x08, 24);
    sendMsg.message_body="123456"s;
    sendMsg.check_code_byteOrder=htobe16(0x1213);

    stream << sendMsg.message_head ;
    stream << sendMsg.message_len_byteOrder ;
    stream << sendMsg.serial_number_byteOrder ;
    stream << sendMsg.message_body_type_byteOrder ;
    stream << sendMsg.agreement_version_number ;
    stream << sendMsg.encryption_type ;
    stream << sendMsg.random_number_byteOrder ;
    stream << sendMsg.time_byteOrder ;
    // stream << sendMsg.reserve ;
    // stream << (unsigned  char *)sendMsg.message_body.data();
    stream << sendMsg.check_code_byteOrder ;
    
    unsigned int length = stream.getBuf(buf);

    show_uint8(buf, length);
    return 0;
}

run

g++ demo01.cpp ../ByteStream.cpp -o main && ./main

79字节流方式打印: 
@<\x16\x162222\x16\x16\x8\x82222dddddddd\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\x8\xe0}Hm\xfe\x7f\x0\x16\x162222\x16\x16\x8\x82222dddddddd\x12\x13
打印结束

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.