ASCII Parity

This is a discussion on ASCII Parity within the C Programming forums, part of the General Programming Boards category; Hi I need to create a program where a single character is entered, and then outputted as ASCII code in ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    22

    ASCII Parity

    Hi

    I need to create a program where a single character is entered, and then outputted as ASCII code in hexadecimal.

    Each bit of the character needs to be examined so all the 1's can be counted. The parity bit most significant needs to be set to 1, if the number of odd bits is odd.

    The 8 bit code then needs to be outputted in hexadecimal.

    e.g. the output required needs to be or similar

    C7=L Hex=4C

    Can anyone point me in the right direction?

    Thanks

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What have you come up with so far?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    22
    Quote Originally Posted by claudiu View Post
    What have you come up with so far?
    Code:
    #include <stdio.h>#include <ctype.h>
    int ascii_value(char c);
    void main()
    {
    
    
     int i,a;
     char c;
     printf("Please enter a string.");
     for (i=0;(c=getchar())!=EOF;i++)
     {
      a=ascii_value(c);
      printf("%d%c",a,' ');
     }
     printf("are the ascii values of the characters of the entered string");
      }
    
    
     int ascii_value(char c)
     {
      int a;
      a=(int)c;
      return(a);
     }
    This is so far, but the outputs seem strange. Any suggestions?

    Thanks

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    getchar() returns an int, so don't try to store it in a char.
    printf() has a way to print an int value as hex. %d isn't that way, but I bet you the correct format string would be listed in the docs for that function.
    C can cast between different types without calling a function (but the solution to the first problem will eliminate the need for that altogether)

    Bit manipulations are generally better done using unsigned values, once you get to that point.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    22
    Quote Originally Posted by KCfromNC View Post
    getchar() returns an int, so don't try to store it in a char.
    printf() has a way to print an int value as hex. %d isn't that way, but I bet you the correct format string would be listed in the docs for that function.
    C can cast between different types without calling a function (but the solution to the first problem will eliminate the need for that altogether)

    Bit manipulations are generally better done using unsigned values, once you get to that point.
    Worked around the printf part, and replaced it with the nesesscary syntax for it to show a hexadecimal, however I think i'm required to do a more in depth check on the individual integers of the bit byte. Any suggestions?

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    343

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parity bit checker
    By krinl in forum C Programming
    Replies: 2
    Last Post: 09-28-2011, 12:57 PM
  2. Parity check ideas
    By newbie30 in forum C Programming
    Replies: 1
    Last Post: 11-23-2009, 02:36 PM
  3. A question about odd parity
    By xxrexdartxx in forum C Programming
    Replies: 6
    Last Post: 10-13-2009, 01:01 PM
  4. Parity Check Matrix
    By Ken JS in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2007, 03:51 AM
  5. Parity check
    By doshell in forum C Programming
    Replies: 22
    Last Post: 09-11-2006, 03:48 AM

Tags for this Thread


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