Hi friends,
How can I store a short into an unsigned char???
its like
unsigned char msg;
//now I want to assign 0xAB into it.
//and than want to test it
if(msg==0xAB)
printf("true");
else
printf("false");
What I did is:
unsigned char msg;
msg=(char)(0xAB<<8);
if condition....
This isnt working...
cant anyone pls help me out?
Thanks
|
|
0
|
|
|
|
Reply
|
a.k.vora (41)
|
10/15/2008 10:39:16 PM |
|
Neel wrote:
> Hi friends,
> How can I store a short into an unsigned char???
>
If you just want the low order bits;
unsigned char a = short_value;
--
Ian Collins
|
|
0
|
|
|
|
Reply
|
ian-news (9880)
|
10/15/2008 10:45:22 PM
|
|
On Oct 15, 3:45=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Neel wrote:
> > Hi friends,
> > How can I store a short into an unsigned char???
>
> If you just want the low order bits;
>
> unsigned char a =3D short_value;
>
> --
> Ian Collins
If I do something like...
unsigned char x=3D 0xAA;
if(x=3D=3D(unsigned char) 0xAA)
it returns false...
whats wrong here?
|
|
0
|
|
|
|
Reply
|
a.k.vora (41)
|
10/15/2008 10:59:34 PM
|
|
Neel wrote:
> On Oct 15, 3:45 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> Neel wrote:
>>> Hi friends,
>>> How can I store a short into an unsigned char???
>> If you just want the low order bits;
>>
>> unsigned char a = short_value;
>>
*Last warning for quoting signatures!*
>
> If I do something like...
>
> unsigned char x= 0xAA;
> if(x==(unsigned char) 0xAA)
>
> it returns false...
>
> whats wrong here?
Everything?
Why the cast?
You obviously didn't post what you tested.
--
Ian Collins
|
|
0
|
|
|
|
Reply
|
ian-news (9880)
|
10/15/2008 11:03:35 PM
|
|
Neel wrote:
> Hi friends,
> How can I store a short into an unsigned char???
>
> its like
>
>
> unsigned char msg;
>
> //now I want to assign 0xAB into it.
> unsigned char msg;
> msg=(char)(0xAB<<8);
Do it this way:
msg = 0xAB;
--
pete
|
|
0
|
|
|
|
Reply
|
pfiland (6613)
|
10/15/2008 11:12:01 PM
|
|
On Oct 15, 4:12=A0pm, pete <pfil...@mindspring.com> wrote:
> Neel wrote:
> > Hi friends,
> > How can I store a short into an unsigned char???
>
> > its like
>
> > unsigned char msg;
>
> > //now I want to assign 0xAB into it.
> > unsigned char msg;
> > msg=3D(char)(0xAB<<8);
>
> Do it this way:
>
> =A0 =A0 =A0msg =3D 0xAB;
>
> --
> pete
thank you
|
|
0
|
|
|
|
Reply
|
a.k.vora (41)
|
10/15/2008 11:19:17 PM
|
|
On Oct 15, 4:12=A0pm, pete <pfil...@mindspring.com> wrote:
> Neel wrote:
> > Hi friends,
> > How can I store a short into an unsigned char???
>
> > its like
>
> > unsigned char msg;
>
> > //now I want to assign 0xAB into it.
> > unsigned char msg;
> > msg=3D(char)(0xAB<<8);
>
> Do it this way:
>
> =A0 =A0 =A0msg =3D 0xAB;
>
I was just wondering, Char occupies 1 Byte and Short occupies 2
Bytes...
so in short 0xAB means lower byte is AB and higher byte is 00.
so when I assign, what byte would be assigned ???
|
|
0
|
|
|
|
Reply
|
a.k.vora (41)
|
10/15/2008 11:20:53 PM
|
|
Ian Collins <ian-n...@hotmail.com> wrote:
> Neel wrote:
> > Hi friends,
> > How can I store a short into an
> > unsigned char???
>
> If you just want the low order bits;
>
> unsigned char a = short_value;
This needn't give you the same low order
value bits as the short if short_value
is negative.
--
Peter
|
|
0
|
|
|
|
Reply
|
airia (1802)
|
10/16/2008 12:26:04 AM
|
|
Neel <a.k.v...@gmail.com> wrote:
> pete <pfil...@mindspring.com> wrote:
> > Neel wrote:
> > > How can I store a short into an unsigned char???
Same way you store a B-Double in the back of a coupe.
> > > its like
> > >
> > > unsigned char msg;
> > >
> > > //now I want to assign 0xAB into it.
> > > unsigned char msg;
> > > msg=3D(char)(0xAB<<8);
Since msg is unsigned char it makes no sense to cast
the value to char beforehand. What is the purpose of
the shift? [What are _really_ trying to do? It looks
like you're posting a broken solution to an undisclosed
problem.]
> > Do it this way:
> >
> > =A0 =A0 =A0msg =3D 0xAB;
>
> I was just wondering, Char occupies 1 Byte
ITYM char, but by definition, yes.
> and Short occupies 2 Bytes...
A short may occupy 1 byte, or 4, or... It must have
at least 15 value bits and 1 sign bit (or 16 value
bits in the case of unsigned short), but how many
bytes those bits occupy is implementation dependant.
On many embedded implementations, short is 1 byte.
> so in short 0xAB means lower byte is AB and higher
> byte is 00.
No, 0xAB 'in short' means the short has the value 171.
If you want to know what the high and low octets of
a 16-bit representation are, then you should simply
mask and shift the value appropriately. You should
also use an unsigned short (or integer in general)
if you're going to do this.
> so when I assign, what byte would be assigned
Since unsigned char is clearly unsigned, the value
stored in it will (by definition) be the value
of the expression modulo one more than UCHAR_MAX.
--
Peter
|
|
0
|
|
|
|
Reply
|
airia (1802)
|
10/16/2008 12:35:24 AM
|
|
Neel <a.k.vora@gmail.com> writes:
> On Oct 15, 4:12�pm, pete <pfil...@mindspring.com> wrote:
>> Neel wrote:
>> > Hi friends,
>> > How can I store a short into an unsigned char???
>>
>> > its like
>>
>> > unsigned char msg;
>>
>> > //now I want to assign 0xAB into it.
>> > unsigned char msg;
>> > msg=(char)(0xAB<<8);
>>
>> Do it this way:
>>
>> � � �msg = 0xAB;
>>
>
>
> I was just wondering, Char occupies 1 Byte and Short occupies 2
> Bytes...
Since C is case-sensitive, it's best not to capitalize type names;
write "char", not "Char".
char occupies 1 byte (by definition -- but a byte in C can be more
than 8 bits).
short occupies 2 bytes in your implementation, but this can vary. The
standard guarantees only that it's at least 16 bits (it actually
guarantees a range of at least -32767 to +32767, but that implies at
least 16 bits). It can be more than 2 bytes on some systems, and it
can be a single byte if a byte is at least 16 bits.
> so in short 0xAB means lower byte is AB and higher byte is 00.
> so when I assign, what byte would be assigned ???
When you assign something to an integer object, the *value* is
assigned. 0xAB is just another way of writing 171 -- and it's of type
int, not short. If you write:
unsigned char msg;
msg = 0xAB;
it just assigns the value 0xAB (equivalently, 171) to msg. The value
is implicitly converted from int to unsigned char. Since the range of
unsigned char is at least 0..255, the conversion is straightforward.
You're usually better off thinking in terms of values rather than
representations. And most casts (explicit conversions) are
unnecessary.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21469)
|
10/16/2008 12:40:01 AM
|
|
Peter Nilsson <airia@acay.com.au> writes:
> Ian Collins <ian-n...@hotmail.com> wrote:
>> Neel wrote:
>> > Hi friends,
>> > How can I store a short into an
>> > unsigned char???
>>
>> If you just want the low order bits;
>>
>> unsigned char a = short_value;
>
> This needn't give you the same low order
> value bits as the short if short_value
> is negative.
It does if short is 2's-complement with no padding bits.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21469)
|
10/16/2008 12:46:21 AM
|
|
Keith Thompson <ks...@mib.org> wrote:
> Peter Nilsson <ai...@acay.com.au> writes:
> > Ian Collins <ian-n...@hotmail.com> wrote:
> > > If you just want the low order bits;
> > > unsigned char a = short_value;
> >
> > This needn't give you the same low order
> > value bits as the short if short_value
> > is negative.
>
> It does if short is 2's-complement with
> no padding bits.
Even with padding bits, that would be the case.
--
Peter
|
|
0
|
|
|
|
Reply
|
airia (1802)
|
10/16/2008 12:48:34 AM
|
|
|
11 Replies
32 Views
(page loaded in 0.164 seconds)
Similiar Articles: converting a character string into a variable name - comp.lang ...For example, for the array called PRESSURE(istart:iend), I would store 'PRESSURE' in a character ... variable name PRESSURE and use it to write out the > data. Short ... integral promotion., sign extension - comp.lang.cIf you're using char to store numeric data (as opposed to things that are notionally ... You can use a char, short, enumerated type, or bit-field ... assigned to addr ... Compiler for 16bit architecture - comp.compilers.lcc... bit and > is supported by software routines and load and store bytes ... adaptation of lcc-win32 for an embedded DSP, I use: > > sizeof(char) == sizeof(int) == sizeof(short ... Read string from fifo - comp.unix.programmer... predictable it can be very long) from the fifo and store ... off reading into a buffer, e.g. something like char b ... FIFO byte queue (hardware) with it, a big ... > short ... improve strlen - comp.lang.asm.x86... regards claudio void RoutineC ( void ) { char ... Faster way to implement strlen() is to store the length ... LL3@strlen: add eax, 1 cmp BYTE PTR [eax], 0 jne SHORT ... percision in matlab command imdivide - comp.soft-sys.matlab ...... dir(fullfile(pathname1,'*.dcm')); % Store the filenames ... movie for i=1:nframes;% image number name1 = char ... crop'); SS= imcrop(SS, [200,200,100,100]); %short core ... database engine creates problem with characters? - comp.databases ...In short, in a complex query where 3 tables from 2 ... same word, but the one spelled with the swedish character ... Creates a new database and the files used to store the ... bx-register question - comp.lang.asm.x86Hi I ve got a short code-listing, see below .... Puffer STRUC MAX DB ... "This is my text" On which character does the bx point to? Or does it point on the ... pthread_create, "this" pointer and SIGBUS - comp.sys.hp.hpux ...... define MAXPACKET 20000 > > struct package_info > { > unsigned short transport_id; > char ... NULL, or a pointer to a pthread_t object where the function can store the ... Free bitmap font - comp.arch.fpgaIf you are short on memory store it the MCM6674 way, as 9x7 plus one extra bit (see the triangle in the corner) to lower the character by two rows. How to change length of a num column - comp.soft-sys.sas ...... proc sql; create table dept(deptno num(20), dname char ... on the platform, numeric variables can be as short as 2 ... comp.databases.mysql I want to have a column to store a ... std::map< MyString, MyString > comparison operator? - comp ...... for your >help. > You should provide a complete (short ... If you are storing MyString by value in the map these are ... from a char* XString& XString::operator=(const char* cs ... nitializing a static vector <> of integers (this static ...Problems storing objects in a vector and incrementing a ... class that has an std::vector member as ... short it is ... method? - comp.lang.c++ ... int main(int argc, char ... External variables - comp.lang.asm.x86If you put the address of something in a variable type designed to store ... bytes): var: times 8 dd 0 I can say extern int var[8]; extern short int var[16]; extern char ... Yet another binary search tree library - comp.lang.c... include "libtree.h" typedef struct { char name[32 ... You want to store each "car" only once, but you want to ... bind): my_addr->sin_family or (unsigned short ... Unsigned Short to Character Array: short, unsigned, char, arrayHi all, In my project I need to store the value of an unsigned short in a character array in a specific format.... As an example char header; unsigned ... Short Story Characterization - Yahoo! Voices - voices.yahoo.comThe short story character can be defined using a few traits, which you later use to refer back to them. Research your characters, stick to simple speech patterns, and ... Char, Short, Int and Long Types - MQL5 DocumentationChar, Short, Int and Long Types char. The char type takes 1 byte of memory (8 bits) and ... The ulong type also occupies 8 bytes and can store values from 0 to 18 446 744 073 ... Integer (computer science) - Wikipedia, the free encyclopediaThe order of the memory bytes storing the bits varies; see endianness. The width or ... uint8_t, char: byte: Byte: n/a: unsigned tinyint: 16: halfword, word, short: Signed: From ... Converting int to char* in C - Ozzu - Webmaster Forums, Headlines ...Also, I'm not really displaying the char array, I have to store it. So I don't think ... short int Send(unsigned char *message, unsigned short int nbytes) for the start. Variables. Data Types. - C++ DocumentationYou should use either signed or unsigned if you intend to store numerical values in a char-sized variable. short and long can be used alone as type specifiers. How should I store short text strings into a SQL Server database ...... is to use CHAR for strings less than 10 chars long, since N/VARCHAR stores both the string and the length and the difference between storing short strings in N/CHAR vs. N ... 6/23/2012 3:39:10 PM
|