1

I'm making a packet sender.

Where the user can input the packet they want to send on a RichTextBox.

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

Sample input:

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

The server should receive:

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

But the server is receiving this instead:

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 

How do I convert the data on the RichTextBox to remove all space and treat each as byte and send it.

This kind of approach works though:

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}

With that code, the server receives the correct data.

So how do I turn the Text inside the TextBox into something like that?


This works

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()); 
5
  • 1
    Not being up on CLI, I can't answer if the have a magic function to do this, I suppose they do, but in standard C++ I would istringstream the input string, then slurp out formatted data using operator >>(), pushing each value into a std::vector<uint8_t> and finally send vec.size() bytes from vec.data() Commented Apr 24, 2013 at 14:28
  • @WhozCraig This could work can I see some example code? :) Commented Apr 24, 2013 at 18:44
  • You're treating the data as decimal, but it's actually hex. Commented Apr 24, 2013 at 19:43
  • @BenVoigt can you please point out where I'm treating it as decimal and how I should treat it as hex? Commented Apr 24, 2013 at 20:06
  • 1
    @zikdaljin: It's reader >> number that assumes decimal. As I already said in my answer, you can use reader >> hex >> number instead. Commented Apr 24, 2013 at 20:42

2 Answers 2

1

Your function using istringstream is really close. To interpret the data as hexadecimal, you will need reader >> hex >> number.

And then

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

Or use

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);
Sign up to request clarification or add additional context in comments.

6 Comments

I got some error on Array::ConvertAll and on the split part.
@zikdaljin: Splitting the code up like this, what line is the error now?
the last line pkt now says cli::pin_ptr<unsigned char> is incompatible with parameter of type char*
@zikdaljin: Ok, it's a signed char vs unsigned char issue. I'll fix it.
Actually now it gives error on &bytes[0] signed char vs unsigned char on that one now. and Text->Split() gives error too. no instance of over loaded function.
|
0

You are sending the ASCII codes for the text "03 00 00 ....". You actually want to send the string "030000...", in its serialized mode.

std::string str("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");
sendbytes(str.c_str(), str.size());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.