11-03-2017 12:17 PM
Hello All,
Can anyone help me how to write floating point number with specific storage format and output as HEX i need, i will be writing the data 16 bit data
For example, I have a floating point number -177.609375 and storage format is s8.7 then i suppose to get HEX A732 .
I have done code like
EX:1
i took the HEX A732 and storage format s8.7( in the main VI Enter 7 in the f control and in the signed unsigned control write s, in HEX write A732 ) i got the Value as -177.609375 this is valid.
Ex:2
i took the HEX A732 and storage format s15.0 i got the Value as -22734 this is valid as per our requirement.
(I have attached the Code for above examples)
Now by taking DBL value -177.609375 and storage format s8.7 as inputs i the output should be HEX A732
Regards,
Nafees
Solved! Go to Solution.
11-05-2017 09:57 AM
@shaik89 wrote:
Hello All,
Can anyone help me how to write floating point number with specific storage format and output as HEX i need, i will be writing the data 16 bit data
For example, I have a floating point number -177.609375 and storage format is s8.7 then i suppose to get HEX A732 .
Regards,
Nafees
First off I'm not exactly sure what your "specific storage format" is. Never heard of s8.7, but I looked at your code and I think I understand what the data format is.
Speaking about looking at your code, it is VERY obtuse. You have a main VI that calls 4 subVIs. You have multiple For Loops, multiple case structures. You convert back and forth from U16 to floating point to String. Your algorithm can be greatly simplified if you work directly with the bits. I got it done in a simple block diagram. I'm sure my algorithm can be further optimized.
You only have 1 exemplar for your format: 0xA732 => -177.609375. This block diagram works for this one conversion but I think it will work on others. Try it out and VERIFY it works correctly for other hex numbers BEFORE you use it.
Now on to your next problem: going from Floating Point to Hex. This one was a little trickier. To work properly, you MUST truncate the U16 number (Round Towards -Infinity) to avoid any carry over of fractional part to the whole number part. However, I'm assuming you would like to "Round to Nearest" value. So to get around the truncate requirement, I added an offset of 1/2 of the smallest fractional step size less a small epsilon before the Round Towards -Infinity.
This is the inverse function of the above algorithm. This will work ONLY if my assumptions of the above Hex_2_Float algorithm are correct.
If you can't read 2017 version, I attached a 2014 VI version.
11-05-2017 10:04 AM - edited 11-05-2017 10:12 AM
p.s., The Float to Hex.vi only works for values -256 < x < +256. It's up to you to make sure the Floats are within the valid range. Actually, since it Rounds to Nearest, the valid range is closer to -255.996 < x < +255.996.
11-05-2017 10:37 AM - last edited on 11-06-2017 11:22 AM by Kristi_Martinez
Hello jamiva,
i have attached the format specification snap.
i have develop code with a single logic which will work for all the format specification and HEX
In the code what i did you can enter hex value and format specification f then you will get the value.
in the same way i want to enter format specification and value i need to get the relevant HEX for that
for Example if format specification is 8.0 enter 0 in the f control and hex is 3A the value is 58
if format specification is s15.0 write s in signed or unsigned string control and 0 in f control and HEX is A732 the value you get is -22734
if format specification is s8.7.0 write s in signed or unsigned string control and 7 in f control and HEX is A732 the value you get is-177.609375
now, In the same way i have differernt format specifications based on the format specification and value i want a HEX for that
Regards,
Nafees
11-05-2017 11:51 AM
OK that spec made a lot of sense. I also over complicated the algorithm. It is really simple!!! BUT you need to know what format spec you are dealing with ahead of time, hence the "Format Spec" Enum Control. Here is what I have so far for the HEX to Float Conversion. I need to sign off. Will look at the Inverse Float to Hex later.
11-05-2017 12:07 PM
A little nugget. You can get rid of the reciprocal function and change the input to the Compound Arithmetic function to Invert. Both results are equivalent. Just looks cleaner.
11-05-2017 12:10 PM
Hello jamiva,
Thanks for your prompt response,your logic was awesome and simple.
it would be great if you save the vi for 2014 and share.
Best Reagrds,
Ali
11-05-2017 01:33 PM - edited 11-05-2017 01:35 PM
Hello jamiva,
the above logic does't work for hex FDCA and format specifier 0.16 * e power-2 i suppose to get value as 0.247840881348 valid response according to the document i attached but am getting value -0.00215911865234375 this is invalid response(in your logic i here changed f as 16 and e as -2) , when you see my main vi you can give the HEX FDCA format as f 16, e is -2 then you will get the value 0.247840881348.
Regards,
Nafees
11-06-2017 09:06 AM - last edited on 11-06-2017 11:27 AM by Kristi_Martinez
@shaik89 wrote:
Hello jamiva,
the above logic does't work for hex FDCA and format specifier 0.16 * e power-2 i suppose to get value as 0.247840881348 valid response according to the document i attached but am getting value -0.00215911865234375 this is invalid response(in your logic i here changed f as 16 and e as -2) , when you see my main vi you can give the HEX FDCA format as f 16, e is -2 then you will get the value 0.247840881348.
Regards,
Nafees
The png file you attached clipped the spec for the 0.16x2^-2. So I didn't include it in the previous VI.
First:
I don't think you fully understand the syntax of the format specifier. "s" means "signed" so you would cast it to a signed integer (I8 or I16 etc). Without the "s" it implies an unsigned integer so you need to cast it to U8 or U16 et.). For the above format, you need a U16.
Second:
I had a bug in the "e" and "f" constants. They are Unsigned constants, so you can't change it to a value of -2. I updated the VI to have them define as I32 instead. Added a case to the ENUM & fixed the bug. It works fine now. (ver 2014 VI is attached)
As far as the "Inverse Function" goes, it is VERY simple algebra.
If:
Float = Ib x 2^e / 2^f
then the inverse is (solve for Ib):
Ib = Float x 2^f / 2^e
I will try and find time today to help you out. Try coding it up yourself. Shouldn't be too hard.
11-06-2017 09:55 AM
Hello jamiva,
Thanks for you response,
This logic was nice and simple it has reduced my code, but if i have some 35 to 40 storage formats i need to create this many cases still this works for me.
I will try to do the same for floating value and storage format to get a HEX it would be great if you share a logic for single format.
Thanks and Regards,
Nafees.... 🙂