C Function to Convert float to byte array

后端 未结 6 896
天命终不由人
天命终不由人 2020-12-04 22:42

I\'m trying to make a function that will accept a float variable and convert it into a byte array. I found a snippet of code that works, but would like to reuse it in a func

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 23:02

    this seems to work also

    #include 
    #include 
    #include 
    
    float fval = 1.11;
    size_t siz;
    siz = sizeof(float);
    
    uint8_t ures[siz];
    
    memcpy (&ures, &fval, siz);
    

    then

    float utof;
    memcpy (&utof, &ures, siz);
    

    also for double

    double dval = 1.11;
    siz = sizeof(double);
    
    uint8_t ures[siz];
    
    memcpy (&ures, &dval, siz);
    

    then

    double utod;
    memcpy (&utod, &ures, siz);
    

提交回复
热议问题