Thread: Convert string to char

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    1

    Convert string to char

    I have a character string as follows:

    D7E37B7B

    I want to take two characters at a time from the string and convert them to a character variable in hexadecimal format. For example, the 'D7' would be assigned to character variable 'c' as if I had done the following:

    unsigned char c = '\xD7';

    Thanks in advance

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It's a little more work, but you break up the string into individual chunks that you can work with and use strtol to convert to hexadecimal:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( void )
    {
      char buff[3] = {0};
      char *p = "D7E37B7BF";
      char *pos;
      int  i;
    
      for ( pos = p; *pos != '\0'; pos += i ) {
        i = ( pos[1] == '\0' ) ? 1 : 2;
        strncpy ( buff, pos, 2 );
        puts ( buff );
        printf ( "%d\n", (int)strtol ( buff, NULL, 16 ) );
      }
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    originally posted by prelude
    Code:
    char buff[3] = {0};
    I have noticed this alot in these forums why do people assign arrays to 0??

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > have noticed this alot in these forums why do people assign arrays to 0??
    It initializes every element in the array to 0. That way I can work with the array without worrying about a nul terminator for the string. I could get the same effect with
    Code:
    char buff[3];
    buff[2] = '\0';
    But that is too much like actual work.
    My best code is written with the delete key.

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    or malloc the array with calloc and let it zero it out for you.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Then you'd have to worry about freeing it
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM