Thread: Conversion of char for hexa!?!?

  1. #1
    Registered User
    Join Date
    Dec 2007

    Conversion of char for hexa!?!?

    Friends, I have a function that it prints an image from an variable of the type byte.

     byte xRPrint []  = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
     PrintImage((byte *)xRPrint, 5, 128, 8); //function print image
    until there all good

    only that I go to send the content of the variable of a server, for socket in one string, Ex:

    char xImage[] = "0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00"
    as I make to catch the content of xImage and to place in each position of xRPrint

    observing the value of string " 0x0F" and placing in the same position the referring value in hexa

    it forgives my English!


  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    You need to turn the string back into a bunch of numbers. The underlying bit pattern for a number is different than the pattern for a glyph like 'A' or 'B' (which are both hex digits, bear with me).

    There are C functions available to help you with your task. The easiest of which I can recommend is strtol. strtol, as implied by the name, takes a string and a radix, and attempts to convert the string data to a number representation.

    You should look it up and devise a method to help yourself.

    I made a demonstration for you, take a look with your debugger:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main ( void ) 
        const char * receev_me = "0xDE 0xAD 0xBE 0xAF 0x00";   /** some data **/
        char * receev_me_pos = NULL;
        unsigned long i = 0;
        unsigned char converted[16];  /** a big enough space to hold actual bytes**/
        int done = 0;                 /** loop control **/
        /** set to all bits zero **/
        memset( converted, '\0', sizeof converted );
        /** parse the hexa bytes with strtol **/
        while ( !done ) {
            unsigned char byt = ( unsigned char )strtol( receev_me, &receev_me_pos, 16 );
            /** strtol will point to where conversion ended. **/
            /** if the number was done sucessfully, try to store it. **/
            if ( receev_me_pos != receev_me && ( isspace( *receev_me_pos ) || *receev_me_pos == '\0' ) ) {
                if ( i >= sizeof converted ) {
                    fprintf( stderr, "No more room to store converted data!!!\n" );
                    done = !done;
                converted[i++] = byt;
                receev_me = receev_me_pos;
            else {
                /** normal completion - nothing to parse **/
                done = !done;
        return 0;
    This may or may not be exactly what you are looking for but I'm confident you'll go far with this help.

  3. #3
    Registered User
    Join Date
    Dec 2007

    Thumbs up

    accurate, it was this same, it correctly printed as img printed in the format
    byte [] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    tnx tnx tnx!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. Fixing Errors
    By brietje698 in forum Networking/Device Communication
    Replies: 9
    Last Post: 12-10-2007, 11:17 PM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM