Code Monkey home page Code Monkey logo

Comments (1)

Naseband avatar Naseband commented on September 26, 2024

It's quite easy to do, you can use PR_BITS to get an arbitrary amount of bits and then format them as hex in a loop.

Note that raknet bitstreams are neither aligned nor padded. Meaning single bits (ie. bool) will cause the rest of the data to no longer be aligned with the half-byte or byte boundaries of the displayed hex (one single digit in hex represents 4 bits of data).
Without knowing the parameter list you cannot meaningfully format it, so keep that in mind.

I made this some time ago, you can use it if you still need it.
The code can be heavily simplified, but I wanted to add some readability in the output (and a simple display if the end if there are for example 3 bits leftover).

static hex_data[2048];
hex_data = "";

new hex_len = 0,
    tmp_text[6],
    total_bits,
    bits_left,
    character_count = 0;

BS_GetNumberOfBitsAllocated(bs, total_bits); // Use BS_GetNumberOfUnreadBits if you want to BS_IgnoreBits before this
bits_left = total_bits;

while(bits_left > 0)
{
    new read_len = bits_left > 4 ? 4 : bits_left,
        value;

    BS_ReadValue(bs, PR_BITS, value, read_len);
    bits_left -= read_len;

    if(read_len == 4)
        format(tmp_text, sizeof(tmp_text), "%x", value);
    else
        format(tmp_text, sizeof(tmp_text), "%x{%i}", value, read_len); // If not a full half-byte, display the number of bits (this only happens on the last chunk)

    if((++character_count) % 2 == 0) // Spacing, optional
        strcat(tmp_text, " ");

    if(hex_len + strlen(tmp_text) >= sizeof(hex_data) - sizeof(tmp_text)) // Cannot append more
    {
        strcat(hex_data, "...");
        break;
    }

    hex_len += strcat(hex_data, tmp_text);
}

BS_ResetReadPointer(bs); // Only required if you want to do something else with this bs handle.

printf("rpc %i [%i]: %s", rpcid, total_bits, hex_data);

from pawn.raknet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.