0

I have c++ code as follows to display hex value from int array.

#include <iostream>
using namespace std;

int main()
{
    int binText[32]={1,0,1,0,1,1,1,1,0,0,0,1,1,0,1,0,1,1,1,1,0,1,0,1,1,1,1,1,0,0,0,1};
    char temp[255]={0};
    for (int i=0; i<32; i++)
    {
        sprintf(&temp[strlen(temp)], "%d", binText[i]);
    }
    char HexBuffer[255];
    unsigned long long int Number = 0;
    int BinLength = strlen(temp);

    for(int i=0; i<32; i++)
    {
        Number += (long int)((temp[32 - i - 1] - 48) * pow((double)2, i));
    }

    ltoa(Number, HexBuffer, 16);
    cout << HexBuffer <<endl;
}

its output is: af1af5f1 So this code converted the binary digit stored in int array into hex value.

But when i tried to use this same code to send the hex value in serial communication using win32 . it is not sending the correct hex value. the code is

    serialObj.begin("COM1", 9600); //opens the port
    int binText[32]={1,0,1,0,1,1,1,1,0,0,0,1,1,0,1,0,1,1,1,1,0,1,0,1,1,1,1,1,0,0,0,1};
    char temp[255]={0};
    for (int i=0; i<32; i++)
    {
        sprintf(&temp[strlen(temp)], "%d", binText[i]);
    }
    char HexBuffer[255];
    unsigned long long int Number = 0;
    int BinLength = strlen(temp);

    for(int i=0; i<32; i++)
    {
        Number += (long int)((temp[32 - i - 1] - 48) * pow((double)2, i));
    }

    ltoa(Number, HexBuffer, 16);
    serialObj.send(HexBuffer);

    serialObj.close();//closes the port

The "send" function invoked by "serialObj.send(HexBuffer);" is as below:

void serial::send(char data[])
{
    DWORD dwBytesWrite;
    WriteFile(serialHandle, data, 4, &dwBytesWrite, NULL);
}

But the data it is sending is : "61 66 31 61". I couldnot figure out why it is giving this output .

The "send" function "serialObj.send" works properly for following code

char str[4]={0x24,0x24,0x53,0x3F};
serialObj.send(str);

and it sends 24 24 53 3F.

So i want to send AF 1A F5 F1 from the above the binary digit stored in the int array(shown above). how can i do this

user3048644
  • 93
  • 1
  • 5
  • 17

1 Answers1

1

If you want to send the actual binary bits, don't call ltoa at all:

serialObj.send((char *)&Number);

Note that Number is declared as long long, which is likely 64 bits, which isn't going to fit in the 4 bytes sent by your serial::send() function.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • hi thank you for the response. It seemed that when i send using your line . it sends F1 F5 1A AF . which is opposite of AF 1A F5 F1. and it is close to the solution. So can you explain what has happened when i send this "Number" – user3048644 Dec 10 '13 at 01:30
  • @user3048644: You are running into "endian" problems. In short, most CPUs these days store integers in memory starting with the *least* significant byte (but the opposite is still true for some CPUs). So the hex number 0x12345678 would be stored in memory on a little-endian system as the bytes `78 56 34 12`, and the bytes would be sent out the serial port in that order. You can quickly turn these bytes around using something like the `htonl()` function (for a 32-bit number). – Greg Hewgill Dec 10 '13 at 01:37
  • to use htonl() , i think we need to include "winsock2.h" and my program is showing many redefinition error when included winsock2. so is there any other way to convert this little endian to big endian – user3048644 Dec 10 '13 at 02:07
  • True, the `htonl()` function is in the winsock library (not sure why, but it is). You can do it manually (eg. see: http://stackoverflow.com/questions/859535/how-do-i-convert-a-big-endian-struct-to-a-little-endian-struct) but it's easiest to call the function already provided. – Greg Hewgill Dec 10 '13 at 02:19
  • For some sloppy reason winsock2.h must be included before windows.h, not after. – ScottMcP-MVP Dec 10 '13 at 03:01
  • @GregHewgill how can we append three hex byte (24 24 53) in front of Number . Actually i have converted number into big endian using bitwise operation . now i have to append three hex byte in front of it . can you suggest how can it be done.. – user3048644 Dec 10 '13 at 03:49
  • @user3048644: The easiest way might be to modify `serial::send()` so it takes a length parameter, then you can pass it the number of bytes to send. So, `char prefix[3] = {0x24, 0x24, 0x53}; serialObj.send(prefix, 3);` or something. – Greg Hewgill Dec 10 '13 at 06:22