Thread: Writing hex nums from console input to string

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    68

    Writing hex nums from console input to string

    Hey there

    Well, basically I have a server-client connection and want to send a packet trough console input stream. I want to type in hex what to send, so if I wanted to send a hello world to the server I'd write:
    Code:
    48656C6C6F20576F726C64 <- this into the console
    H e l l o   W o r l d  <- instead of that
    But how can I fill a memory location with 0x48 when I type "48", instead of 0x34 (4) and 0x38 (8)?

    And also, a bit offtopic, it's long time ago I programmed with sockets (in windows), when I use winsocks send(socket, buf, length, flag), will zero bytes within <length> bytes also be sent or will the packet truncated if it finds a zero termination?

    TIA, Hawk


    Edit: Main problem is, that I am unable to format many following hex-numbers with scanf, I want to send variable packet sizes.
    Last edited by Hawkin; 10-16-2007 at 12:41 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Hawkin View Post
    But how can I fill a memory location with 0x48 when I type "48", instead of 0x34 (4) and 0x38 (8)?
    You want to break the string into little groups of two digits each, and convert from hex representation. Try this:

    Code:
    unsigned int val;
    int scanned;
    do
    {
        scanned = sscanf(string, "%2x", &val);
        if(scanned == 1)
        {
            /* Go to the next pair of digits */
            string += 2;
            /* Successfully read a value -- do something with it */
        }
    } while(scanned == 1);
    And also, a bit offtopic, it's long time ago I programmed with sockets (in windows), when I use winsocks send(socket, buf, length, flag), will zero bytes within <length> bytes also be sent or will the packet truncated if it finds a zero termination?
    The zero byte is ONLY special for C strings and C string processing functions. You don't have to worry about it with send().

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Thank you, that was exactly what I needed

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Hawkin View Post
    Thank you, that was exactly what I needed
    Beware that the code I posted is NOT robust against errors. If you have whitespace in your string, it will break. If the string is not an even number of characters, it will break. If there is an invalid hexadecimal digit in the string, it will stop processing when it hits it.

    But hopefully it gives you the idea.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Yes I know (and experienced) that you can easily cause errors. But I only needed it and will need it for testing and debugging purposes. The original client has hardcoded packets and will change the content with normal string input and does check for several possible errors.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One way to make it a little more robust would be to use something like this:
    Code:
    int number, next = 0;
    const char *buffer = "48656C6C6F20576F726C64";
    
    while(sscanf(buffer, "&#37;2x%p", &number, &next) == 2) {
        /* process number */
        buffer += next;
    }
    That should handle whitespace properly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 01-18-2008, 04:14 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Writing a Variable as a string input to a function
    By gaza2k1 in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2007, 01:04 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM