gpt4 book ai didi

c++ - 将字符串转换为字节并发送()它

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:50 25 4
gpt4 key购买 nike

我正在制作一个数据包发送器。

用户可以在 RichTextBox 上输入他们想要发送的数据包。

this->packetdata = (gcnew System::Windows::Forms::RichTextBox());

示例输入:

03 00 00 13 0e e0 00 00 00 00 00 01 00 08 00 00 00 00 00 03
00 00 6a 02 f0 80 7f 65 82 00 5e 04 01 01 04 01 01 01 01 ff

服务器应该接收:

03 00 00 13 0e e0 00 00 00 00 00 01 00 08 00 00 00 00 00 03
00 00 6a 02 f0 80 7f 65 82 00 5e 04 01 01 04 01 01 01 01 ff

但是服务器收到的是:

30 33 20 30 30 20 30 30 20 31 33 20 30 65 20 65   03 00 00 13 0e e
30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 0 00 00 00 00 00
20 30 31 20 30 30 20 30 38 20 30 30 20 30 30 20 01 00 08 00 00
30 30 20 30 30 20 30 30 20 30 33 0a 30 30 20 30 00 00 00 03.00 0
30 20 36 61 20 30 32 20 66 30 20 38 30 20 37 66 0 6a 02 f0 80 7f
20 36 35 20 38 32 20 30 30 20 35 65 20 30 34 20 65 82 00 5e 04
30 31 20 30 31 20 30 34 20 30 31 20 30 31 20 30 01 01 04 01 01 0
31 20 30 31 20 66 66 20 1 01 ff

如何转换 RichTextBox 上的数据以删除所有空格并将每个空格视为字节并发送。

这种方法虽然有效:

char this[] = {0x03, 0x00, 0x00, 0x13, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03
0x00, 0x00, 0x6a, 0x02, 0xf0, 0x80, 0x7f, 0x65, 0x82, 0x00, 0x5e, 0x04, 0x01, 0x01, 0x04, 0x01, 0x01, 0x01, 0x01, 0xff}

使用该代码,服务器接收到正确的数据。

那么如何将 TextBox 中的 Text 变成类似的东西呢?


这行得通

int mysendfunc(char *sendbuf, int size);

(...)

std::string inputpkt = marshal_as<std::string>(this->packetdata->Text);
std::istringstream reader(inputpkt);
std::vector<char> pkt;
do
{
// read as many numbers as possible.
for (int number; reader >> std::hex >> number;) {
pkt.push_back(number);
}
// consume and discard token from stream.
if (reader.fail())
{
reader.clear();
std::string token;
reader >> token;
}
}
while (!reader.eof());

int hehe = mysendfunc(pkt.data(), pkt.size());

最佳答案

您使用 istringstream 的功能非常接近。要将数据解释为十六进制,您需要 reader >> hex >> number

然后

mysendfunc(&pkt[0], pkt.size())

或者使用

array<String^>^ hexCodes = this->packetdata->Text->Split(" ");
Converter<String^, SByte>^ parser = gcnew Converter<String^, SByte>(&SByte::Parse);
array<SByte>^ bytes = Array::ConvertAll(hexCodes, parser);
pin_ptr<char> pkt = &bytes[0];
int x = mysendfunc(pkt, bytes->Length);

关于c++ - 将字符串转换为字节并发送()它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16189825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com