Ich hab ein kleines Problem mit der Zahlenkonvertierung. Via serieller Schnittstelle bekomme ich 4 8-Bit-Zahlen, die ich in ein Array schreibe. Diese 4x1Byte ist eine Zahl. Bei Integern ist das auch nicht weiter schwer, diese in die ursprüngliche Zahl zurückzuführen:
Code: Alles auswählen
def makeAtoInt4(self, bytearray):
"""Wandelt 4x8-Bit in 32-Bit Werte um"""
int32_0 = bytearray[0] << 24
int32_1 = bytearray[1] << 16
int32_2 = bytearray[2] << 8
int32_3 = bytearray[3]
int32 = in32_0 + int32_1 + int32_2 + int32_3
return int32
Danke
Tulkas
P.S.
Mit C würde ich es so machen:
Code: Alles auswählen
typedef union
{
float f;
int l;
short int i[2];
char b[4];
}U32;
U32 int32;
int32.b[0] = bytearray[0]
int32.b[1] = bytearray[1]
int32.b[2] = bytearray[2]
int32.b[3] = bytearray[3]
printf("Float: %f\n", int32.f);