The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Typecast a Short to a table of char
Discuss Typecast a Short to a table of char in the C Programming forum on Dev Shed. Typecast a Short to a table of char C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 17th, 2003, 12:06 PM
|
|
Junior Member
|
|
Join Date: Sep 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Typecast a Short to a table of char
Hello,
I need to convert a short int (2 bytes) to a table of 2 char (Ex: tab_char[2]), 1 byte in each space.
But I cannot figure out how to typecast the address of a short to a table of char!?
The relevant code:
char* tab_char[2];
...
tab_char[0] = (char*)short_integer
/*short_integer contain 2 char but on 2 bytes*/;
...
printf("%c%c", tab_char[0], tab_char[1]);
This doesn't work and I don't know why... Maybe I'm just stupid... :-)
Thank you for your fast help!!!
Vid1048
|

September 17th, 2003, 12:22 PM
|
|
Dinesh_P_V
|
|
Join Date: Aug 2003
Location: India
Posts: 259
Time spent in forums: 12 h 1 m 3 sec
Reputation Power: 0
|
|
|
I don't know about your purpose to typecast.
When you want to store the short values in a string, use sprintf.
char numstring[20];
sprintf(numstring,"%d",num);
Otherwise when you want to do bit manipulation use union.
union dins
{
char a[2];
short num;
}d;
d.num=10;
-Murugesan
|

September 17th, 2003, 12:24 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
if i read your question correctly then your going to have to get each digit out of the number first, like the tens place then the ones place and put then assign them to the corresponding character array. for example:
int x = 25;
char xc[2];
int temp;
temp = x/10;
xc[0] = atoi(temp);
temp = x%10;
xc[1] = atoi(temp);
that should do it.
|

September 17th, 2003, 12:31 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Typecasting in C takes the variable being cast and converts it to the target data type. Therefore, your line:
tab_char[0] = (char*)short_integer;
actually has the effect of taking that short int value and converting it to a char pointer which it then assigns to a char, causing an implicit type cast from char pointer to char (that should have at least given you a warning for different levels of indirection).
What did you get as output, out of curiosity? I would guess a char representation of the entire short_integer followed by however NULL is displayed on your console (probably nothing); e.g., if short_integer == 65, then and 'A' would have been displayed.
You could use a union, but then I'm not a union man. What I normally use are pointers; off the top of my head:
tab_char[0] = *((char*)&short_integer);
tab_char[1] = *((char*)&short_integer + 1);
The first line takes the address of short_integer and casts the address as a char pointer. Then we dereference that char pointer and assign what it's pointing at to tab_char[0].
The second line does the same, except it performs a bit of pointer arithmetic to get the address of the second byte. Then, as before, we dereference that char pointer and assign what it's pointing at to tab_char[1].
Now, are you really wanting to see the characters for which the bytes provide the ASCII code? Or are you wanting to see the actual byte values? If the latter, then you should use %d for the values in decimal or %x for in hex. Or if you are feeling nostalgic, you could use %o for octal -- does anybody ever use octal anymore?
|

September 17th, 2003, 12:31 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
you'll want to use unsigned char's so that you can properly represent each byte. dont forget about endianness as well, the bytes will be reversed as u_char's.
Code:
#include <stdio.h>
main()
{
unsigned char x[2];
unsigned short y = 0xdeaf;
*(unsigned short *)x = y;
printf("y=%hx\nx=%hx\n%hhx %hhx\n", y, *(unsigned short *)x, x[0], x[1]);
}
Quote:
< o7:> ./a.out
y=deaf
x=deaf
af de |
|

September 17th, 2003, 12:45 PM
|
|
Junior Member
|
|
Join Date: Sep 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thank you for all your fast reply:
I'm trying the soltion of dwise1_aol:
Quote: | tab_char[0] = *((char*)&short_integer); | Quote: | tab_char[1] = *((char*)&short_integer + 1); |
but I obtain the error:
" invalid conversion from 'char' to ' "
and to answer to your curiosity :-) , I got 4 ascii character, but not those who should be there...
Vid1048
|

September 17th, 2003, 12:50 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
using a signed char you are limited to values 0-127. using an unsigned char you can properly represent 0-255. try my solution
|

September 17th, 2003, 01:01 PM
|
|
Junior Member
|
|
Join Date: Sep 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I'm trying your solution, but need to go out back to university, will work it out tonight and post back result...
Vid1048
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|