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 

Int to Char

 
Post new topic   This topic is locked: you cannot edit posts or make replies.    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Sonicadvance1



Joined: 13 Oct 2005
Posts: 10

PostPosted: Thu Oct 13, 2005 8:39 am    Post subject: Int to Char Reply with quote

I'm trying to change a int to char as the title suggests.I'v been getting on fine up untill now but I can't seem to get a function to work that changes an int to char.Think anyone can help me out with this?
Back to top
View user's profile Send private message
ChaosKnight



Joined: 14 Apr 2005
Posts: 142
Location: Florida, USA

PostPosted: Thu Oct 13, 2005 8:49 am    Post subject: Reply with quote

You can be lazy and do this:
Code:

int i = 0;
char a = (char)i;


Otherwise you will need to bit-shift to get differant values out. A char is 1/4 of an int so an int can contain up to 4 chars in a row. So you could be super duper lazy and get those out like this:
Code:

int i = 65535;
char *MyChars = (char *)i;
char a = MyChars[0];
char b = MyChars[1];
char c = MyChars[2];
char d = MyChars[3];

_________________
w00t
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Thu Oct 13, 2005 9:22 am    Post subject: Reply with quote

The line

char *MyChars = (char *)i;

in the second example should read

char *MyChars = (char*)&i;

or you'll get a crash :)
Back to top
View user's profile Send private message
Sonicadvance1



Joined: 13 Oct 2005
Posts: 10

PostPosted: Thu Oct 13, 2005 9:30 am    Post subject: Reply with quote

Should have named this topic "Int to String",woops :P
Back to top
View user's profile Send private message
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Thu Oct 13, 2005 9:39 am    Post subject: Reply with quote

This will work if you want the output in hex:

void numtohex8(char *dst, int n)
{
int i;
static char hex[]="0123456789ABCDEF";
for (i=0; i<8; i++)
{
dst[i]=hex[(n>>((7-i)*4))&15];
}
}


Credit for this goes to Abu - it was in his hello world code. You can easily adapt it if you want a shorter output.

Remember to null-terminate the string afterwards : *dst[8] = '\0';
Back to top
View user's profile Send private message
ChaosKnight



Joined: 14 Apr 2005
Posts: 142
Location: Florida, USA

PostPosted: Thu Oct 13, 2005 11:59 am    Post subject: Reply with quote

Fanjita wrote:
The line

char *MyChars = (char *)i;

in the second example should read

char *MyChars = (char*)&i;

or you'll get a crash :)

Nice catch. That's my second time today...
_________________
w00t
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
raf



Joined: 13 Oct 2005
Posts: 57

PostPosted: Thu Oct 13, 2005 1:48 pm    Post subject: Reply with quote

Fanjita wrote:
This will work if you want the output in hex:

void numtohex8(char *dst, int n)
{
int i;
static char hex[]="0123456789ABCDEF";
for (i=0; i<8; i++)
{
dst[i]=hex[(n>>((7-i)*4))&15];
}
}


Credit for this goes to Abu - it was in his hello world code. You can easily adapt it if you want a shorter output.

Remember to null-terminate the string afterwards : *dst[8] = '\0';


If you're linking against newlib, you could also use sprintf....

sprintf(dest_string, "%d", source_int); //For decimal output (also try itoa())

or

sprintf(dest_string, "%x", source_int); // For hex output

Raf.
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Fri Oct 14, 2005 1:51 am    Post subject: Reply with quote

Er, although this is a software development forum, it is not a forum on discussing the fundamentals of C. If there's a compiler-specific issue then that can be discussed, but please redirect budding programmers to a more appropriate website.

Locked.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.    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