It is currently 19 Oct 2017 01:28

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: 18 Jun 2017 23:32 
Offline

Joined: 12 Jan 2013 04:17
Posts: 42
Hello,

I'm looking to convert a 4 byte hex string in the IEEE 754 format to its floating point decimal value in MikroBasic.

Does anyone have a library function already made for this?

For example; 0x3E200000 would convert to 0.15625

I believe that MikroC has this functionality built in, but I can't find it in MikroBasic.

If no one has it, I'll have to put my thinking cap on.

Here are some pages that explain the complexities:

https://www.h-schmidt.net/FloatConverter/IEEE754.html
http://teaching.idallen.com/cst8281/10w ... rsions.txt
https://techdocs.altium.com/display/FPG ... Aodg80EaQ#

Regards to all,

Stuart


Top
 Profile  
 
PostPosted: 03 Jul 2017 23:08 
Offline

Joined: 12 Jan 2013 04:17
Posts: 42
I've put the thinking cap on and constructed the function:

Calling the function with example: 0x3E200000 will return 0.15625

Sub Function IEEE754_to_Extended(dim Byte3, Byte2, Byte1, Byte0 as byte) as Extended
  dim sign as short
  dim exponent as byte
  dim final_exp as integer
  dim value as byte[24]
  dim Mantissa as float
  dim Counter as byte
 
  'initialize
  Mantissa = 0
 
  'Calculate the sign
  sign = Byte3.B7       'bit 31 or bit 7 of byte3 is the sign
  if sign = 0 then      'strip the sign
   sign = 1
  else
   sign = -1
  end if
 
  'Calculate the exponent
  exponent = byte3 << 1   'bits 0-6 of byte3 become bits 1-7 of exponent
  exponent.B0 = byte2.B7  'bit 0 of exp is set to bit 7 of byte2
  final_exp = exponent - 127
 
  'Build the mantissa array
  value[23]=Byte0.B0  value[22]=Byte0.B1 value[21]=Byte0.B2 value[20]=Byte0.B3
  value[19]=Byte0.B4  value[18]=Byte0.B5 value[17]=Byte0.B6 value[16]=Byte0.B7
 
  value[15]=Byte1.B0 value[14]=Byte1.B1 value[13]=Byte1.B2 value[12]=Byte1.B3
  value[11]=Byte1.B4 value[10]=Byte1.B5 value[9]=Byte1.B6 value[8]=Byte1.B7
 
  value[7]=Byte2.B0 value[6]=Byte2.B1 value[5]=Byte2.B2 value[4]=Byte2.B3
  value[3]=Byte2.B4 value[2]=Byte2.B5 value[1]=Byte2.B6 value[0]=1
 
  'calculate the mantissa
   For Counter = 0 to 23
    if value[Counter] = 1 then
      Mantissa = Mantissa + pow(2, (Counter * -1))  'Sum all the points
    end if
   Next Counter

  'Calculate result = sign x (1 + mantissa) x 2^e   e= exponent-127
  Result = (pow(2, final_exp) * Mantissa) 'need to calculate 1 step a time otherwise generates error
  Result = (Result * sign)
end sub


Result=IEEE754_to_Extended(0x3E, 0x20, 0x00, 0x00) ' equals 0.15625




Regards,

Stuart


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: