English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

example:

unsigned char foo[5] = {0x01, 0x02, 0x03, 0x04, 0x05};

short bar;

//bar should be the 2 byte value spanning foo[1] and foo[2];

bar = (short)(foo[1] ) + (short)(foo[2] * 256); //this is correct.

I can do it this way but i was wondering if there was a casting mechanism to accomplish this without explicity addressing the two separate elements.

//Something like this...
//will only copy the 8 bit value in foo[1].

bar = (short)(*(foo + 1));


suggestions?

2007-08-22 05:27:48 · 4 answers · asked by Joe 4 in Computers & Internet Programming & Design

4 answers

The best way is the way you did it, accessing the two values separately ((short)(foo[1] ) + (short)(foo[2] * 256)), or you can use "<<8" instead of "*256" but the optimizer will probably produce the same code in either case. It actually will run quite efficiently. And it's the safest thing to do.

In "(short)(*(foo + 1))", the expression in parentheses beside the typecast, "*(foo + 1)", references the second character only, because foo is a char *. Casting that to a short AFTER dereferencing it doesn't cause the additional byte to get picked up.

You'd want "*((short *)(foo + 1))", I think. Add 1 to foo while it is still a char *, so that it adds only 1 byte offset. Then cast to a short * to point to 2 bytes, and then dereference.

That being said...

My recollection is that this code will not work, because foo[1..2] won't be aligned on an even byte boundary. While the C language syntax technically allows it, I wouldn't be surprised if it didn't execute. I think it will only work if you use an even-numbered offset in the character string.

For example, Microsoft Developer Network says:
"Access to data at unaligned addresses can affect correct program operation as well as processing efficiency. Some CPU architectures, such as the Itanium, cannot properly reference numeric data unless it is properly aligned in memory. When running a program on the Itanium, access to data which is not properly aligned will generate an EXCEPTION_ DATATYPE_ MISALIGNMENT exception which, if not handled, will cause the application to hang or crash."
http://msdn2.microsoft.com/en-us/library/aa505951.aspx

2007-08-22 05:31:08 · answer #1 · answered by McFate 7 · 1 0

To begin with, you really should use this form to define the array: byte[] barray; The reason being is that it better shows that the type is "byte array" and that the [] is part of the type, not part of the variable name. To this end, because [] is part of the type and not the name "barray[] = {123};" doesn't make much sense by itself. Try this instead: byte[] barray = {123};

2016-05-19 23:07:50 · answer #2 · answered by luella 3 · 0 0

Arrays are contiguous memory, so if you want to bytes that are side by side, just make the short bar address = to address of the first byte from the array that you want.

If they are not contiguous, you must do by individual bytes, which is the most robust way to do it to begin with. It is fast too.

2007-08-22 05:35:13 · answer #3 · answered by Anonymous · 1 0

short bar = ((short)(foo[1] << 256)) | (short)(foo[0]);

with shift and or :)

2007-08-22 09:46:03 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers