Welcome, Guest. Please login or register.

Author Topic: printf output to string possible ?  (Read 3163 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: printf output to string possible ?
« on: September 10, 2005, 12:44:32 PM »
Indeed sprintf is your friend. However sometimes you want to avoid linking whole stdio just for that. You might then want to use exec/RawDoFmt:

Code: [Select]

#include <proto/exec.h>
#include <proto/dos.h>

static const
UWORD stuffChar[] = {0x16c0, 0x4e75};

int main(void)
{
  UBYTE string[256];
  LONG array[2];

  array[0] = (LONG) &quot;Fish&quot;;
  array[1] = 2;

  RawDoFmt(&quot;%s have %ld eyes.&quot;, array, (void (*)(void)) &stuffChar, string);

  PutStr(string);
  PutStr(&quot;\n&quot;);

  return 0;
}


However, please note that the default width of the %d, %u, %x and %c is 16-bit, not 32-bit as with printf functions. So you must use %ld, %lu, %lx and %lc if your array is LONGs. RawDoFmt is also limited in some other ways compared to sprintf. For simple stuff RawDoFmt is fine, however.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: printf output to string possible ?
« Reply #1 on: September 10, 2005, 03:05:35 PM »
Quote
Wouldn't a simple cast to void work here? I don't get it, it's not even a function..

It isn't a function, but I want the compiler to believe it is (to avoid some warning).

Code: [Select]
static const
UWORD stuffChar[] = {0x16c0, 0x4e75};

Is really:
Code: [Select]

_stuffChar:
  move.b d0,(a3)+
  rts

It's much easier to use direct hexdump of the instructions than to try to get the compiler to produce something as simple.