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 

getting character out of integer

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



Joined: 31 Dec 2005
Posts: 288

PostPosted: Mon May 22, 2006 4:49 pm    Post subject: getting character out of integer Reply with quote

Hi folks,

when i have say an integer with value 26. I want some function to change this into the letter 'z' are there any standard libraries, functions, header files i could use for this or do i have to make them myself?

greets ghoti
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Mon May 22, 2006 4:54 pm    Post subject: Re: getting character out of integer Reply with quote

Ghoti wrote:
Hi folks,

when i have say an integer with value 26. I want some function to change this into the letter 'z' are there any standard libraries, functions, header files i could use for this or do i have to make them myself?

greets ghoti


What do you want? to use 1 like an 'a', 2 like a 'b' ...?

I don't see the sense of that, but it's very simple to implement it, you only have to add 0x40 to the number for capital letters, or 0x60 for non capital leters.
Back to top
View user's profile Send private message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Mon May 22, 2006 4:58 pm    Post subject: Reply with quote

well i'm trying to make an highscore field where you can input your 3 initials and i use an integer to hold which letter you choose but when i draw the screen I want to draw the letters instead of the integers.

sorry for sounding like an newbie but how do i attach 0x60 to an integer into an char? so that when i have an array with 3 integers i have a resulting char with 3 chars?
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Mon May 22, 2006 5:10 pm    Post subject: Reply with quote

Ghoti wrote:
well i'm trying to make an highscore field where you can input your 3 initials and i use an integer to hold which letter you choose but when i draw the screen I want to draw the letters instead of the integers.

sorry for sounding like an newbie but how do i attach 0x60 to an integer into an char? so that when i have an array with 3 integers i have a resulting char with 3 chars?


I hope you know that chars are simply integers, in the range of [-128, 127].

Maybe you want this:

Code:

char inline integer2char(int i)
{
     return (char)(i + 0x60);
}
Back to top
View user's profile Send private message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Mon May 22, 2006 5:30 pm    Post subject: Reply with quote

another newbie question :(

Code:
sprintf(sBuffer, "%s %i %i", (char)(iName[1] + 0x60), iName[2], iName[3]);   


i get with this the error that i assign an integer to %s but i cast it so how can i circumvent this?
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
dot_blank



Joined: 28 Sep 2005
Posts: 498
Location: Brasil

PostPosted: Mon May 22, 2006 5:44 pm    Post subject: Reply with quote

:P
Code:
sprintf(sBuffer, "%s %i %i", (char *)(iName[1] + 0x60), iName[2], iName[3]);

_________________
10011011 00101010 11010111 10001001 10111010
Back to top
View user's profile Send private message
white rabbit



Joined: 06 Jul 2005
Posts: 60

PostPosted: Mon May 22, 2006 5:49 pm    Post subject: Reply with quote

Use the ASCII table then you don't have to translate your letters to and from their already assigned integer values (google is your friend).

when you do
Code:
printf( "%s", string );
printf is expecting a pointer with a null terminater (a zero value at the end).

If you do some clever stuff you can store 4 characters in the space of an int: An int is 32 bits (4 bytes), and a char is 8 bits (1 byte). If you have a variable in your code called, say, inital:
Code:
int initials;
then pack this viable with the 3 characters and terminate it thus:
Code:

initals = firstInitialChar;
intials += secondInitialChar << 8;
initals += thirdInitialChar << 16;
initials += 0x0<< 24;

NB - this might be back to front (i.e. should be 4th (0x0) -> 1st instead of 1st -> 4th (0x0)), I have no psp compiler to test this with right now and I've not tried it for myself yet!

When you want to print this, you need to pass the address of your initals variable to the function like so:
Code:
printf( "%s", &initals );

You may need to cast this to a char*:
Code:
printf( "5s", (char*)&initials );


If you need to display characters with the first bit set (value over 127) you'll need to play around to see what your font does and what the PSP does.

Hope this helps.


Last edited by white rabbit on Mon May 22, 2006 5:50 pm; edited 1 time in total
Back to top
View user's profile Send private message
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Mon May 22, 2006 5:49 pm    Post subject: Reply with quote

thanks for the reply,

only i use now :

Code:
sprintf(sBuffer, "Initials: %c %c %c", iName[1] + 96, iName[2] + 96, iName[3] + 96);   // strlen gebruiken voor betere formatting
         

what's better? or faster?
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
white rabbit



Joined: 06 Jul 2005
Posts: 60

PostPosted: Mon May 22, 2006 5:54 pm    Post subject: Reply with quote

No idea which is faster, but I doubt there'd be much in it.

I'd say it's probably better you use your way: it will read easier when you come back to debug any code around there, and I'd say that's worth a few tenths of anybodies load time.
Back to top
View user's profile Send private message
Hexstr



Joined: 23 May 2006
Posts: 5

PostPosted: Tue May 23, 2006 11:08 pm    Post subject: Reply with quote

Ghoti, why don't you just use some code like this

Code:
char initials[4]; // String to hold the initials and a null terminator

initials[0] = 'a'; // Initial 1
initials[1] = 'b'; // Initial 2
initials[2] = 'c'; // Initial 3
initials[3] = '\000'; // Null terminator

sprintf(sBuffer, "Initials: %s", initials); // Print out the initials

_________________
Check out www.sf.net/projects/psp-tools
Back to top
View user's profile Send private message AIM Address
CyberBill



Joined: 26 Jul 2005
Posts: 86
Location: Redmond, WA

PostPosted: Wed May 24, 2006 7:18 am    Post subject: Reply with quote

initials[3] = '\000'; ???

Why three 0's?? You only need: '\0' or, better yet, just do:

initials[3] = 0;


to answer some other questions from above, there are times when you need to take 0-25 (or 1-26) as a letter in the alphabet, and the easiest way to do that is:

char mychar = 'A' + num; or (num-1).

This will translate any number (0 - 25) into A-Z. If you want lower case, just make 'a' instead of 'A'. Remember that characters are just numbers.
Back to top
View user's profile Send private message AIM Address MSN Messenger
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