C++:字符串和数字数组转换

#include <vector>
#include <string>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <stdint.h>

namespace msgBldr
{

    uint8_t hexToByte(const std::string from, const uint32_t offset)
    {
        // Isolate byte characters
        std::string byteChars = from.substr(offset, 2);

        // Use string to unsigned long conversion with base 16
        const uint8_t value = static_cast<uint8_t>(std::stoul(byteChars, nullptr, 16));

        return value;
    }


    void addData(std::vector<uint8_t>& msg,  const std::string from)
    {
        // Note: drops last character if not paired
        for (int i = 0; i < (from.size()/2); i++)
        {
            msg.insert(msg.end(), hexToByte(from, i*2));
        }
    }

    void addString(std::vector<uint8_t>& msg, const std::string from)
    {
        // copy includes null terminator
        msg.insert(msg.end(), from.begin(), from.end());
    }

    void msgPrint(std::vector<uint8_t>& msg, const std::string source)
    {
        msgPrint(&msg, source);
    }

    void msgPrint(std::vector<uint8_t>* msg, const std::string source)
    {
        std::stringstream stream;
        // message id
        stream << "test_ethR - len: " << msg->size() << "; ";
        stream << std::setfill('0') << std::setw(2) << std::hex;

        // data as hex bytes
        stream << std::setw(sizeof(uint8_t)*2);

        for (int i = 0; i < msg->size(); i++)
        {
            stream << +msg->at(i) << " ";
        }

        printf(ETHR_NOTE "%s: %s\n", ETHR_PARAM,
                source.c_str(), stream.str().c_str());
    }

}
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <unistd.h>

void consoleCallbackEthRClient(int argc, char ** argv)
{
    std::vector<uint8_t> msg;

    // Process command
    for (int i = 0; i < argc; i++)
    {
        alxLog::print(alxLog::INFO, "%s ",argv[i]);
    }
    alxLog::print(alxLog::INFO, "\n");

    if (strcmp(argv[0],"ethc") != 0) return;

    // Ignore comments starting with '#' or '/'
    if (argv[1][0] == '#') return;
    if (argv[1][0] == '/') return;

    if ((2 == argc) && (strcmp(argv[1],"name") == 0))
    {
        alxLog::print(alxLog::INFO, "EthR Test Client Name: %s\n",
                m.client->getName().c_str());
        alxLog::print(alxLog::INFO, "EthR Console Name: %s\n",
                m.name.c_str());
    }
    else if ((2 == argc) && (strcmp(argv[1],"terminate") == 0))
    {
        m.client->terminate();
    }
    else if ((4 == argc) &&
        (strcmp(argv[1],"delay") == 0) && (strcmp(argv[2],"ms") == 0))
    {
        if (argv[3] != nullptr)
        {
            int delay  = atoi(argv[3]);
            usleep(delay * 1000);
        }
        else
        {
            usleep(500 * 1000);
        }
    }
    else if ((4 == argc) &&
        (strcmp(argv[1],"delay") == 0) && (strcmp(argv[2],"sec") == 0))
    {
        if (argv[3] != nullptr)
        {
            int delay  = atoi(argv[3]);
            sleep(delay);
        }
        else
        {
            sleep(2);
        }
    }
    else if ((4 == argc) && (strcmp(argv[1],"subscribe") == 0))
    {
        if (strcmp(argv[2],"0") == 0) // unsubscribe
        {
            if (strcmp(argv[3],"1") == 0)
            {
               m.client->unsubscribe1();
            }
            else if (strcmp(argv[3],"2") == 0)
            {
                m.client->unsubscribe2();
            }
            else if (strcmp(argv[3],"3") == 0)
            {
                m.client->unsubscribe1();
                m.client->unsubscribe2();
            }
        }
        else if (strcmp(argv[2],"1") == 0) // subscribe
        {
            if (strcmp(argv[3],"1") == 0)
            {
               m.client->subscribe1();
            }
            else if (strcmp(argv[3],"2") == 0)
            {
                m.client->subscribe2();
            }
            else if (strcmp(argv[3],"3") == 0)
            {
                m.client->subscribe1();
                m.client->subscribe2();
            }
        }
    }
    else if ((3 < argc) &&
        (strcmp(argv[1],"auto") == 0) && (strcmp(argv[2],"run") == 0))
    {
        if (4 < argc)
        {
            m.autoOutputCount = atoi(argv[3]);
        }
        else
        {
            // default
            m.autoOutputCount = defaultOutputCount;
        }

        if (argv[4] != nullptr)
        {
            m.autoOutputDelayUs = atoi(argv[4]);
        }
        else
        {
            // default
            m.autoOutputDelayUs = defaultOutputDelayUs;
        }
    }
    else if ((3 == argc) &&
        (strcmp(argv[1],"auto") == 0) && (strcmp(argv[2],"stop") == 0))
    {
        m.autoOutputCount = 0;
        m.autoOutputDelayUs = defaultOutputDelayUs;
    }
    else if ((6 == argc) &&
            (strcmp(argv[1],"sendMsg") == 0) &&
            (strcmp(argv[2],"data") == 0))
    {
        alxLog::print(alxLog::INFO, "Message UDP data\n");
        msgBldr::setMsgId(msg, argv[4]);
        msgBldr::addData(msg, argv[5]);
        msgBldr::msgPrint(msg, __FUNCTION__);
        m.client->msgSend(argv[3], msg);
    }
    else if ((6 == argc) &&
            (strcmp(argv[1],"sendMsg") == 0) &&
            (strcmp(argv[2],"str") == 0))
    {
        alxLog::print(alxLog::INFO, "Message UDP str\n");
        msgBldr::setMsgId(msg, argv[4]);
        msgBldr::addString(msg, argv[5]);
        msgBldr::msgPrint(msg, __FUNCTION__);
        m.client->msgSend(argv[3], msg);
    }
    else if ((6 == argc) &&
            (strcmp(argv[1],"sendNotif") == 0) &&
            (strcmp(argv[2],"data") == 0))
    {
        alxLog::print(alxLog::INFO, "NotificationUDP data\n");
        msgBldr::setMsgId(msg, argv[4]);
        msgBldr::addData(msg, argv[5]);
        msgBldr::msgPrint(msg, __FUNCTION__);
        m.client->notifSend(argv[3], msg);
    }
    else if ((6 == argc) &&
            (strcmp(argv[1],"sendNotif") == 0) &&
            (strcmp(argv[2],"str") == 0))
    {
        alxLog::print(alxLog::INFO, "Notification UDP str\n");
        msgBldr::setMsgId(msg, argv[4]);
        msgBldr::addString(msg, argv[5]);
        msgBldr::msgPrint(msg, __FUNCTION__);
        m.client->notifSend(argv[3], msg);
    }
    else
    {
        alxLog::print(alxLog::INFO, "Unprocessed console command\n");
    }
}

猜你喜欢

转载自blog.csdn.net/xikangsoon/article/details/109397431
今日推荐