[PD] Getting 32 bit floating point number from 4 sequential bytes

Christof Ressi christof.ressi at gmx.at
Sat Apr 6 16:04:15 CEST 2019


While type punning through unions is allowed in C, the only way which works in both C and C++ (without breaking the strict aliasing rule) is using memcpy. In such case, the call to memcpy will completely optimized away by every decent compiler.

> Gesendet: Samstag, 06. April 2019 um 11:28 Uhr
> Von: "cyrille henry" <ch at chnry.net>
> An: "pd-list at lists.iem.at" <pd-list at lists.iem.at>
> Betreff: Re: [PD] Getting 32 bit floating point number from 4 sequential bytes
>
> hello,
> I think a better way (that did not need to copy the data) is to use a union : i.e. 2 different type can be declared in the same memory space.
> something like :
> 
> // union type definition
> union int_and_float {
>    int8_t my_int[4];
>    float my_float;
> };
> 
> int_and_float my_int_and_float; // declaration of a variable of this type
> 
> my_int_and_float.my_int[0] = 0; // value assignation
> my_int_and_float.my_int[1] = 0;
> my_int_and_float.my_int[2] = 0;
> my_int_and_float.my_int[3] = 0;
> 
> the float data should be in : my_int_and_float.my_float;
> 
> I did not test this code, It will certainly contain syntaxes errors, but you'll find the way to correct it on internet...
> 
> 
> An other solution is to use bitwise operator :
> 
> my_float = (byte1 << 24) & (byte2 << 16) & (byte3 << 8) & byte4;
> 
> cheers
> Cyrille
> 
> Le 05/04/2019 à 19:04, Arda Eden a écrit :
> > I found a method using "memcpy". Seems like it is working for now.
> > 
> > Arda Eden <ardaeden at gmail.com <mailto:ardaeden at gmail.com>>, 5 Nis 2019 Cum, 16:00 tarihinde şunu yazdı:
> > 
> >     Hi,
> >     I am trying to write a Pure Data external for getting real time motion capture values from an Xsens system. According to the manual (https://xsens.com/download/usermanual/3DBM/MVN_real-time_network_streaming_protocol_specification.pdf), the x, y and z coordinates of the skeleton segment positions are transferred over network (udp) as sequential 4 bytes (32 bit floating point - big endian).
> > 
> >     I am reading the udp data with [netreceive] succesfully and able to reach any value I need.  Now trying to find a way to combine these (big endian) sequential 4 bytes in order to get the resulting 32 bit floating point number. A little advice would speed me up.
> > 
> >     By the way, I studied the [oscparse] source. It was very helpful in most ways but also a little complicated too.
> > 
> >     Thanks already.
> > 
> >     -- 
> >     Arda EDEN
> >     Yıldız Teknik Üniversitesi
> >     Sanat ve Tasarım Fakültesi
> >     Müzik ve Sahne Sanatları Bölümü
> >     Duysal (Ses) Sanatları Tasarımı Programı
> >     İstanbul/Türkiye
> > 
> >     Yildiz Technical University
> >     Faculty of Art and Design
> >     Department of Music and Performing Arts
> >     Audio Design Program
> >     Istanbul/Turkey
> > 
> > 
> > 
> > -- 
> > Arda EDEN
> > Yıldız Teknik Üniversitesi
> > Sanat ve Tasarım Fakültesi
> > Müzik ve Sahne Sanatları Bölümü
> > Duysal (Ses) Sanatları Tasarımı Programı
> > İstanbul/Türkiye
> > 
> > Yildiz Technical University
> > Faculty of Art and Design
> > Department of Music and Performing Arts
> > Audio Design Program
> > Istanbul/Turkey
> > 
> > _______________________________________________
> > Pd-list at lists.iem.at mailing list
> > UNSUBSCRIBE and account-management -> https://lists.puredata.info/listinfo/pd-list
> > 
> 
> 
> 
> _______________________________________________
> Pd-list at lists.iem.at mailing list
> UNSUBSCRIBE and account-management -> https://lists.puredata.info/listinfo/pd-list
>





More information about the Pd-list mailing list