Page 1 of 1

NXC: display a letter by it's ascii code

Posted: 27 Jun 2012, 15:27
by HaWe
hey yall,
how can I display a letter by it's ascii code like

Code: Select all

printf("%s", ascii(65)); // should display "A"
TextOut(0,56, ....(?)  );

Re: NXC: display a letter by it's ascii code

Posted: 27 Jun 2012, 15:46
by mattallen37
I use FlattenVar, like this:

Code: Select all

task main(){
  string ascii_char = FlattenVar(65);
  TextOut(0, 56, ascii_char);
  while(true);
}
I only used the string variable to prove that it is actually a string, but the example works just the same like this:

Code: Select all

task main(){
  TextOut(0, 56, FlattenVar(65));
  while(true);
}

Re: NXC: display a letter by it's ascii code

Posted: 27 Jun 2012, 17:29
by HaWe
that's perfect, thank you!
so we can say

Code: Select all

#define num2ascii(x) FlattenVar(x)
a pity, with printf it doesn't work directly

Code: Select all

task main(){
  TextOut(0, 48, FlattenVar(65));
  printf("%s",  FlattenVar(66));
  while(true);
}
(if we once will have stdio functions...? ^^)[/size]

Re: NXC: display a letter by it's ascii code

Posted: 29 Jun 2012, 04:55
by spillerrec
The C way of doing this is by accessing the character directly:

Code: Select all

string ascii = " ";
ascii[0] = 65;
If you are using formatted output, you can use %c and avoid the overhead of an array:

Code: Select all

task main(){
  TextOut(0, 48, FlattenVar(65));
  printf("%c",  66);
  while(true);
}
If it does not need to be variable, you would normally use:

Code: Select all

printf( "\x41" );
However the NXC compiler does not currently support this escape sequence.

Re: NXC: display a letter by it's ascii code

Posted: 30 Jun 2012, 12:22
by HaWe
yes, indeed, I remember that I even once read about that "%c" format string - but then I forgot about it.
It works fine, thanks!
(shooting with cannons at sparrows^^ )

Code: Select all

int _cur_x_=0, _cur_y_=56;

#define scrclr() {        \
  ClearScreen();          \
  _cur_x_=0; _cur_y_=56;  \
}

#define printfxy( x, y, _fmt, _val) {   \
   _cur_y_=y;                           \
  string sval1 = FormatNum(_fmt, _val); \
  TextOut(x, y, sval1);                 \
  _cur_x_=x+6*(strlen(sval1));          \
}

// like printf() in stdio.h
#define printfEx(_fmt, _val) {      \
  int x=_cur_x_; int y=_cur_y_;     \
  int posn, posf, slen, rlen;       \
  string rstr, lstr;                \
  slen=strlen(_fmt);                \
  posn=Pos("\n", _fmt);             \
  posf=Pos("%", _fmt);              \
  if (posn>=0) {                    \
    rlen=slen-posn-1;               \
    lstr= LeftStr(_fmt,posn);       \
    rstr= RightStr(_fmt,rlen);      \
    strcat(lstr,rstr);              \
    if(posn<posf) {                 \
      if(y>=8)y-=8; x=0;}           \
    printfxy(x,y,lstr,_val);        \
    if(posf<posn) {                 \
      if(_cur_y_>=8)                \
      _cur_y_-=8; _cur_x_=0;}       \
    }                               \
  else printfxy(x,y,_fmt,_val);     \
}


task main() {

  scrclr();
  for(int i=0; i<255; ++i) {       // (0...31 not displayed)
    printfEx("%3d", i);    printfEx(" %c ", i);
    printfEx("%3d", ++i);  printfEx(" %c ", i);
    if (_cur_y_==8)  printfEx(" <%d>", 1+i/16);
    if (_cur_y_==0) {
      printfEx("%s", " BTN");
      if (getchar()==BTNLEFT) {i=(i>=31?i-=32:111);}
      scrclr();
    }
    else printfEx("\n","");
    if (i>126) i=-1;
  }
  while(true);
}

Re: NXC: display a letter by it's ascii code

Posted: 02 Jul 2012, 11:25
by iplewis
Another way* is like this

Code: Select all

char variable;
string msg;

sprintf(msg, "%c", variable);
TextOut(x, y, msg, DRAW_OPT_NORMAL);

*Specifically, the way that I stumbled on after several unsuccessful attempts to do this a couple of weeks ago :D

Ian