Like Tree3Likes

ASCII values

This is a discussion on ASCII values within the C Programming forums, part of the General Programming Boards category; Originally Posted by Matticus Though allowed by the standard, wouldn't such an example fall outside the scope of what is ...

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by Matticus View Post
    Though allowed by the standard, wouldn't such an example fall outside the scope of what is considered "ASCII" (as per the original question)?
    A good post on this is

    c - Multiple characters in a character constant - Stack Overflow

    Basically, the important key idea is that a character constant in C is defined as an integer. A char is simply an integer that is one byte wide. So the statement

    char magic_letter = 'a';

    will do an implicit typecast from (int) on the right to (char) on the left.

    An integer has an implementation-defined width and representation. Therefore, If I write this line in my code

    Code:
    static const int magic_code = 'XYZ6';
    the most likely result is that the string of characters "XYZ6" will appear somewhere in the binary output. Of course, the interpretation of these bytes as an integer is implementation defined, but there must be some mapping between character constants and integers.

  2. #17
    Registered User
    Join Date
    Jun 2011
    Posts
    2,242
    @c99tutorial - Thanks for the info, I understand that. I suppose my question had more to do with semantics. As I understand it, ASCII specifically refers to a 7-bit character encoding, which would (I imagine) not include multibyte values.

    I'm certainly no expert on this subject, but I've seen enough discussions on these forums to know that there's a distinct difference between the colloquial and official definitions of "ASCII." This is likely where my uncertainty lies.

    My answer in post #2 was in reply to the question: "...however I am wondering if it is possible to find the ASCII value of two character?" From a technical standpoint, taking the definition of "ASCII" into account, was my answer of "No" incorrect? It would be good for me to know if that were the case.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Matticus View Post
    @c99tutorial - Thanks for the info, I understand that. I suppose my question had more to do with semantics. As I understand it, ASCII specifically refers to a 7-bit character encoding, which would (I imagine) not include multibyte values.

    I'm certainly no expert on this subject, but I've seen enough discussions on these forums to know that there's a distinct difference between the colloquial and official definitions of "ASCII." This is likely where my uncertainty lies.

    My answer in post #2 was in reply to the question: "...however I am wondering if it is possible to find the ASCII value of two character?" From a technical standpoint, taking the definition of "ASCII" into account, was my answer of "No" incorrect? It would be good for me to know if that were the case.
    You are correct - 7 bit encoding. The "ascii" chars above 127 are a generic term for character sets that generally follow the same values as ascii. Arrow keys are multi-key values, for example.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Study your corrected substitution sort:

    Code:
    #include <stdio.h> 
    
    int main( void )
    {
       int num[5];
       int i,j;
       int temp;
         
       printf("enter 5 numbers\n");      
        
       for ( i=0;i<5; i++)             //in general use i<5, instead of i<=4 
          scanf( "%d", &num[i]);       //<= comparisons will trip you up
                                       //         {                      //???
       for (i=0; i<4; i++)
       {
          for (j=i+1; j<5; j++)
          {
             if(num[i] > num[j]) {
                temp = num[i];
                num[i] = num[j];
                num[j] = temp;
             }
          }   
       }
       
       for(i=0;i<5;i++)
          printf("%d ",num[i]);
       printf("\n");
       
       return 0; 
    }

  5. #20
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Thank you very much Adak, highly appreciate the help. I didnt think to look at it as using 5 instead of <=4. Been working on my project all day and final feel like I am making progress, now I just need to combine all the aspects together.
    Regards Dan

  6. #21
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I am able to get both my codes to show the ASCII values of input, and the bubble sort to work too, but I am stuck on how to get the bubble sort to sort the ASCII values. Any tips would be really helpful. I am guessing I have to make my ASCII equal a character e.g. a, then get the bubble sort to sort out a in an array. Would this the correct way?

  7. #22
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Correct me if I'm wrong, but here is the original question:

    Quote Originally Posted by SDH View Post
    For example I know that C is 43 and K is 4b, however would there be a value for CK if they were combined?

    thanks
    As stated prior there is no "combined" representational value that represents the fragment 'CK' as as single unit. However as you state you want to print ASCII values, you can easily print the ascii value of each character (separately):

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      const char * test_str = "CK";
      const char * p;
    
      for (p = test_str; *p; ++p)
          printf("%c = %d ",*p,*p);
      return putchar('\n');
    }
    As far as representing data as a single "value", there are several algorithms which can achieve this. Hashing or CRC come to mind. E.G. if you hash your string then you have a single "hash value" that represents that whole string, (not just a single letter) which when given the same string, will produce the same hash.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Since the ASCII values can be printed out in order (although some are dicey to print because they are used to control the monitor or are a non-printing character), I'm not sure why you'd want to sort them.

    Code:
    #include <stdio.h> 
    
    int main( void ) {
       int i;
          
       printf("\t\t*** My Character Set ***\n\n");
       
       for(i=33;i<128;i++) {
          if(i=='0' || i=='A' || i=='a')
             printf("\n\n");
          printf("%4d %c  ",i,i);
       }
       printf("\n");
    
       return 0; 
    }
    They simply "are". Below 33 are the control char's.
    Last edited by Adak; 01-09-2013 at 07:13 PM.

  9. #24
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Thanks for all the replies.

    As far as representing data as a single "value", there are several algorithms which can achieve this. Hashing or CRC come to mind. E.G. if you hash your string then you have a single "hash value" that represents that whole string, (not just a single letter) which when given the same string, will produce the same hash
    So would it be possible to then divide the hash by the value of one character of the string, so you would be able to work out the other character if it was a two character string.

    Since the ASCII values can be printed out in order (although some are dicey to print because they are used to control the monitor or are a non-printing character), I'm not sure why you'd want to sort them.
    I am trying to make a program which can score poker hands, I was hinted by my tutor that you need to work out the ASCII values of each cards, then rank them. From this you would be able to then determine which cards are present ( perhaps by division, and also how many of that card there is ) and then give the hand a score. Perhaps I am just going about it the long way.

    Thanks Dan

  10. #25
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by SDH View Post
    I am trying to make a program which can score poker hands, I was hinted by my tutor that you need to work out the ASCII values of each cards, then rank them.
    Playing cards don't have ASCII values. A playing card is an abstract notion which one might represent with a character sequence such as "9c" which, if we agreed upon it, stands for the playing card "nine of clubs". These two characters have the two ASCII values 57 ('9') and 99 ('c').

    There's no rule that says you have to represent cards as characters. How many possible cards are there?
    ace of clubs
    two of clubs
    three of clubs
    ...
    king of spades

    If you list them all, you just have integers. Let 0 stand for an invalid card, let 1 be the first possible card, 2 the second possible card, etc.

    #define CARD_INVALID (0)
    #define CARD_ACE_OF_CLUBS (1)
    #define CARD_TWO_OF_CLUBS (2)
    ...

    This is just one way to do it. Probably you should use an enum if you do it this way.

  11. #26
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Thank you very much for your advice, c99tutorial. Will give it a go now and see how I get on.

  12. #27
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I have just had a thought and is there a function that you can use to make the program to search for characters, for example make it search for multiplies of the same character? so would be able to search for 4 of a kind etc?

    Developing on your idea
    If you list them all, you just have integers. Let 0 stand for an invalid card, let 1 be the first possible card, 2 the second possible card, etc.
    if I used this method how would I state what hand is present, I think I could do straight and flush, but for three of a kind wouldnt there be so many different variations that could be possible?
    thanks

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by SDH View Post
    I have just had a thought and is there a function that you can use to make the program to search for characters, for example make it search for multiplies of the same character? so would be able to search for 4 of a kind etc?

    Developing on your idea
    if I used this method how would I state what hand is present, I think I could do straight and flush, but for three of a kind wouldnt there be so many different variations that could be possible?
    thanks
    There have been some clever schemes developed to handle just what you're doing. Several months back, someone posted about this question, and someone mentioned the plan for handling cards in a poker program.

    Naturally, I don't recall any of the specifics, but if you search for "poker programs" in the C forum, you should be able to find it.

    You could work out your own way, of course, but I'd suggest something based on numbers. Say you want to ID the 7's:

    Little digression first:
    If you printf("%d c",i,i); when i is 3,4,5 and 6, you'll see the chars for the 4 suits of the cards:3 is diamonds, 4 is hearts, 5 is clubs, and 6 is spades.

    So my idea is:
    73 = 7+3 becomes 7 of diamonds
    74 = 7+4 becomes 7 of hearts
    75 = 7+5 becomes 7 of clubs
    76 = 7+6 becomes 7 of spades.

    Jacks, being 110+N, (N=number designating their suit), Queens being 120+N, etc.

    Now mod 10 gives you the suit for any card, and dividing by 10 (integer division), will give you the card face.

  14. #29
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    If you really want to get creative you could use binary to store the card informtion. Use the lower 4 bits of a characer to encode the rank and the upper 3 bits to encode the suite. This would allow you to encode every card in the deck using a char. To make finding the suite you could use the following encoding of the high bits: 0000 Clubs, 0001 Diamonds, 0010 Hearts, and 0100 Spades.So the 6 of Hearts would be 00100111 and the Jack of Diamonds would be 00011100. By using masks you could then ealisly check the suite and the value of each card.

    Jim
    Matticus likes this.

  15. #30
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Hello, I am getting there. Just thought I would give you an update on where I am. I am using a linear search to search the input and it gives me how many times each input shows up. From this I am then going to use statements to declare the hands.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii values
    By caliber005 in forum C Programming
    Replies: 6
    Last Post: 05-18-2011, 03:16 PM
  2. ASCII values help
    By Grant_Searle in forum C Programming
    Replies: 5
    Last Post: 10-22-2010, 09:01 AM
  3. Using ASCII Values
    By ga836044 in forum C Programming
    Replies: 7
    Last Post: 03-17-2004, 12:31 AM
  4. adding ASCII values
    By watshamacalit in forum C Programming
    Replies: 1
    Last Post: 12-26-2002, 06:16 PM
  5. Testing for ASCII values?
    By csmatheng in forum C Programming
    Replies: 1
    Last Post: 02-19-2002, 01:22 PM

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