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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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: 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).
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
True, the htonl() function is in the winsock library (not sure why, but it is). You can do it manually (eg. see: stackoverflow.com/questions/859535/…) but it's easiest to call the function already provided.
For some sloppy reason winsock2.h must be included before windows.h, not after.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.