Thread: Character String to Hex Word

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Character String to Hex Word

    Hi Guys

    I wrote a simple program that takes in a string of 8 bytes representing a 32 bit 4byte hex word. The purpose of the function is to convert the string into a hex number, can someone please tell me what i am doing wrong, please!! ***hexstr_ptr is the character string holding the 8bytes representing a 32 bit hex word

    Code:
    	int nxt_char, cmdopt, cmdport, nibble0, nibble1;//, index;
    	const char hex_str[] = "0123456789ABCDEF";
    	hex_value = 0;
    	
    	for ( int j = 0; j <= 7; j++ ) {            //compare the two strings and convert string to hex
    		
    		for ( int i = 0; i <= 15; i++ ) { 
    	
    			if ( hex_str[i] == hexstr_ptr[j] ) {
    					
    				hex_value = ( hex_value << 4 ) | i;	
    
    				break;
    			}
    		}
    	}
    	
    	return ( hex_value );
    Last edited by asic_designer; 03-09-2011 at 10:29 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What's hexstr_ptr?

    And what's wrong with just using strtoul()?

    Code:
    unsigned long func(char *str)
    {
      return strtoul(str, NULL, 16);
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    please be aware that such comparisons are case sensitive so you couldn't convert "0123456789abcdef"
    Last edited by whiteflags; 03-09-2011 at 11:09 PM.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    I don't think I can use that function ( strtoul ) I don't have the library.


    Any suggestions on what I wrote, anyone?

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    It's in stdlib.h

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by asic_designer View Post
    I don't think I can use that function ( strtoul ) I don't have the library.


    Any suggestions on what I wrote, anyone?
    You're not creating the value correctly. You're not even using the nibble variable you declared.

    How about something like this?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    unsigned int hexchartovalue(char ch)
    {
      static char hexchars[] = "0123456789ABCDEF";
      char *p = strchr(hexchars, ch);
      if(!p)
      {
        fprintf(stderr, "Invalid hex digit: %c\n", ch);
        exit(EXIT_FAILURE);
      }
      return p - hexchars;
    }
    
    unsigned int str_to_hex(char *str)
    {
      unsigned int len = strlen(str);
      unsigned int nibble;
      char *s;
      unsigned int rv = 0;
    
      for(s = str + len - 1;s >= str;s -= 2)
      {
        nibble = hexchartovalue(*s);
        if(s > str)
          nibble |= hexchartovalue(*(s - 1)) << 4;
        rv |= nibble << (((len - (s - str)) / 2) * 8);
      }
    
      return rv;
    }
    
    int main(void)
    {
      printf("0x%X\n", str_to_hex("DEADBEEF"));
      return 0;
    }
    I get:
    Code:
    $ strtohex
    0xDEADBEEF
    EDIT: Just realized this was the C++ board. Oh well, not much has to change.
    Last edited by itsme86; 03-10-2011 at 10:42 AM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Hmmm, ok maybe I should have said this before but my compiler is very, very weird I can't use any of those "pre-compiled" functions that you called. Looking at your code I'm a little confused do you mind explaining what you did?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Not sure it has to be that confusing, I was able to run your code with the following minor corrections:
    Code:
        //int nxt_char, cmdopt, cmdport, nibble0, nibble1;//, index;
    	const char hex_str[] = "0123456789ABCDEF";
    	int hex_value = 0;
    You don't use anything on the first line anyway, and the only other thing is that the hexstr_ptr has to point to an UPPERCASE hex value.

    I printed my output like this:
    Code:
    #include <iomanip>
    std::cout<< std::hex << foo("DEADBEEF") << std::endl;
    foo is your function.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    OK so are you saying that, the code I have is convering the string correctly? I'm tring to debug a problem on an embedded system, so checking that it works in hardware is not is simple as using a debugger, this is why I'm trying to make sure that this code is working properly.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Are you so dense that you can't comprehend what I'm saying? Of course I'm saying I think it converts correctly. Please point out where I said it did not.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Look I have no idea who you think you are, but you probably get away with talking to people like this a lot on these boards because you get to hide behind your computer, but don't play the tough guy please, and watch who you talk to like that, because I just might show up at your mommy's house and kick your ass!! Yeah I said your mommy's house because you probably still live at home with her, still sipping that special milk, you little dork, talking tough behind a screen. Don't come at me like I'm some kind of idiot, you wouldn't last two seconds in my world, so watch your mouth, before you get your ass beat. It wouldn't be hard to track you down, you little ...........
    Last edited by asic_designer; 03-11-2011 at 02:36 PM.

  12. #12
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by whiteflags View Post
    please be aware that such comparisons are case sensitive so you couldn't convert "0123456789abcdef"
    Here you mentioned and i believe he/she took it in wrong way.......

    Quote Originally Posted by asic_designer View Post
    Look I have no idea who you think you are, but you probably get away with talking to people like this a lot on these board because you get to hide behind your computer, but don't play the tough guy please, and watch who you talk to like that, because I just might show up at your mommy's house and kick your ass!! Yeah I said your mommy's house because you probably still live at home with her, still sipping that special milk, you little dork, talking tough behind a screen. Don't come at me like I'm some kind of idiot, you wouldn't last two seconds in my world, so watch your mouth, before you get your ass beat. It wouldn't be hard to track you down, you little ...........
    Don't be hyper... He/she tried to help you and nothing else...... These forums are not to degrade anyone but to encourage to think themselves..... Don't be that personal.....
    Last edited by Mr.777; 03-10-2011 at 10:32 PM.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I just might show up at your mommy's house and kick your ass
    In my minds eye I see whiteflags slowly chewing a nail sandwich and washing it down the blood of demons.

    Soma ^_^

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Quote Originally Posted by Mr.777 View Post
    Here you mentioned and i believe he/she took it in wrong way.......


    Don't be hyper... He/she tried to help you and nothing else...... These forums are not to degrade anyone but to encourage to think themselves..... Don't be that personal.....
    No there is nothing OK about the way He/She/it/ responded, PERIOD!!!! NOTHING.. I want to drop this though, I don't want it to derail my post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM