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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值