Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Determining length of Switch compare

Determining length of Switch compare

Scheduled Pinned Locked Moved C / C++ / MFC
10 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    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

    P L S L 4 Replies Last reply
    0
    • F ForNow

      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

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      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 your case values too. Something like

      unsigned 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, Peter

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      F 1 Reply Last reply
      0
      • F ForNow

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.

        F 1 Reply Last reply
        0
        • L Lost User

          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.

          F Offline
          F Offline
          ForNow
          wrote on last edited by
          #4

          thankx

          1 Reply Last reply
          0
          • P Peter_in_2780

            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 your case values too. Something like

            unsigned 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, Peter

            Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

            F Offline
            F Offline
            ForNow
            wrote on last edited by
            #5

            thankx

            1 Reply Last reply
            0
            • F ForNow

              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

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              (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)

              F 1 Reply Last reply
              0
              • S Stefan_Lang

                (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)

                F Offline
                F Offline
                ForNow
                wrote on last edited by
                #7

                I thought casting it to a short would force the compiler to do a 2 byte compare thanks

                1 Reply Last reply
                0
                • F ForNow

                  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

                  L Offline
                  L Offline
                  leon de boer
                  wrote on last edited by
                  #8

                  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

                  F 1 Reply Last reply
                  0
                  • L leon de boer

                    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

                    F Offline
                    F Offline
                    ForNow
                    wrote on last edited by
                    #9

                    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

                    L 1 Reply Last reply
                    0
                    • F ForNow

                      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

                      L Offline
                      L Offline
                      leon de boer
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups