GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Feb-2007, 09:23
ravi119's Avatar
ravi119 ravi119 is offline
New Member
 
Join Date: Nov 2006
Posts: 4
ravi119 is on a distinguished road
Exclamation

Convert Characters into Decimal value of ASCII


Hello Everyone

I need to convert Characters into Decimal values of ASCII in C like if I type A i need to have 65 and similarly if I type AA I need to add up them and get 130.
Please help me regarding this.

Thank you for your help in advance.
  #2  
Old 15-Feb-2007, 10:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Convert Characters into Decimal value of ASCII


Quote:
Originally Posted by ravi119
Hello Everyone

I need to convert Characters into Decimal values of ASCII in C like if I type A i need to have 65 and similarly if I type AA I need to add up them and get 130.
Please help me regarding this.

Thank you for your help in advance.

Internally they are the same. That is: a char is an integer data type. I/O routines do the conversions for you so that you can see characters rather than their decimal values. Your program does not need to convert anything.

In most systems these days the largest char value is 127, so you will probably want to use ints to hold the value of the arithmetic expressions.

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    char ca;
    char cb;
    int sum;

    printf("Type in two characters, then press 'Enter': ");
    scanf("%c%c", &ca, &cb);
    sum = ca + cb;

    printf("\nThe characters are:\n");
    printf("  First  character: '%c'\n", ca);
    printf("  Second character: '%c'\n\n", cb);
    printf("Decimal values: \n");
    printf("  First  character = %3d\n", ca);
    printf("  Second character = %3d\n", cb);
    printf("  Sum of the chars = %3d\n", sum);

    return 0;
}
Output:
Code:
Type in two characters, then press 'Enter': Ab The characters are: First character: 'A' Second character: 'b' Decimal values: First character = 65 Second character = 98 Sum of the chars = 163

The arithmetic is done with ints (this is automatic in C) and the value is stored as an int, so the user program doesn't have to "convert" anything.

Regards,

Dave
  #3  
Old 15-Feb-2007, 14:14
ravi119's Avatar
ravi119 ravi119 is offline
New Member
 
Join Date: Nov 2006
Posts: 4
ravi119 is on a distinguished road

Re: Convert Characters into Decimal value of ASCII


Hi Dave

Thank you for your Reply Dave. But I need arrays of Characters or strings to be converted into its decimals form. Like User may give any input like "a" or "abc" or "abcdef" or "abcAWe" and I need to sum all their decimal values and give output like 97 or 294 or 597.

Thank you.
  #4  
Old 15-Feb-2007, 17:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Convert Characters into Decimal value of ASCII


Quote:
Originally Posted by ravi119
Hi Dave

But I need arrays of Characters or strings to be converted into its decimals form.

Just add them up, as I indicated in my example. If you have a string of characters, then make a loop to step through the string and add each character as you go.

CPP / C++ / C Code:
    int i;
    int sum;
    char whatever[BUFSIZ]; /* a large constant */

    /* get something into the array */

    printf("<whatever>: <%s>\n", whatever);
    sum = 0;
    for (i = 0; i < whatever[i] != 0; i++) {
        sum += whatever[i];
    }
    printf("Sum = %d\n", sum);



Output might look something like:
Code:
Enter a string: This is a test <whatever>: <This is a test> Sum = 1269

Regards,

Dave
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary to Decimal Conversion Saberwing C Programming Language 5 28-Dec-2006 15:57
How to convert from Ascii to Hexadecimal in C program Kennice Low C Programming Language 2 29-Sep-2006 11:30
Roman to decimal to roman SpudNuts C++ Forum 2 16-Feb-2005 20:44
Problem with int mixed with char,... leitz C++ Forum 17 07-Dec-2004 21:56
Hex Result giving strange answers. Rosdahale C Programming Language 6 07-Dec-2004 21:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:45.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.