Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C

Transform between IEEE, IBM or VAX floating point number formats and bytes expressions

Rate me:
Please Sign up or sign in to vote.
4.74/5 (14 votes)
21 Nov 20056 min read 106.5K   1.9K   27  
This program can transform between IEEE, IBM or VAX floating point number formats and their bytes expressions.
/***************************************************************************
                          TestlibNumber.c  -  description
                             -------------------
    begin                : November 2005
    copyright            : (C) 2005 by John Jiyang Hou
    email                : jyhou69@hotmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "libNumber.h"

void TestlibNumber(int FloatType, float f, double df)
{

switch(FloatType)
{
	case IEEE_SINGLE_FLOAT:
	case IEEE_DOUBLE_FLOAT:
		IeeeFloatTest(f, df);
		break;
	case IBM_SINGLE_FLOAT:
	case IBM_DOUBLE_FLOAT:
		IbmFloatTest(f, df);
		break;
	case VAX_SINGLE_FLOAT:
		VaxFloatTest(f, df);
		break;
}

}

void IeeeFloatTest(float f, double df)
{
	unsigned char bb[8];
	float f1=f;
	double df1=df;

	// ieee float test

	printf("--------------------------------------------\n");

	memset(bb,'\0',sizeof(bb));

	IeeeSingleFloat2ByteUnion(f1,bb);

	printf("\nIeeeSingleFloat2ByteUnion:\n%f ==> %.2X %.2X %.2X %.2X\n", f1,
		bb[0],bb[1],bb[2],bb[3]);

	f1=0;

	Byte2IeeeSingleFloatUnion(bb,&f1);

	printf("\nByte2IeeeSingleFloatUnion:\n%.2X %.2X %.2X %.2X ==> %f\n",
		bb[0],bb[1],bb[2],bb[3],f1);

	printf("--------------------------------------------\n");

	
	memset(bb,'\0',sizeof(bb));

	IeeeDoubleFloat2ByteUnion(f1,bb);

	printf("\nIeeeDoubleFloat2ByteUnion:\n%f ==> %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n", df1,
		bb[0],bb[1],bb[2],bb[3],bb[4],bb[5],bb[6],bb[7]);

	df1=0;

	Byte2IeeeDoubleFloatUnion(bb,&df1);

	printf("\nByte2IeeeDoubleFloatUnion:\n%.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X ==> %f\n",
		bb[0],bb[1],bb[2],bb[3],bb[4],bb[5],bb[6],bb[7],df1);

	printf("--------------------------------------------\n");
		
	f1=f;
	memset(bb,'\0',sizeof(bb));

	IeeeSingleFloat2ByteL(f1,bb);

	printf("\nIeeeSingleFloat2ByteL:\n%f ==> %.2X %.2X %.2X %.2X\n",
		f1,bb[0],bb[1],bb[2],bb[3]);

	f1=f;
	memset(bb,'\0',sizeof(bb));

	IeeeSingleFloat2ByteM(f1,bb);

	printf("\nIeeeSingleFloat2ByteM:\n%f ==> %.2X %.2X %.2X %.2X\n",
		f1,bb[0],bb[1],bb[2],bb[3]);

	f1=0;

	Byte2IeeeSingleFloat(bb,&f1);

	printf("\nByte2IeeeSingleFloat:\n%.2X %.2X %.2X %.2X ==> %f\n",
		bb[0],bb[1],bb[2],bb[3],f1);
	
	printf("--------------------------------------------\n");

	// ieee double byte test

	df1=df;
	memset(bb,'\0',sizeof(bb));

	IeeeDoubleFloat2ByteL(df1,bb);

	printf("\nIeeeDoubleFloat2ByteL:\n%f ==> %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n", df1,
		bb[0],bb[1],bb[2],bb[3],bb[4],bb[5],bb[6],bb[7]);

	df1=df;
	memset(bb,'\0',sizeof(bb));

	IeeeDoubleFloat2ByteM(df1,bb);

	printf("\nIeeeDoubleFloat2ByteM:\n%f ==> %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n", df1,
		bb[0],bb[1],bb[2],bb[3],bb[4],bb[5],bb[6],bb[7]);

	df1=0;

	Byte2IeeeDoubleFloat(bb,&df1);

	printf("\nByte2IeeeDoubleFloat:\n%.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X ==> %f\n", 
		bb[0],bb[1],bb[2],bb[3],bb[4],bb[5],bb[6],bb[7],df1);

	printf("--------------------------------------------\n");
}

void IbmFloatTest(float f, double df)
{
	unsigned char bb[8];
	float f1=f;
	double df1=df;

	// ibm float to byte

	f1=f;
	memset(bb,'\0',sizeof(bb));

	IbmSingleFloat2ByteL(f1,bb);

	printf("\nIbmSingleFloat2ByteL:\n%f ==> %.2X %.2X %.2X %.2X\n",
		f1,bb[0],bb[1],bb[2],bb[3]);	

	f1=f;
	memset(bb,'\0',sizeof(bb));

	IbmSingleFloat2ByteM(f1,bb);

	printf("\nIbmSingleFloat2ByteM:\n%f ==> %.2X %.2X %.2X %.2X\n",
		f1,bb[0],bb[1],bb[2],bb[3]);	

	f1=0;

	Byte2IbmSingleFloat(bb,&f1);

	printf("\nByte2IbmSingleFloat:\n%.2X %.2X %.2X %.2X ==> %f\n",
		bb[0],bb[1],bb[2],bb[3],f1);	

	printf("--------------------------------------------\n");

	df1=df;
	memset(bb,'\0',sizeof(bb));

	IbmDoubleFloat2ByteL(df1,bb);

	printf("\nIbmDoubleFloat2ByteL:\n%f ==> %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n", df1,
		bb[0],bb[1],bb[2],bb[3],bb[4],bb[5],bb[6],bb[7]);

	df1=df;
	memset(bb,'\0',sizeof(bb));

	IbmDoubleFloat2ByteM(df1,bb);

	printf("\nIbmDoubleFloat2ByteM:\n%f ==> %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n", df1,
		bb[0],bb[1],bb[2],bb[3],bb[4],bb[5],bb[6],bb[7]);

	df1=0;

	Byte2IbmDoubleFloat(bb,&df1);

	printf("\nByte2IbmDoubleFloat:\n%.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X ==> %f\n", 
		bb[0],bb[1],bb[2],bb[3],bb[4],bb[5],bb[6],bb[7],df1);

	printf("--------------------------------------------\n");

}

void VaxFloatTest(float f, double df)
{
	unsigned char bb[8];
	float f1=f;
	double df1=df;

	// vax float test

	f1=f;
	memset(bb,'\0',sizeof(bb));

	VaxSingleFloat2ByteL(f1,bb);

	printf("\nVaxSingleFloat2ByteL:\n%f ==> %.2X %.2X %.2X %.2X\n",
		f1,bb[0],bb[1],bb[2],bb[3]);	

	f1=f;
	memset(bb,'\0',sizeof(bb));

	VaxSingleFloat2ByteM(f1,bb);

	printf("\nVaxSingleFloat2ByteM:\n%f ==> %.2X %.2X %.2X %.2X\n",
		f1,bb[0],bb[1],bb[2],bb[3]);	

	f1=0;

	Byte2VaxSingleFloat(bb,&f1);

	printf("\nByte2VaxSingleFloat:\n%.2X %.2X %.2X %.2X ==> %f\n",
		bb[0],bb[1],bb[2],bb[3],f1);	

	printf("--------------------------------------------\n");
}	

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Canada Canada
My name is Jiyang Hou (or John Hou). I was born in HeiLongJiang province in north east of China. I got all my educations in China. My university major is Geophysics, but my main professional role is software developer. My biggest accomplishment so far is quit smoking about 5 years ago after almost 20 years smoking history. I am still interested on programming beside making living with it like many other developers. I immigrated to Canada in 2003 and became a permanent resident till now. I live in Calgary, Alberta, Canada. You can reach me by jyhou69@gmail.com regarding to any questions, comments, advice, etc.

Comments and Discussions