| View previous topic :: View next topic |
| Author |
Message |
plebihan
Joined: 16 Jul 2006 Posts: 10
|
Posted: Thu Jul 26, 2007 5:11 am Post subject: I'm a little lost, i need help to build a function |
|
|
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 |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Thu Jul 26, 2007 12:41 pm Post subject: |
|
|
The PSP is almost certainly not the best platform to start learning to program C.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Thu Jul 26, 2007 1:01 pm Post subject: |
|
|
| 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 |
|
 |
willow :--)
Joined: 13 Jan 2007 Posts: 126
|
Posted: Thu Jul 26, 2007 3:16 pm Post subject: |
|
|
| 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 |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Thu Jul 26, 2007 3:26 pm Post subject: |
|
|
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 |
|
 |
plebihan
Joined: 16 Jul 2006 Posts: 10
|
Posted: Thu Jul 26, 2007 3:53 pm Post subject: |
|
|
Ok thank you for your advices but what about the iostream header. Can it be used in PSP programmation ?
Thanx
Pacopad |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Thu Jul 26, 2007 6:07 pm Post subject: |
|
|
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 |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Fri Jul 27, 2007 4:52 am Post subject: |
|
|
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 |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Fri Jul 27, 2007 9:45 am Post subject: |
|
|
| 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 |
|
 |
plebihan
Joined: 16 Jul 2006 Posts: 10
|
Posted: Fri Jul 27, 2007 4:13 pm Post subject: |
|
|
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 |
|
 |
Alberto
Joined: 12 Feb 2007 Posts: 57 Location: Sofia
|
Posted: Sat Jul 28, 2007 8:05 am Post subject: |
|
|
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 |
|
 |
Alberto
Joined: 12 Feb 2007 Posts: 57 Location: Sofia
|
Posted: Sat Jul 28, 2007 9:46 am Post subject: |
|
|
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 |
|
 |
|