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

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

Moderators: cheriff, TyRaNiD

Post Reply
plebihan
Posts: 10
Joined: Sun Jul 16, 2006 11:29 pm

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

Post by plebihan »

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: Select all

char binary(int number){
	int remainder;
	char resultat[5];	
	if &#40;number <= 1&#41;
		&#123;
		sprintf&#40;resultat,"%X",remainder&#41;;	
		printf &#40;resultat&#41;;	
		return &#40;resultat&#41;;
		&#125;
	remainder = number%2;
	binary&#40;number >> 1&#41;;
	sprintf&#40;resultat,"%X",remainder&#41;;	
return &#40;resultat&#41;;
&#125; 
[/code]
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

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

Jim
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

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?
willow :--)
Posts: 107
Joined: Sat Jan 13, 2007 11:50 am

Post by willow :--) »

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 ;)
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

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.
plebihan
Posts: 10
Joined: Sun Jul 16, 2006 11:29 pm

Post by plebihan »

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

Thanx
Pacopad
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

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
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

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: Select all

svn co svn&#58;//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.
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

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.
plebihan
Posts: 10
Joined: Sun Jul 16, 2006 11:29 pm

Post by plebihan »

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
Alberto
Posts: 51
Joined: Mon Feb 12, 2007 8:16 pm
Location: Sofia

Post by Alberto »

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.
Alberto
Posts: 51
Joined: Mon Feb 12, 2007 8:16 pm
Location: Sofia

Post by Alberto »

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);
}
Post Reply