3 Replies - 2127 Views - Last Post: 19 April 2013 - 10:55 AM Rate Topic: -----

#1 gordlonious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 27-March 13

Getting garbled char when doing ASCII math

Posted 18 April 2013 - 05:22 PM

Here's the code:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


int main(int argc, char **argv)

{
    char key[10];
    char text[10];

    key[0] = 'b';
    key[1] = 'a';
    key[2] = 'c';
    key[3] = 'o';
    key[4] = 'n';

    text[0] = 'h';
    text[1] = 'a';
    text[2] = 'm';
    text[3] = 'a';
    text[4] = 'n';

    //Declare variables necessary for encryption
    int placeofkey = 0;
    int placeoftext = 0;
    int cypher;

    cypher = key[placeofkey] + text[placeoftext];

    printf("%c", cypher);

    return 0;

}




is the cypher = key[placeofkey] + text[placeoftext]; not accessing the ASC11 value? If so, why not? Any help/hint is much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting garbled char when doing ASCII math

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,420
  • Joined: 25-December 09

Re: Getting garbled char when doing ASCII math

Posted 18 April 2013 - 08:51 PM

Quote

is the cypher = key[placeofkey] + text[placeoftext]; not accessing the ASC11 value?

This is adding the two characters 'b' + 'h' which is actually 98 + 104 or 202.

However your printf() function is using an incorrect format specifier. The "%c" specifier is only used for a char, not an int. The correct specifier would be "%d" to print the int cypher.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,010
  • Joined: 05-May 12

Re: Getting garbled char when doing ASCII math

Posted 18 April 2013 - 09:17 PM

As an aside: it is ASCII with two i's. Not with two 1's.
Was This Post Helpful? 1
  • +
  • -

#4 gordlonious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 27-March 13

Re: Getting garbled char when doing ASCII math

Posted 19 April 2013 - 10:55 AM

Thanks guys, that helped me isolate the problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1