Click here to Skip to main content
15,881,812 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.1K   1.9K   27  
This program can transform between IEEE, IBM or VAX floating point number formats and their bytes expressions.
/***************************************************************************
                          VaxFloat.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"

// transform vax single float to byte by LOG method
int VaxSingleFloat2ByteL(float VaxSingleFloat, unsigned char bytes[4])
{
// ===================================
// S EEEEEEEE FFFFFFF FFFFFFFF FFFFFFFF
// 0 1      8 9                       31
// byte1    byte0     byte3    byte2
// ===================================	

	unsigned char S;
	int E;
	unsigned long F;

	SingleFloat2SEF(BYLOG, VAX_SINGLE_FLOAT, (double)VaxSingleFloat, &S, &E, &F);

	SingleSEF2Byte(VAX_SINGLE_FLOAT, S, E, F, bytes);

	return 0;
}

// transform vax single float to byte by MULTIPLAE
int VaxSingleFloat2ByteM(float VaxSingleFloat, unsigned char bytes[4])
{
// ===================================
// S EEEEEEEE FFFFFFF FFFFFFFF FFFFFFFF
// 0 1      8 9                       31
// byte1    byte0     byte3    byte2
// ===================================	

	unsigned char S;
	int E;
	unsigned long F;

	SingleFloat2SEF(BYMULTIPLE, VAX_SINGLE_FLOAT, (double)VaxSingleFloat, &S, &E, &F);

	SingleSEF2Byte(VAX_SINGLE_FLOAT, S, E, F, bytes);

	return 0;
}

// transform byte to vax single precision float
int Byte2VaxSingleFloat(unsigned char bytes[4], float * VaxSingleFloat)
{
// ===================================
// S EEEEEEEE FFFFFFF FFFFFFFF FFFFFFFF
// 0 1      8 9                       31
// byte1    byte0     byte3    byte2
// ===================================			

	unsigned char S;
	int E;
	unsigned long F;
	
	SingleByte2SEF(VAX_SINGLE_FLOAT, bytes, &S, &E, &F);

	SingleSEF2Float(VAX_SINGLE_FLOAT, S, E, F, VaxSingleFloat);

	return 0;
}

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