_itoa question

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
cable16
Posts: 22
Joined: Tue Mar 22, 2005 9:43 am
Contact:

_itoa question

Post by cable16 »

I recently installed cygwin, pspsdk, and the toolchain, and I'm having some problems getting _itoa(int n, char *buf, int radix) from stdlib to work. There's some code I have that used the pspsdk beta, and I'm trying to get it to work under my cygwin install now. When the code used the beta, I manually added an itoa function to my headers, because the beta's stdlib didn't have a itoa function back then. Now, the code uses the sdk's function, and it doesn't work right.

this leaves tmp empty:

Code: Select all

#include <stdlib.h>

char *tmp = &freespace;
long long int offset = 33526363;

_itoa&#40;offset, &tmp, 16&#41;;
this will leave tmp as "1FF925B":

Code: Select all

// declared outright
char *test_itoa&#40;int n, char *buf, int radix&#41;
&#123;
  char         *ret = buf;
  char         tmp&#91;33&#93;;
  int          i = 0, j, r;

  /* validate the conversion number base. */
  if &#40;&#40;radix >= 2&#41; && &#40;radix <= 36&#41;&#41; &#123;
    if &#40;&#40;radix == 10&#41; && &#40;n < 0&#41;&#41; &#123;
      /* negative integer value. */
      *buf++ = '-';
      n = -n;
    &#125;
    do &#123;
      /* calculate the current digit. */
      r = &#40;int&#41;&#40;&#40;unsigned int&#41;n % radix&#41;;
      tmp&#91;i++&#93; = &#40;&#40;r < 10&#41; ? &#40;r + '0'&#41; &#58; &#40;r - 10 + 'a'&#41;&#41;;
    &#125; while &#40;&#40;n /= radix&#41; != 0&#41;;
    /* reverse the buffer string. */
    for &#40;--i, j = 0; &#40;i >= 0&#41;; --i, ++j&#41; buf&#91;j&#93; = tmp&#91;i&#93;;
    buf&#91;j&#93; = 0;
  &#125;
  return &#40;ret&#41;;
&#125;

char *tmp = &freespace;
long long int offset = 33526363;

test_itoa&#40;offset, &tmp, 16&#41;;
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: _itoa question

Post by Shine »

this leaves tmp empty:

Code: Select all

#include <stdlib.h>

char *tmp = &freespace;
long long int offset = 33526363;

_itoa&#40;offset, &tmp, 16&#41;;
use _itoa(offset, tmp, 16); and then buy and read some C learning book ;-)
cable16
Posts: 22
Joined: Tue Mar 22, 2005 9:43 am
Contact:

Post by cable16 »

ok, oops, that's a messup in the sample I posted, in the real code it uses like you said

Code: Select all

_itoa&#40;offset, tmp, 16&#41;;
The problem is, if I declare my own itoa function it will work correctly, but if I use the sdk's function in libpsplibc.a it won't work.

this doesn't work

Code: Select all

#include <stdlib.h>

int main&#40;void&#41;
&#123;
char *tmp = &freespace;
long long int offset = 33526363;

_itoa&#40;offset, tmp, 16&#41;;
&#125;
this does

Code: Select all

char *test_itoa&#40;int n, char *buf, int radix&#41;
&#123;
  char         *ret = buf;
  char         tmp&#91;33&#93;;
  int          i = 0, j, r;

  /* validate the conversion number base. */
  if &#40;&#40;radix >= 2&#41; && &#40;radix <= 36&#41;&#41; &#123;
    if &#40;&#40;radix == 10&#41; && &#40;n < 0&#41;&#41; &#123;
      /* negative integer value. */
      *buf++ = '-';
      n = -n;
    &#125;
    do &#123;
      /* calculate the current digit. */
      r = &#40;int&#41;&#40;&#40;unsigned int&#41;n % radix&#41;;
      tmp&#91;i++&#93; = &#40;&#40;r < 10&#41; ? &#40;r + '0'&#41; &#58; &#40;r - 10 + 'a'&#41;&#41;;
    &#125; while &#40;&#40;n /= radix&#41; != 0&#41;;
    /* reverse the buffer string. */
    for &#40;--i, j = 0; &#40;i >= 0&#41;; --i, ++j&#41; buf&#91;j&#93; = tmp&#91;i&#93;;
    buf&#91;j&#93; = 0;
  &#125;
  return &#40;ret&#41;;
&#125; 

int main&#40;void&#41;
&#123;
char *tmp = &freespace;
long long int offset = 33526363;

test_itoa&#40;offset, tmp, 16&#41;;
&#125;
Post Reply