Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //by steef
- #include <iostream>
- using namespace std;
- void decoup_float(float val, unsigned char tab[])
- {
- unsigned char * ptr_char = reinterpret_cast<unsigned char *>(&val);//ptr sur uchar à l'adresse du float (val)
- for (int i = 0; i < sizeof(val); i++) {
- tab[i] = static_cast<int>(*(ptr_char + i));//stock la valeur de chaque octets pointer dans un tableau
- }
- }
- float reconstitution_float(unsigned char tab[])
- {
- float val;
- unsigned char * ptr_char = reinterpret_cast<unsigned char *>(&val);//ptr sur uchar à l'adresse du float (val)
- for (int i = 0; i < sizeof(val); i++) {
- *(ptr_char + i) = tab[i] ;//écrit la valeur de chaques octets dans l'emplacement memoire correspondant dans le float
- }
- return val;
- }
- int main()
- {
- float val_f = 3.14;
- unsigned char octets[4];
- decoup_float(val_f, octets);
- cout <<hex<< static_cast<int>(octets[0]) << endl;
- cout << static_cast<int>(octets[1]) << endl;
- cout << static_cast<int>(octets[2]) << endl;
- cout << static_cast<int>(octets[3]) << endl;
- cout << reconstitution_float(octets) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment