1. 程式人生 > >float 與 char[4]之間的轉換, float的變相的移位操作

float 與 char[4]之間的轉換, float的變相的移位操作

// Console.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>


#include <iostream>
#include <string>
using namespace std;


int main(int argc, char* argv[])
{


//====================================
#if 0
short s = 0;
unsigned char c[2];


//short -> char
printf("short:");  
scanf("%d", &s);
memcpy(c, &s, 2);
printf("char: %x %x\n", c[0], c[1]);  


//char->short  
printf("char:");  
scanf("%x %x", c, c+1);  
memcpy(&s, c, 2);  
printf("short: %d\n", s);  
#endif


//====================================
#if 0
printf("================================\n");
printf("%d\n", sizeof(char));
printf("%d\n", sizeof(unsigned char));
printf("%d\n", sizeof(signed char));


printf("================================\n");
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(unsigned int));
printf("%d\n", sizeof(signed int));
printf("%d\n", sizeof(unsigned int));
printf("%d\n", sizeof(signed int));


printf("================================\n");
printf("%d\n", sizeof(short));
printf("%d\n", sizeof(short int));
printf("%d\n", sizeof(signed short int));
printf("%d\n", sizeof(unsigned short int));
printf("%d\n", sizeof(signed short));
printf("%d\n", sizeof(unsigned short));


printf("================================\n");
printf("%d\n", sizeof(long));
printf("%d\n", sizeof(long int));
printf("%d\n", sizeof(signed long int));
printf("%d\n", sizeof(unsigned long int));
printf("%d\n", sizeof(unsigned long));
printf("%d\n", sizeof(signed long));


printf("================================\n");
printf("%d\n", sizeof(float));
printf("%d\n", sizeof(const float));


printf("================================\n");
printf("%d\n", sizeof(double));
printf("%d\n", sizeof(unsigned double));
printf("%d\n", sizeof(signed double));
printf("%d\n", sizeof(const double));
printf("%d\n", sizeof(const unsigned double));
printf("%d\n", sizeof(const signed double));


printf("================================\n");
printf("%d\n", sizeof(bool));
#endif




//====================================
#if 0
    //float 和 char[4]之間的轉換
float f2;
char *str = "12345.67";
f2 = atof(str);
printf("string = %s \nfloat = %f\n", str, f2);


char szValue[] = "0x11";
int nValue = 0;
sscanf(szValue,"%x",&nValue);
printf("%x\n", nValue);


//=======================
float f = 0.0f;
unsigned char c[sizeof(float)];

//float->char  
printf("float:");  
scanf("%f", &f);
memcpy(c, &f, sizeof(float));
printf("char: %x %x %x %x\n", c[0], c[1], c[2], c[3]);  

//char->float  
printf("char:");  
scanf("%x %x %x %x", c, c+1, c+2, c+3);  //a4 70 9d 3f


memcpy(&f, c, sizeof(float));  
printf("float: %f\n", f);  
#endif


//====================================
#if 0


float f = 0.0f;
    unsigned char c[] = {0xa4, 0x70, 0x9d, 0x3f};
#if 0
    c[0] = '\xa4';
    c[1] = '\x70';
    c[2] = '\x9d';
    c[3] = '\x3f';
#endif
    memcpy(&f, c, sizeof(float));
printf("float: %f\n", f);  
#endif


//====================================
//float移位的變通方法
float f = 1.23f;
unsigned int *pa = (unsigned int *)(&f);


for(int i = 31; i >= 0; i--)
{
cout << (*pa>>i &  0x01) << (i == 31 || i == 23 ? "- " : "  ");
}
cout << "\n";


//以float的形式,去讀取這塊記憶體
printf("%f\n", *((float *)(pa)));
return 0;
}