forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

I'm a little lost, i need help to build a function

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
plebihan



Joined: 16 Jul 2006
Posts: 10

PostPosted: Thu Jul 26, 2007 5:11 am    Post subject: I'm a little lost, i need help to build a function Reply with quote

Hi all,

i would like to create a function that returns the binary representation of a decimal in a string. i tried many think but i didn't succed .

An other point i can't load the iostream header in /usr/include/pspdev/include/c++/4.1.0 cause it returns error. Is it the good file or must i search it in an other directory ?
Thanx for your help ,

Best regards

Here is a sample of my code :
Code:

char binary(int number){
   int remainder;
   char resultat[5];   
   if (number <= 1)
      {
      sprintf(resultat,"%X",remainder);   
      printf (resultat);   
      return (resultat);
      }
   remainder = number%2;
   binary(number >> 1);
   sprintf(resultat,"%X",remainder);   
return (resultat);
}
[/code]
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Thu Jul 26, 2007 12:41 pm    Post subject: Reply with quote

The PSP is almost certainly not the best platform to start learning to program C.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Art



Joined: 09 Nov 2005
Posts: 647

PostPosted: Thu Jul 26, 2007 1:01 pm    Post subject: Reply with quote

Jim wrote:
The PSP is almost certainly not the best platform to start learning to program C.

Jim


Really? I would have thought so... Is that for lack of official documentation?

What is?
Back to top
View user's profile Send private message
willow :--)



Joined: 13 Jan 2007
Posts: 126

PostPosted: Thu Jul 26, 2007 3:16 pm    Post subject: Reply with quote

Art wrote:
Jim wrote:
The PSP is almost certainly not the best platform to start learning to program C.

Jim


Really? I would have thought so... Is that for lack of official documentation?

What is?


Well C itself is not the easiest programming language, and you add to that an unusual environment with little debugging capacities and many more reasons for your application not to work...

Obviously it's easier to learn C on a Linux or Unix machine...

PS : use english for your variable names, try to avoid french, you never know, people might want to reuse your code one day ;)
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Thu Jul 26, 2007 3:26 pm    Post subject: Reply with quote

The remainder var isn't initialized in the case the if() code block is executed. This means that it will have an undefined value that will almost certainly cause a buffer overflow in the sprintf() as resultat is only 5 characters long, but the %X can generate up to eight characters.

You should define them as

int remainder = 0;
char resultat[9];

You need an extra char in resultat for the null terminating byte.
Back to top
View user's profile Send private message AIM Address
plebihan



Joined: 16 Jul 2006
Posts: 10

PostPosted: Thu Jul 26, 2007 3:53 pm    Post subject: Reply with quote

Ok thank you for your advices but what about the iostream header. Can it be used in PSP programmation ?

Thanx
Pacopad
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Thu Jul 26, 2007 6:07 pm    Post subject: Reply with quote

Why are you including iostream? Do you need it for some other part of your program? The code snippet you posted only needs stdio.h. Perhaps you're actually building a C++ program, or you're using the C compiler by mistake?

Good places to start with C and/or C++.
Windows:
DevC++
Visual Studio Express
Linux:
GCC

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Fri Jul 27, 2007 4:52 am    Post subject: Reply with quote

If you really insist on learning on the PSP, look at the example programs that come with the SDK. To check out the full SDK, use subversion (or a subversion client on Windows) to get it like this:

Code:
svn co svn://svn.ps2dev.org/psp/trunk psp


That will make a directory called psp in the current directory where you are which has all the different subdirectories from the psp sdk main trunk. You'll find various libraries for the PSP as well as the SDK itself. In the SDK directory will be examples.

You could also look at the various homebrew games and apps released as most of them come with the source code.

As others have mentioned, you're better off LEARNING c or c++ for linux or Windows first, then moving to c or c++ for PSP. It's much easier that way since the PSP is a really oddball cross-compiling environment. Learn c first, then c++. Most c++ books assume you know c well before even opening the c++ book as c++ is derived from c.
Back to top
View user's profile Send private message AIM Address
Art



Joined: 09 Nov 2005
Posts: 647

PostPosted: Fri Jul 27, 2007 9:45 am    Post subject: Reply with quote

Quote:
PS : use english for your variable names, try to avoid french, you never know, people might want to reuse your code one day ;)

tsnt s bd s yu thnk.
Back to top
View user's profile Send private message
plebihan



Joined: 16 Jul 2006
Posts: 10

PostPosted: Fri Jul 27, 2007 4:13 pm    Post subject: Reply with quote

Ok , with a little of change and some functions that i picked on the web my first hombrew begin to work. it's an app to control an IR helicopter "the picooz".. After some code lines , i could start the graphic part.

Thanx for you help
Pacopad
Back to top
View user's profile Send private message
Alberto



Joined: 12 Feb 2007
Posts: 57
Location: Sofia

PostPosted: Sat Jul 28, 2007 8:05 am    Post subject: Reply with quote

well, TTYTT I just think the following:

1. you're heading for a binary representation of a number, while you're using %X which is HEX representation...
2. your code has an IF statement, while it should have had a WHILE one...

Cheers, A.
Back to top
View user's profile Send private message
Alberto



Joined: 12 Feb 2007
Posts: 57
Location: Sofia

PostPosted: Sat Jul 28, 2007 9:46 am    Post subject: Reply with quote

So,

try like the following

#include <stdio.h>
#include <string.h>
#include <malloc.h>

void tobin(int i, char sBinary[])
{
char *sTemp = (char*)malloc(100);
strcpy(sTemp, "\0");
while (i)
{
if (i % 2)
strcat(sTemp, "1");
else
strcat(sTemp, "0");
i /= 2;
}
for(int n = 1; n <= strlen(sTemp); n++)
sBinary[n - 1] = sTemp[strlen(sTemp) - n];
sBinary[strlen(sTemp)] = '\0';
free(sTemp);
}

int main(void)
{
char s1[20];
tobin(0xAA55, s1); // 1010 1010 0101 0101
printf("%s\n", s1);
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group