| View previous topic :: View next topic |
| Author |
Message |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
Posted: Mon May 22, 2006 4:49 pm Post subject: getting character out of integer |
|
|
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 |
|
 |
moonlight
Joined: 26 Oct 2005 Posts: 567
|
Posted: Mon May 22, 2006 4:54 pm Post subject: Re: getting character out of integer |
|
|
| 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 |
|
 |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
Posted: Mon May 22, 2006 4:58 pm Post subject: |
|
|
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 |
|
 |
moonlight
Joined: 26 Oct 2005 Posts: 567
|
Posted: Mon May 22, 2006 5:10 pm Post subject: |
|
|
| 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 |
|
 |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
Posted: Mon May 22, 2006 5:30 pm Post subject: |
|
|
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 |
|
 |
dot_blank

Joined: 28 Sep 2005 Posts: 498 Location: Brasil
|
Posted: Mon May 22, 2006 5:44 pm Post subject: |
|
|
:P
| Code: | | sprintf(sBuffer, "%s %i %i", (char *)(iName[1] + 0x60), iName[2], iName[3]); |
_________________ 10011011 00101010 11010111 10001001 10111010 |
|
| Back to top |
|
 |
white rabbit
Joined: 06 Jul 2005 Posts: 60
|
Posted: Mon May 22, 2006 5:49 pm Post subject: |
|
|
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: 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 |
|
 |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
Posted: Mon May 22, 2006 5:49 pm Post subject: |
|
|
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 |
|
 |
white rabbit
Joined: 06 Jul 2005 Posts: 60
|
Posted: Mon May 22, 2006 5:54 pm Post subject: |
|
|
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 |
|
 |
Hexstr
Joined: 23 May 2006 Posts: 5
|
Posted: Tue May 23, 2006 11:08 pm Post subject: |
|
|
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 |
|
 |
CyberBill
Joined: 26 Jul 2005 Posts: 86 Location: Redmond, WA
|
Posted: Wed May 24, 2006 7:18 am Post subject: |
|
|
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 |
|
 |
|