Thread: User inputs two characters and...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    20

    User inputs two characters and...

    I would like for my program to be able to give the binary values of these characters. I would then like to be able to work with these binary equivalents and be able to find the hamming distance between the two binaries, one's complement of them etc.

    I was thinking maybe if I can somehow figure out their binary equivalents, I can create two arrays which hold these binaries and then will be able to compare the arrays to find the hamming distance and manipulate the binaries in the array to print out the one's complement and such.

    Can anybody give me some hints on how to get started?

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Define "binary equivalent". Surely you don't mean
    Code:
    char x = 'a'; //the binary equivalent of the character a

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Basically it's 8 bit ASCII value equivalent in binary...

    'a' would be 01100001

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dre View Post
    Basically it's 8 bit ASCII value equivalent in binary...

    'a' would be 01100001
    So since it's essentially impossible for you to store the character in any other way, trying to convert into this form is futile.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Data stored in a variable (characters or otherwise) is already in a binary format. You can work with it as is. What makes 'a' show up as 'a' in a display on your screen is only a matter of representation of something that's already in binary. Conveniently enough, this wikipedia page has C code for computing hamming distance that can easily be converted to working with chars instead of ints.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Quote Originally Posted by hk_mp5kpdw View Post
    Data stored in a variable (characters or otherwise) is already in a binary format. You can work with it as is. What makes 'a' show up as 'a' in a display on your screen is only a matter of representation of something that's already in binary.
    Thanks. I read that yesterday and came up with this little code. I'm trying to get it to read the binary of the char and then store either an int 1 or 0 into an array i'm calling hamming3 and hamming4.

    Code:
    printf("\n\nEnter two characters: ");
    		scanf("%c %c",&char1, &char2);
    
    		for(i=0; i < 2; i++) {
    		printf("The bit pattern for byte %d is: ", (i+ 1));
            for(j=0; j < 8; j++) {
               
           
    			if (((*char1 & 1) != 1) && (i == 0)) {
                  
    
    		        printf("0");
                    ++bitCount0;
    				hamming3[k] = 0;
    				k++;
    	
    			}
    			
    
    			
    			if (((*char1 & 1) != 0) && (i == 0)) {
                    printf("%d", 1);
                    ++bitCount1;
    				hamming3[k] = 1;
    				k++;
    				
    			}
    			
    
    			if (((*char1 & 1) != 0) && (i == 1)) {
                    printf("%d", 1);	
                    ++bitCount1;
    				hamming4[l] = 1;
    				l++;
    			} 
    			
    			if (((*char1 & 1) == 0) && (i == 1)) {
                    (++bitCount0);
                    printf("%d", 0);
    				hamming4[l] = 0;
    				l++;
    			
    			}
              
                *char1 >>= 1;
            }
            
    		printf("\n");
            ++char1;
        }
    However the program crashes after I type in two characters all it says prints is "The bit pattern is for byte 1 is: and then crashes.

    It gives me the error:

    Unhandled exception at 0x004126f8 in testing0.exe: 0xC0000005: Access violation reading location 0xcccccc61.

    does anybody know why this is happening?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If char1 is a character, then *char1 is oh so wrong.

    Alternatively, if char1 is a pointer-to-character, then &char1 is oh so wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  2. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM
  5. Stopping a user from typeing.
    By knave in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2001, 12:21 PM