Binary numbers on Raspberry Pi



  • Hello together,

    I have been trying for some time now to get my Raspberry PI Project to run but I have not yet found a solution to the following problem:

    For the use of the WiringPI SPI Library I need to transmit data - over the SPI - in the format of unsigned chars bytewise. To adress the Register i only have hex or binary values. How can I convert binary or hex values to be unsigned chars so I have the correct type for the parameter to call the function ?



  • i guess, u have to develop protocol.. u can choose bigendien or littleendian and than if u have qint16 than cut 2 bytes and make uint16.



  • @vish I'm sorry, I don't understand what you mean.


  • Lifetime Qt Champion

    Hi,

    Can you show what you want to use from the WiringPI library ?



  • @Dan90 like this
    qFromBigEndian<uint16>((const uchar*) byteArray.data())
    this will give u uint16 type from ur bytearray buffer.



  • @SGaist said:

    Hi,

    Can you show what you want to use from the WiringPI library ?

    Hi,

    i want to use the method

    int wiringPiSPIDataRW (int channel, unsigned char *data, int len)

    where I neet to pass an unsigned char pointer. When I try to set one up like this

    unsigned char *data = 0b10000000

    I get the warning that conversion from int to unsigned char is not possible.

    @vish said:

    @Dan90 like this
    qFromBigEndian<uint16>((const uchar*) byteArray.data())
    this will give u uint16 type from ur bytearray buffer.

    I understand that this funtion gets an unsigned char and converts it to uint16 ? But as I said, I need an unsigned char in the first place.


  • Lifetime Qt Champion

    Then you can use a QByteArray to build your data and then use its data function to access them. You can cast data to unsigned char *



  • Thanks for the hint with QByteArray. I checked it and the problem I still have ist that the data function of my QByteArray object returns a char*, but the wiringPiSPIDataRW function needs an unsigned char*, so I still get an error out of this.

    EDIT:
    I just tried an explicit type conversion and it worked. Could this have a negative effect on my data by any means ?


  • Lifetime Qt Champion

    Do a type cast: wiringPiSPIDataRW (static_cast<unsigned char *>(myByteArray.data()), myBateArry.size());



  • Thanks a lot for the input. It gave no error messages while compiling, however it did not work. I switched to another Library (BCM2835) and i got it to work in just a few Minutes. This library is much better documented than wiringPI SPI.

    Thanks again.


  • Moderators

    @Dan90 You are assigning a value to a pointer, but data is actually an array of "unsigned char":

    unsigned char *data = 0b10000000;
    

    Instead you should do (C++11):

    unsigned char data[] = { (unsigned char)0b10000000 };
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.