10 years, 8 months ago.

print an Ascii

can someone help me how to increment an ASCII character? i feel dumb.

my code below.

char a = 'a';

   if (_ps3report->PressureCircle > 0 ) 
   {
            printf("output: %c ",  a ); 
   }
            a++;
           

I guess you figured out now that if should be while...

posted by Ad van der Weiden 14 Aug 2013

2 Answers

10 years, 8 months ago.

Hi kilo, your code looks fine, perhaps something else in the code is making it fail?, I probe this code to show in my lcd the first three letters and works fine:

#include "mbed.h"
#include "C12832_lcd.h"

C12832_LCD lcd;

int main() {
    char a='a';
    
    while(1) {
        lcd.printf("Character: %c\n",a);
        
        if(a=='c')
            break;
            
        a++;    
    }
}

Greetings

i tried your code, but the character didnt increment to B, C, D. it looped by printing only letter 'a'

posted by kilo byte 15 Aug 2013

Well, thats a little strange, the code works fine to me, can you post your complete code? in which platform youre developing?. Try to replace this line:

a++;

for this and see if that helps:

a=a+1;

Greetings

posted by Ney Palma 15 Aug 2013
10 years, 8 months ago.

You need to do somethinl like this, assuming you want to display a, b, c ..

..

char ascii = 'a';

// print out the 'a'
lcd.printf ("First time $c .. \r\n", ascii);

// increment value ..
ascii ++;

lcd.printf ("Inced by one .. %c", ascii);

lcd.printf ("Inced by one again .. %c", ++ascii);   // inc before use

lcd.printf ("Inced by one .. %c", ascii++);   // inc after use ..
lcd.printf ("Inced by one .. %c", ascii++);   // inc after use ..

hope that was usefull,

Ceri

i did some few changes on my code based on yours,

char a = 'a';
                  printf("output: %c \n", a); 

                 if (_ps3report->PressureCircle > 0 ) {
                 
                 a++;  

but output only displays

Mac address is set to 00:02:72:AD:F3:5B , result = 8
output: a
output: a
output: a
output: a
output: a
output: a
output: a
output: a
output: a
output: a

its not increasing or changing to B going Z, it just displays letter 'a'

posted by kilo byte 15 Aug 2013

The question is how you used it in your code. My guess is that it is a function called in response to USB input. So everytime a USB packet has arrived char a is set at 'a', and it is checked what kind of ps3report you got. If you want it to increment you need to define a globally if it is a C-style function, or if it is a class define it in your .h file as variable. In the constructor initiate it at 'a', and then you can happily increment it.

posted by Erik - 15 Aug 2013

Not sure, but I think (if you are using above code)

then a is always set to 'a' .. so it never gets to be tested after it gets incremented.

try putting define & load in MAIN, as global,

or set the value BEFORE program loop

posted by ceri clatworthy 15 Aug 2013