• Increase font size
  • Default font size
  • Decrease font size

float into char array and back casting in C++

How to convert float variable (4 bytes) into an array of chars and then back? Here is the code:

 

#include 
 #include 
 #include 
 #include 
 
 using namespace std;
 
 void somefunc(unsigned int fptr);
 void somefunc2(float* fptr);
 
 int main()
 {
 float a = 1.345678;
 char cArray[1024];
 sprintf(cArray, "%f", a);
 
 cout << "Float converted into char array: " << cArray << endl;
 
 float b = atof(cArray);
 cout << "Char array converted into float: " << b << endl;
 
 //-------------------
 // POINTERS
 // http://www.cplusplus.com/doc/tutorial/pointers/
 //-------------------
 float fl=3.14;
 unsigned int addr=(unsigned int) &fl;
 somefunc(addr);
 cout << "Variable accessed by pointer: " << fl << endl;
 
 //-------------------
 float f2=6.14;
 float* addr2 = &f2;
 somefunc2(addr2);
 cout  << "Variable accessed by pointer: " << f2 << endl;
 
 cout << sizeof(char *) << ", " << sizeof(float *) << endl;
 
 int *abc = NULL;
 cout << "Prints sizeof(int): " << abc + 1 << endl; /* Prints sizeof(int) */
 
 //Prints 1. Casting it back to void * is necessary, otherwise it will try to dereference it and print as a string.
 cout << (void *)((char *)abc + 1) << endl;
 
 return 0;
 }
 
 void somefunc(unsigned int fptr){
 *(float*)fptr=99.9;
 }
 
 void somefunc2(float* fptr){
 *fptr=88.8;
 }
 

 

 

Comments  

 
0 #3 Fakta Unik Dan Aneh 2016-12-28 07:51
Britney Spears: http://tinyurl.com/hew84tv
onsu cinta mati: http://tinyurl.com/js49e5o
http://tinyurl.com/hlka37p kabar terbaru Fendy Chou
http://bit.ly/2hi1zqD film terbaru
Quote
 
 
0 #2 http://www. 2015-07-25 10:55
You actually make it seem so easy with your presentation but I find this topic to be actually somethng that I think I would never understand.
It seems too complicated and vwry broad for me. I'm looking forward for your next post, I'll try to get the hang of it!


Here is my web site ... MakingUsmile (http://www.makingusmile.net: http://www.makingusmile.net)
Quote
 
 
0 #1 mikesmaineventpc.com 2014-10-05 23:14
Howdy! I could ɦave sworn I've been to this website
Ьefore ƅut after going thгough many ߋf thе articles I realized it's neԝ to me.
Ɍegardless, Ӏ'm ϲertainly happy Ι discovrred it
and I'll Ье book-marking it and checking back regularly!
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1696 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2476 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3026 Hits
gpsim tutorial Machine Learning and Robotics
Read More 6092 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 1696 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2476 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3026 Hits
gpsim tutorial Machine Learning and Robotics
Read More 6092 Hits
Design by i-cons.ch / etosha-namibia.ch