Hex to binary conversion and storing in nybble

This is a discussion on Hex to binary conversion and storing in nybble within the C Programming forums, part of the General Programming Boards category; Hi guys, i am trying to convert a hex string into binary and store it in a seperate array. the ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    Hex to binary conversion and storing in nybble

    Hi guys,

    i am trying to convert a hex string into binary and store it in a seperate array.

    the hex characters must be stored in every nybble instead of every byte. thus reducing the size of the array.

    at the moment, i've managed to convert them from chars into hex format and stored them into an array. the next step would be to take the characters, chop them into nybbles and store two of them in every BYTE. at the moment, there is one hex character in every byte.

    could anyone help me with that problem?

    thanks alot

    im using VC++ and windows xp.

    my latest code is below

    Code:
    #include <windows.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void main ()
    
    
    {
    char streaminput[300] = "53720caea0e9d080c8280821b775ca286094e9d0402c1bfc50d4fbd700";   //sample string
    char f;
    char mybyte;
    char decode[300], copystring[300];
    int i; 
    i = 0;
    
    strcpy(copystring, streaminput);
    
    	while ( i<67 )  /* Walks buffer until NULL */
    	{
    
    		f = streaminput[i];
    switch( f )
    {
    	case '0':		mybyte = 0x00;
    				break;
    	case '1':		mybyte = 0x01;
    				break;
    	case '2':		mybyte = 0x02;
    				break;
    	case '3':		mybyte = 0x03;
    				break;
    	case '4':		mybyte = 0x04;
    				break;
    	case '5':		mybyte = 0x05;
    				break;
    	case '6':		mybyte = 0x06;
    				break;
    	case '7':		mybyte = 0x07;
    				break;
    	case '8':		mybyte = 0x08;
    				break;
    	case '9':		mybyte = 0x09;
    				break;
    	case 'a':
    	case 'A':		mybyte = 0x0A;
    				break;
    	case 'b':
    	case 'B':		mybyte = 0x0B;
    				break;
    	case 'c':
    	case 'C':		mybyte = 0x0C;
    				break;
    	case 'd':
    	case 'D':		mybyte = 0x0D;
    				break;
    	case 'e':
    	case 'E':		mybyte = 0x0E;
    				break;
    	case 'f':
    	case 'F':		mybyte = 0x0F;
    			                break;
    
    	strcat (decode, &mybyte);
    	i++;
    }
    
                            }
    }
    Last edited by shoobsie; 10-24-2005 at 04:06 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First and foremost, main is not a void function. It returns an int always. Next, what exactly is that strcat trying to do? It's wrong, whatever it is.

    You're looking for bit shifting. There's a FAQ on the topic.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by quzah
    First and foremost, main is not a void function. It returns an int always. Next, what exactly is that strcat trying to do? It's wrong, whatever it is.

    You're looking for bit shifting. There's a FAQ on the topic.


    Quzah.

    hi, strcat was supposed to add the latest binary nybble that will have been masked beforehand. it should follow on from the section im actually enquiring about!!

    sorry for confusing

    thanks for the pointer

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the point. strcat is for strings, all you have in mybyte is a single char. A single char, unless it's a null character only, is not a string, and cannot be a string. A string by definition is zero or more characters + one null character. So if you're using a single character as a string, it can only ever contain the value of a null character (zero).

    At any rate, bit shifting will do the trick for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I do not understand the goal of your program but I think the following code could be useful to you:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int Hex;
    
    	printf("Please enter a number in hexadecimal Ex. A4  : ");
    	scanf("%x", &Hex);
    
    	printf("0x%X is equal to %d.\n", Hex, Hex);
    
    	return 0;
    }
    My Output:

    Please enter a number in hexadecimal Ex. A4 : 1F
    0x1F is equal to 31

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    thanks brad, i managed to fix it with quzah's advice.

    i have a new problem that, now i want to add the resultant strings together but strcat isnt working for me, it keeps saying it cant convert between unsigned char pointers and so on....

    im using the following snippet of code but it gives me that error

    Code:
    unsigned char streaminput[300] = "53720caea0e9d080c8280821b775ca286094e9d0402c1bfc50d4fbd700";
    unsigned char f;
    unsigned char mybyte, mybyte1, nibble1, nibble2;
    unsigned char decode[2];
    
    char decoded[60];
    unsigned int k,l; 
    
    
    ........
    
    
    
    	k++;
    	nibble2 = mybyte1;	
    
    	decode[0] = nibble1 | nibble2;
    	decode[1] = '\0';
    
    	printf("%02x", decode[0]);
    
    	strcat(decoded, (char *) decode);
    
    	l++;
    }
    	printf("\n%02x\n", decoded);
    thanks everyone, so far

  7. #7
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    strcat isn't the most spectacular tool for this job. Why don't you just assign decode[0] (nibble1 | nibble2) straight into decoded[offset], then when you're finished, terminate decoded with a null character ('\0').

    Also, printf("%02x", decoded); makes no sense, decoded is a char array, perhaps you meant to iterate through it and show the hex value of each element? ie decoded[0], decoded[1], etc.?

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by cwr
    strcat isn't the most spectacular tool for this job. Why don't you just assign decode[0] (nibble1 | nibble2) straight into decoded[offset], then when you're finished, terminate decoded with a null character ('\0').

    Also, printf("%02x", decoded); makes no sense, decoded is a char array, perhaps you meant to iterate through it and show the hex value of each element? ie decoded[0], decoded[1], etc.?

    i just realised the first part of your answer!!! man, im stupid.

    i changed the decode char to an array because quzah said i couldnt use strcat on a char. but i guess your approach is so much simpler than mne.

    thanks!

    and the second part, i wanted to print the whole string. guess i need to check up on printf
    Last edited by shoobsie; 10-25-2005 at 02:14 AM.

  9. #9
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If you want to print the whole string as hex values, you'll need a for loop, unless you want to do:
    Code:
    printf("%02x %02x %02x ...", decoded[0], decoded[1], decoded[2], ...);
    Which is quite ridiculous.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by cwr
    If you want to print the whole string as hex values, you'll need a for loop, unless you want to do:
    Code:
    printf("%02x %02x %02x ...", decoded[0], decoded[1], decoded[2], ...);
    Which is quite ridiculous.
    i'd say that was pretty ridiculous, even for me!!

    i've gone for this approach so far, based on what you told me

    Code:
    	decode[0] = nibble1 | nibble2;
    	decode[1] = '\0';
    
    	decoded[l] = decode[0];
    
    	printf("%02x", decode[0]);
    
    	l++;
    }
    	printf("\n%x\n", decoded[0]);
    here its obviously printing out the first address of decoded and gives me "53", whci his perfect as its now storing two of these chars in the one address.

    thanks

  11. #11
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Right, but you no longer need the decode array:
    Code:
        decoded[l] = nibble1 | nibble2;
        printf("%02x", decoded[l]);
        l++;
    }
    printf("%x\n", decoded[0]);

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by cwr
    Right, but you no longer need the decode array:
    Code:
        decoded[l] = nibble1 | nibble2;
        printf("%02x", decoded[l]);
        l++;
    }
    printf("%x\n", decoded[0]);

    ahhh, yes!!

    thanks alot.....

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by shoobsie
    ahhh, yes!!

    thanks alot.....
    one last question, when i try to print

    Code:
     decode[6];
    or something higher, it gives me freaky outputs like
    0effff
    or something.....

  14. #14
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Most likely because you are mixing signed or unsigned, or something. Show your full code, if it's not too long.

    Edit: And I hope you mean decoded[6], not decode[6]? Have you got rid of the decode array?
    Last edited by cwr; 10-25-2005 at 02:40 AM.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by cwr
    Most likely because you are mixing signed or unsigned, or something. Show your full code, if it's not too long.

    Edit: And I hope you mean decoded[6], not decode[6]? Have you got rid of the decode array?
    yeah you were right!

    decoded was a char instead of bieng unsigned!

    thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 12:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21