Int to Char

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

Moderators: cheriff, TyRaNiD

Locked
Sonicadvance1
Posts: 10
Joined: Thu Oct 13, 2005 8:35 am

Int to Char

Post by Sonicadvance1 »

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?
User avatar
ChaosKnight
Posts: 142
Joined: Thu Apr 14, 2005 2:08 am
Location: Florida, USA

Post by ChaosKnight »

You can be lazy and do this:

Code: Select all

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

int i = 65535;
char *MyChars = (char *)i;
char a = MyChars[0];
char b = MyChars[1];
char c = MyChars[2];
char d = MyChars[3];
w00t
Fanjita
Posts: 217
Joined: Wed Sep 28, 2005 9:31 am

Post by Fanjita »

The line

char *MyChars = (char *)i;

in the second example should read

char *MyChars = (char*)&i;

or you'll get a crash :)
Sonicadvance1
Posts: 10
Joined: Thu Oct 13, 2005 8:35 am

Post by Sonicadvance1 »

Should have named this topic "Int to String",woops :P
Fanjita
Posts: 217
Joined: Wed Sep 28, 2005 9:31 am

Post by Fanjita »

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=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';
User avatar
ChaosKnight
Posts: 142
Joined: Thu Apr 14, 2005 2:08 am
Location: Florida, USA

Post by ChaosKnight »

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
raf
Posts: 57
Joined: Thu Oct 13, 2005 7:38 am

Post by raf »

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=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.
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

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.
Locked