Determining length of Switch compare
-
Hi I have a Data Type of BYTE defined as
typedef unsigned char BYTE;
when I want to define 2 bytes I do BYTE itshort[2]; if I want a switch value to compare for 2 bytes I do switch ((short) itshort[0]) seems like it will only do the compare for the first byte any way to make it two bytes Thanks
-
Hi I have a Data Type of BYTE defined as
typedef unsigned char BYTE;
when I want to define 2 bytes I do BYTE itshort[2]; if I want a switch value to compare for 2 bytes I do switch ((short) itshort[0]) seems like it will only do the compare for the first byte any way to make it two bytes Thanks
The
switch
argument needs to be a "simple" type, so you should glue your two bytes together into a 16-bit value. You'll need to do something similar with yourcase
values too. Something likeunsigned short testval = itshort[0] | (itshort[1] << 8);
switch (testval)
{
case value1[0] | (value1[1] << 8):
....It might be easier to just use
unsigned short
all round. Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
Hi I have a Data Type of BYTE defined as
typedef unsigned char BYTE;
when I want to define 2 bytes I do BYTE itshort[2]; if I want a switch value to compare for 2 bytes I do switch ((short) itshort[0]) seems like it will only do the compare for the first byte any way to make it two bytes Thanks
-
The
switch
argument needs to be a "simple" type, so you should glue your two bytes together into a 16-bit value. You'll need to do something similar with yourcase
values too. Something likeunsigned short testval = itshort[0] | (itshort[1] << 8);
switch (testval)
{
case value1[0] | (value1[1] << 8):
....It might be easier to just use
unsigned short
all round. Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
You could use a
union
To get the 16 bit value.union _bands
{
BYTE bValues[2];
SHORT sValue;
};You can then use
sValue
to refer to the 16 bit number. -
Hi I have a Data Type of BYTE defined as
typedef unsigned char BYTE;
when I want to define 2 bytes I do BYTE itshort[2]; if I want a switch value to compare for 2 bytes I do switch ((short) itshort[0]) seems like it will only do the compare for the first byte any way to make it two bytes Thanks
(short) is just a type cast. All it does is tell the compiler to interpret the value as a different type. The value in this case is just the single character itshort[0]. What you intended to do was something dofferent: you wanted the computer to reinterpret the memory starting at itshort[0]. But there is no way for the compiler to understand that is what you want. As others pointed out, there are ways to achieve this. However, why don't you simple use short or unsigned short?
typedef short itshort;
If you don't trust the compiler to use 16 bit values for short, you can also use size-specific types. E. g. Microsoft offers __int8, __int16, etc. for these purposes. See __int8, __int16, __int32, __int64 | Microsoft Docs[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
(short) is just a type cast. All it does is tell the compiler to interpret the value as a different type. The value in this case is just the single character itshort[0]. What you intended to do was something dofferent: you wanted the computer to reinterpret the memory starting at itshort[0]. But there is no way for the compiler to understand that is what you want. As others pointed out, there are ways to achieve this. However, why don't you simple use short or unsigned short?
typedef short itshort;
If you don't trust the compiler to use 16 bit values for short, you can also use size-specific types. E. g. Microsoft offers __int8, __int16, etc. for these purposes. See __int8, __int16, __int32, __int64 | Microsoft Docs[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Hi I have a Data Type of BYTE defined as
typedef unsigned char BYTE;
when I want to define 2 bytes I do BYTE itshort[2]; if I want a switch value to compare for 2 bytes I do switch ((short) itshort[0]) seems like it will only do the compare for the first byte any way to make it two bytes Thanks
The C standard fixed size integer units is called stdint.h to use it is as simple as
#include
<stdint.h>[^] Doesn't matter what platform you write your code on your signed and unsigned ints are the right size and it is the industry standard way to deal with it rather than your own macros of typedefs.
In vino veritas
-
The C standard fixed size integer units is called stdint.h to use it is as simple as
#include
<stdint.h>[^] Doesn't matter what platform you write your code on your signed and unsigned ints are the right size and it is the industry standard way to deal with it rather than your own macros of typedefs.
In vino veritas
The code I write here on Windows I am hoping to execute on Z/OS as well using XL C\C++ compiler I have ensure that int, short, and long sizes are consistent I will regardless have a number #ifdef zos and #ifdef MSVC for the I/O reads etc besides that I have to do memory moves in big endian as its a z/os mainframe file I am processing Thanks
-
The code I write here on Windows I am hoping to execute on Z/OS as well using XL C\C++ compiler I have ensure that int, short, and long sizes are consistent I will regardless have a number #ifdef zos and #ifdef MSVC for the I/O reads etc besides that I have to do memory moves in big endian as its a z/os mainframe file I am processing Thanks
Again you are reinventing the wheel the traditional way to deal with Endian is via the standard ints and you simply do eandian aware reads from files. The output from the reads being big_int16_t or big_uint16_t which are big endian versions.
In vino veritas