C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 17th, 2003, 12:06 PM
Vid1048 Vid1048 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 3 Vid1048 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question 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

Reply With Quote
  #2  
Old September 17th, 2003, 12:22 PM
murugesan murugesan is offline
Dinesh_P_V
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: India
Posts: 259 murugesan New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 12 h 1 m 3 sec
Reputation Power: 0
Send a message via Yahoo to murugesan
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

Reply With Quote
  #3  
Old September 17th, 2003, 12:24 PM
samlab samlab is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 33 samlab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old September 17th, 2003, 12:31 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,256 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 36 m 23 sec
Reputation Power: 1985
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?

Reply With Quote
  #5  
Old September 17th, 2003, 12:31 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
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

Reply With Quote
  #6  
Old September 17th, 2003, 12:45 PM
Vid1048 Vid1048 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 3 Vid1048 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old September 17th, 2003, 12:50 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
using a signed char you are limited to values 0-127. using an unsigned char you can properly represent 0-255. try my solution

Reply With Quote
  #8  
Old September 17th, 2003, 01:01 PM
Vid1048 Vid1048 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 3 Vid1048 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Typecast a Short to a table of char

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap