| View previous topic :: View next topic |
| Author |
Message |
Sonicadvance1
Joined: 13 Oct 2005 Posts: 10
|
Posted: Thu Oct 13, 2005 8:39 am Post subject: Int to Char |
|
|
| 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 |
|
 |
ChaosKnight

Joined: 14 Apr 2005 Posts: 142 Location: Florida, USA
|
Posted: Thu Oct 13, 2005 8:49 am Post subject: |
|
|
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 |
|
 |
Fanjita
Joined: 28 Sep 2005 Posts: 217
|
Posted: Thu Oct 13, 2005 9:22 am Post subject: |
|
|
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 |
|
 |
Sonicadvance1
Joined: 13 Oct 2005 Posts: 10
|
Posted: Thu Oct 13, 2005 9:30 am Post subject: |
|
|
| Should have named this topic "Int to String",woops :P |
|
| Back to top |
|
 |
Fanjita
Joined: 28 Sep 2005 Posts: 217
|
Posted: Thu Oct 13, 2005 9:39 am Post subject: |
|
|
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 |
|
 |
ChaosKnight

Joined: 14 Apr 2005 Posts: 142 Location: Florida, USA
|
Posted: Thu Oct 13, 2005 11:59 am Post subject: |
|
|
| 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 |
|
 |
raf
Joined: 13 Oct 2005 Posts: 57
|
Posted: Thu Oct 13, 2005 1:48 pm Post subject: |
|
|
| 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 |
|
 |
mrbrown
Joined: 17 Jan 2004 Posts: 1536
|
Posted: Fri Oct 14, 2005 1:51 am Post subject: |
|
|
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 |
|
 |
|