getting character out of integer
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
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
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Re: getting character out of integer
What do you want? to use 1 like an 'a', 2 like a 'b' ...?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
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.
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?
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
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
I hope you know that chars are simply integers, in the range of [-128, 127].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?
Maybe you want this:
Code: Select all
char inline integer2char(int i)
{
return (char)(i + 0x60);
}
another newbie question :(
i get with this the error that i assign an integer to %s but i cast it so how can i circumvent this?
Code: Select all
sprintf(sBuffer, "%s %i %i", (char)(iName[1] + 0x60), iName[2], iName[3]);
My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
:P
Code: Select all
sprintf(sBuffer, "%s %i %i", (char *)(iName[1] + 0x60), iName[2], iName[3]);
10011011 00101010 11010111 10001001 10111010
-
- Posts: 60
- Joined: Wed Jul 06, 2005 7:03 pm
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 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:
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:
You may need to cast this to a char*:
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.
when you do
Code: Select all
printf( "%s", string );
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: Select all
int initials;
Code: Select all
initals = firstInitialChar;
intials += secondInitialChar << 8;
initals += thirdInitialChar << 16;
initials += 0x0<< 24;
When you want to print this, you need to pass the address of your initals variable to the function like so:
Code: Select all
printf( "%s", &initals );
Code: Select all
printf( "5s", (char*)&initials );
Hope this helps.
Last edited by white rabbit on Mon May 22, 2006 5:50 pm, edited 1 time in total.
thanks for the reply,
only i use now :
what's better? or faster?
only i use now :
Code: Select all
sprintf(sBuffer, "Initials: %c %c %c", iName[1] + 96, iName[2] + 96, iName[3] + 96); // strlen gebruiken voor betere formatting
My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
-
- Posts: 60
- Joined: Wed Jul 06, 2005 7:03 pm
Ghoti, why don't you just use some code like this
Code: Select all
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
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.
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.