Search code examples
cglib

Sending hex values with g_socket_send in C


Is it possible in C to send a hex value via UDP, through a GSocket using g_socket_send? I don't necessarily need to do any conversions one way or the other, but even when I manually enter a hex string as the gchar buffer in g_socket_send, it is sent as an ASCII string.

Thank you in advance!


Solution

  • You are sending bytes. You need to figure out what the other end expects, and build those bytes accordingly. Say, if your receiver wants binary data, then this might be it:

    char buffer[4] = { 0xFF, 0x05, 0x01, 0xFE };
    /* ... */
    if ( g_socket_send( socket, buffer, 4, ... ) < 0 ) {
        /* handle error */
    }