float int short int 转byte

void intToBytesLittle(int value, unsigned char*cSendBuff,int pos) {
    cSendBuff[pos+3] = (byte)((value >> 24) & 0xFF);
    cSendBuff[pos+2] = (byte)((value >> 16) & 0xFF);
    cSendBuff[pos+1] = (byte)((value >> 8) & 0xFF);
    cSendBuff[pos+0] = (byte)(value & 0xFF);
}

void shotIntToBytesLittle(short int value, unsigned char*cSendBuff, int pos) {
    cSendBuff[pos + 1] = (byte)((value >> 8) & 0xFF);
    cSendBuff[pos + 0] = (byte)(value & 0xFF);
}

void floatToBytesLittle(float value, unsigned char*cSendBuff, int pos)
{
    unsigned short i = 0;
    float floatVariable = value;
    unsigned char *pdata = (unsigned char *)&floatVariable;
    for (i = 0; i < 4; i++)
    {
        cSendBuff[i+pos] = *pdata++;//float转BYTE
    }
}

你可能感兴趣的:(float,int,short,int,转byte)