-3

I am working in ISO8583 format where i am getting length of message in a 2 byte binary format . I need to convert it into an integer using C . In C# this can be achieved by:

byte B1 = 0xFE;
byte B2 = 0xFC;

int r = (B1 << 8) | B2;

How we can do it in C. Please help

Thanks

jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
Nibin Issac
  • 11
  • 1
  • 3
  • 2
    Change `byte` to `unsigned char`. Exactly the same. – llllllllll Mar 06 '18 at 18:18
  • 1
    Thanks for the reply . I am getting data in this way ^A▒ which is not readable . Do i need to assign it to unsigned char Array and then take first element and second element and do this operation? – Nibin Issac Mar 06 '18 at 18:58

1 Answers1

1

It's almost the same as doing it in C#. As mentioned above you change the byte type to a comparable c/c++ type. You can use unsigned char or one of the int8 types: int8_t, uint8_t. For signed and unsigned. The OR operation is exactly the same as in C#. I get the result to be 65276. e.g.

    #include <iostream>

    using namespace std;

    int main() {

        uint8_t B1 = 0xFE;
        uint8_t B2 = 0xFC;

        unsigned int r = (B1 << 8) | B2;

        cout << r << endl;

        return 0;
     }