Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:32:28+00:00 2026-06-04T18:32:28+00:00

This is what i have: std::string GetBytesAsHEX(const char *arr, int arr_size) { BYTE ch

  • 0

This is what i have:

std::string GetBytesAsHEX(const char *arr, int arr_size)
{
    BYTE ch = 0x00;
    char pseudo[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    std::string ret_val;

    for (int i = 0; i < arr_size; i++)
    {
        ch = (BYTE) (arr[i] & 0xF0);
        ch = (BYTE) (ch >> 4);
        ch = (BYTE) (ch & 0x0F);
        ret_val += pseudo[(int)ch];
        ch = (BYTE) (arr[i] & 0x0F);
        ret_val += pseudo[(int)ch];
        ret_val += ' ';
    }

    return ret_val;
}

int __stdcall Hooked_send(SOCKET s, const char *buf, int len, int flags)
{
    h_send.PreHook();
    //--------------

    int ret_val = send(s, buf, len, flags);

    if (LogPackets)
    {
        FILE *fptr = fopen("packet_log_hex.txt", "a");
        char header[128] = { 0 };
        sprintf(header, "\nSENT %i bytes: ", ret_val);
        fwrite(header, strlen(header), sizeof(char), fptr);
        fwrite(GetBytesAsHEX(buf, ret_val).c_str(), ret_val, sizeof(char), fptr);

        fclose(fptr);

        fptr = fopen("packet_log.txt", "ab");
        fwrite(buf, ret_val, sizeof(char), fptr);
        fclose(fptr);
    }

    //---------------
    h_send.PostHook();

    return ret_val;
}

int __stdcall Hooked_recv(SOCKET s, char *buf, int len, int flags)
{
    h_recv.PreHook(); //restore original recv address

    int ret_val = recv(s, buf, len, flags);

    if (ret_val > 0 && LogPackets)
    {
        FILE *fptr = fopen("packet_log_hex.txt", "a");
        char header[128] = { 0 };
        sprintf(header, "\nRECV %i bytes: ", ret_val);
        fwrite(header, strlen(header), sizeof(char), fptr);
        fwrite(GetBytesAsHEX(buf, ret_val).c_str(), ret_val, sizeof(char), fptr);

        fclose(fptr);

        fptr = fopen("packet_log.txt", "ab");
        fwrite(buf, ret_val, sizeof(char), fptr);
        fclose(fptr);
    }

    h_recv.PostHook(); //replace recv address with Hooked_recv

    return ret_val;
}

and this is what i get in file packet_log_hex.txt

SENT 16 bytes: 55 47 0C 00 00 0 //this is way not 16 bytes... and why so weird termination?
RECV 32 bytes: 55 47 1C 00 00 00 10 00 03 00 00
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 00 0
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 0B 0
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 10 F
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 C5 E

packet_log.txt (this one contain pure bytes) (copied from hex editor)

55 47 0C 00 00 00 00 00 02 00 00 00 01 00 03 02
55 47 1C 00 00 00 10 00 03 00 00 00 2D 04 00 00 50 07 F3 17 1A 37 34 48 81 D2 5E 13 73 21 37 A3
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 30 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 0B 00 00 00 12 00 00 00 40 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 10 FB 00 00 12 00 00 00 50 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 C5 EE 00 00 12 00 00 00 60 00 00 00

uhh, you see that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:32:30+00:00Added an answer on June 4, 2026 at 6:32 pm

    The ret_val you are using in the following line:

    fwrite(GetBytesAsHEX(buf, ret_val).c_str(), ret_val, sizeof(char), fptr);
    

    is the number of bytes you have received from the recv() call. This is not the number of bytes you need to write to the file after you have converted the received bytes to a hex string separated by spaces. Your call should probably be:

    std::string hex_str = GetBytesAsHEX(buf, ret_val);
    fwrite(hex_str.c_str(), hex_str.length(), sizeof(char), fptr);
    

    As for the weird termination, this is caused by the same thing. You can see that you are writing exactly 16 bytes in the first line by simply counting them, but it is 16 bytes of the converted string:

              111111
    0123456789012345
    
    55 47 0C 00 00 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this block of code: int myFunc( std::string &value ) { char buffer[fileSize];
I have this function that converts an integer to a std::string: std::string intToStr(const int
In my header file, I have this: std::string StringExtend(const std::string Source, const unsigned int
Suppose I have this class: class foo { public: foo() { } foo(const std::string&
I have this function in my program const char* Graph::toChar() { std::string str; const
I have this code: std::string name = kingfisher; char node_name[name.size()+1]; strcpy(node_name,name.c_str()); node_name[name.size()] = '\0';
I have this static std::string exec(char* cmd) { FILE* pipe = _popen(cmd, r); if
I have some code that looks like: static const std::string and( AND ); This
I have this code, int main() { std::string st; std::stringstream ss; ss<<hej hej med
I have this puny program. #include <iostream> #include <string> int main() { std::string st

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.