0

I intercept the WSASend call and print the contents of the buffer to the console using this code

void printBuffer(BYTE* buffer, int length)
{
    for (int i = 0; i < length; i++)
    {
        printf("%02X ", buffer[i]);
    }

    cout << endl;
}

int WSAAPI __WSASend(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesSent, DWORD dwFlags, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
{
    printBuffer((BYTE*)lpBuffers->buf, lpBuffers->len);
    return _WSASend(s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags, lpOverlapped, lpCompletionRoutine);
}

I want to be able to select in the console the data I need that was printed using the printBuffer and send it again. But for this I need to perform the conversion from what printf output to a char array to pass this to WSASend. How to do it?

Example:

EB 62 1C 99 = ыb∟Щ

AA ED C8 89 0C C1 F9 = кэ╚Й♀┴∙

I think you understand me..

πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183
  • `printf()` is used to print to a terminal, you cannot grab that output unless running another program which captures it (e.g. via a pipe). If you want to output stuff to character buffers for later reuse, use `std::stringstream` in c++. – πάντα ῥεῖ Dec 24 '20 at 19:13
  • @Allan That's clearly no c code! Or when did you use `cout` with c recently? – πάντα ῥεῖ Dec 24 '20 at 19:16
  • Yeah, my bad, I was paying too much attention to the printf. – Allan Wind Dec 24 '20 at 19:20
  • I need to convert from what printf output to the format that was originally in WSASend. – August Vishnevsky Dec 24 '20 at 19:22
  • Sorry, I don't still don't understand. I will leave my (misunderstood) answer up in case the comment helps others. The original data is still in your buffer variable. The only conversion I see is in printBuffer when you encode the data as 2 digit hex characters. You can do that same encoding with snprintf. If are you trying to the get an image (how the byte renders as a bitmap) then it's whole different thing. – Allan Wind Dec 24 '20 at 19:28
  • printf outputs in AA ED C8 89 0C C1 F9 format, I need to write a function that will convert these bytes to char – August Vishnevsky Dec 24 '20 at 19:38
  • https://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer ı think your answer is here. – qqq Dec 24 '20 at 20:40
  • Does this answer your question? [C++ convert hex string to signed integer](https://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer) – Brian Tompsett - 汤莱恩 Dec 25 '20 at 11:36

1 Answers1

1

You are looking for snprintf(3).

Allan Wind
  • 11,844
  • 4
  • 24
  • 32
  • No, I just need to convert what I have selected. – August Vishnevsky Dec 24 '20 at 19:05
  • 1
    Then I don't understand what you are asking including the two examples. Can you clarify the question? I am happy to delete my answer and give someone else a shot at it. – Allan Wind Dec 24 '20 at 19:07
  • The data is output to the console in the format AA ED C8 89 0C C1 F9. I want to be able to copy any line in the console and send the data in the original encoding. And for this I need to write a conversion algorithm from AA ED C8 89 0C C1 F9 to "кэ╚ ∙" – August Vishnevsky Dec 24 '20 at 19:11
  • WSASend(convert("AA ED C8 89 0C C1 F9"), ....); – August Vishnevsky Dec 24 '20 at 19:13
  • If you literally want to copy data from the console, then it's not a C++ question but specific to your platform. For instance, if you are using curses you might be able to retrieve what is displayed on the screen. What I was suggesting was to retain a copy of the data that you make with snprintf, then once you figure out the selecting you can resend the copy. – Allan Wind Dec 24 '20 at 19:13
  • @AllanWind I don't think the OP is asking about the copying process, it's the conversion process once the copy has been done. – john Dec 24 '20 at 19:18
  • I got what you mean. But I just need to implement an algorithm for converting from the format that printf outputs to the one that is passed to WSASend without any saving. I think the extra characters shouldn't get lost because printf outputs bytes. I just need to convert back, but I don't know how. – August Vishnevsky Dec 24 '20 at 19:18