getting character out of integer

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

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

getting character out of integer

Post by Ghoti »

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
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Re: getting character out of integer

Post by moonlight »

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.
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

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?
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

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

char inline integer2char(int i)
{
     return (char)(i + 0x60); 
}
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

another newbie question :(

Code: Select all

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?
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

:P

Code: Select all

sprintf(sBuffer, "%s %i %i", (char *)(iName[1] + 0x60), iName[2], iName[3]);
10011011 00101010 11010111 10001001 10111010
white rabbit
Posts: 60
Joined: Wed Jul 06, 2005 7:03 pm

Post by white rabbit »

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

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

int initials;
then pack this viable with the 3 characters and terminate it thus:

Code: Select all

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

printf&#40; "%s", &initals &#41;;
You may need to cast this to a char*:

Code: Select all

printf&#40; "5s", &#40;char*&#41;&initials &#41;;
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.
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

thanks for the reply,

only i use now :

Code: Select all

sprintf&#40;sBuffer, "Initials&#58; %c %c %c", iName&#91;1&#93; + 96, iName&#91;2&#93; + 96, iName&#91;3&#93; + 96&#41;;	// strlen gebruiken voor betere formatting
			
what's better? or faster?
white rabbit
Posts: 60
Joined: Wed Jul 06, 2005 7:03 pm

Post by white rabbit »

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.
Hexstr
Posts: 5
Joined: Tue May 23, 2006 5:07 am

Post by Hexstr »

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

Code: Select all

char initials&#91;4&#93;; // String to hold the initials and a null terminator

initials&#91;0&#93; = 'a'; // Initial 1
initials&#91;1&#93; = 'b'; // Initial 2
initials&#91;2&#93; = 'c'; // Initial 3
initials&#91;3&#93; = '\000'; // Null terminator

sprintf&#40;sBuffer, "Initials&#58; %s", initials&#41;; // Print out the initials
CyberBill
Posts: 86
Joined: Tue Jul 26, 2005 3:53 pm
Location: Redmond, WA

Post by CyberBill »

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.
Post Reply