Thread: ASCII value

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    6

    ASCII value

    Hello everyone
    I am new to this forum and I just started out with C. I just shifted to C from VB and I think it is going to take some time getting used to.

    Anyway my doubt is this. Does C have any in built functions to convert a char to its ascii code and store it in a int variable? If it does not have a function how can I get the ascii value?

    Any help would be greatly appreciated. Thanks in advance.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does C have any in built functions to convert a char to its ascii code and store it in a int variable?
    No, because C doesn't need such a function. char is just a small int anyway, so you can safely assign a char to an int:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char ch = 'a';
      int i = ch;
    
      printf ( "%c\t%d\n", ch, i );
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    If you want to do math with characters, you don't need to copy them out to ints. For example:
    Code:
    char ch = 'a';
    char ch2 = ch + 5;
    printf("The ASCII value of %c is %d\n", ch, ch);
    printf("The ASCII value of %c is %d\n", ch2, ch2);

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    6

    Thanks for the reply

    So that problem is sorted out, I seem to have run into another problem

    I am trying to read the data from a file using fread but I keep getting 0 bytes read although there is data in my file.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {
    	FILE *fp;
    	float a,b;
    	char buffer[100],c;
    	int i,bytesread;
    
    	if ((fp=fopen("E:\\test.txt", "rb")) == NULL)
    	{
    		printf("Unable to open the file\n");
    		exit(1);
    	}
    	
    	a = fseek(fp,0,SEEK_END);
    	printf("The value of a is:%d\n",a);
    	b = ftell(fp);
    	printf("The value of b is:%f\n",b);
    	
    
    	bytesread = fread(buffer,sizeof(char),10,fp);
    	printf("The total number of bytes read =%d\n",bytesread);
    
    }
    This is what I get as output
    The value of a is:0
    The value of b is:5.000000
    The total number of bytes read =0

    I tried using the getc function and writing it into a buffer, that seems to work fine but for some reason fread is not working, anyone have any idea as to what is wrong with my prog.
    Thanks

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void main()
    This is wrong. Read the FAQ.
    Code:
    	printf("The value of a is:%d\n",a);
    	b = ftell(fp);
    	printf("The value of b is:%f\n",b);
    What the hell is this supposed to do? Why are you using floating point numbers to store ftell and fseek return values, and then using %d to display it? In a word: Wrong.

    Of course you're not going to get anything from fread! You've just fseeked to the end of the file! And where'd you get the number 10 from? Just pull it out of your ass?


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

  6. #6
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Code:
       a = fseek(fp,0,SEEK_END);
    fseek() returns an int, but you have declared a as a float. What you are doing here is moving the file pointer to the very end of the file, and fseek() returns zero to indicate the operation succeeded.

    Code:
       b = ftell(fp);
    ftell() returns a long, but you have declared b as a float.

    Code:
       bytesread = fread(buffer,sizeof(char),10,fp);
    You moved the file pointer to the end of the file with your earlier fseek() and there is nothing to read beyond the end (here there be dragons!). That is why you're reading zero bytes.
    Jason Deckard

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Is it ok to have an array of different types? Can I do this?

    Code:
    unsigned int DumpHex(unsigned int start_addr,unsigned int end_addr)
    {
    
    DWORD iBytesWritten;
    DWORD iBytesRead;
    unsigned char *ucArray;
    char ch[25];
    unsigned int putin[15];
    
    	 putin[0]='D';
    	 putin[1]= ' ';
    	 putin[2]=start_addr;           //hex number Ex. 0xFFFF
    	 putin[3]= ' ';
    	 putin[4]=end_addr;
    }
    Last edited by NewGuy100; 08-15-2005 at 02:53 PM.

  8. #8
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by NewGuy100
    Is it ok to have an array of different types? Can I do this?
    Yes, all of those values will be automatically typecasted to an unsigned int value.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Ok thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM