From: Oleg Komarov on 12 Jul 2010 10:42 Dear all, I have a binary file where each record is 53 byte long. Starting from byte 26 to byte 29 I have a Microsoft Binary Float used to store a date which should be in the format 'yyyymmdd'. So, if I read the file in with fread(fid,4,'uint32') I get 4 values which I assume are the first chunk of 8 bits, the second chunk of 8 bits etc...according to the followin schema: 32 24 23 16 15 8 7 0 XEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM I need to convert this MBF format to IEEE and I found this code: function output=MBF2IEEE(input); % Convert 32 bit Microsoft Basic Float into IEEE format for MatLab % If input is an array, all values will be converted. % mask1=16777215 ; % 2^24-1 ; % bottom 24 bits holds the mantissa % mask2=4278190080 ; % bitxor(2^32-1,mask1) ; % top 8 bits holds the exponent % sign=bitget(input,24) ; % hi bit in mantissa % mantissa=bitset(bitand(input,mask1),24) ; % restore hidden bit % exponent=bitshift(bitand(input,mask2),-24)-152 ; % scale exponent % sign=((bitget(input,24)==0)*2)-1 ; % +1 for zero or positive, -1 for negative % zeros=(input~=0) ; % 0 for zero values else +1 % output=mantissa*2^exponent*zeros*sign, as done below: output= bitset(bitand(input,16777215),24)... .*(power(2,(bitshift(bitand(input,4278190080),-24)-152)))... .*(input~=0).*(((bitget(input,24)==0)*2)-1) ; return; I simply don't get it...any help (especially explanations or a link to the basics on bits manipulation) would be much appreciated. Thank Oleg
From: James Tursa on 12 Jul 2010 12:41 "Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <i1f9js$235$1(a)fred.mathworks.com>... > > I have a binary file where each record is 53 byte long. > Starting from byte 26 to byte 29 I have a Microsoft Binary Float used to store a date which should be in the format 'yyyymmdd'. > > So, if I read the file in with fread(fid,4,'uint32') I get 4 values which I assume are the first chunk of 8 bits, the second chunk of 8 bits etc...according to the followin schema: > 32 24 23 16 15 8 7 0 > XEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM > > > I need to convert this MBF format to IEEE and I found this code: > > function output=MBF2IEEE(input); > > % Convert 32 bit Microsoft Basic Float into IEEE format for MatLab > % If input is an array, all values will be converted. > > % mask1=16777215 ; % 2^24-1 ; % bottom 24 bits holds the mantissa > % mask2=4278190080 ; % bitxor(2^32-1,mask1) ; % top 8 bits holds the exponent > % sign=bitget(input,24) ; % hi bit in mantissa > % mantissa=bitset(bitand(input,mask1),24) ; % restore hidden bit > % exponent=bitshift(bitand(input,mask2),-24)-152 ; % scale exponent > % sign=((bitget(input,24)==0)*2)-1 ; % +1 for zero or positive, -1 for negative > % zeros=(input~=0) ; % 0 for zero values else +1 > % output=mantissa*2^exponent*zeros*sign, as done below: > > output= bitset(bitand(input,16777215),24)... > .*(power(2,(bitshift(bitand(input,4278190080),-24)-152)))... > .*(input~=0).*(((bitget(input,24)==0)*2)-1) ; > return; > > I simply don't get it...any help (especially explanations or a link to the basics on bits manipulation) would be much appreciated. Are you just trying to understand the algorithm, or are you saying that this is not working for you? James Tursa
From: dpb on 12 Jul 2010 17:07 Oleg Komarov wrote: .... > I simply don't get it...any help (especially explanations or a link to > the basics on bits manipulation) would be much appreciated. .... <http://support.microsoft.com/kb/42980> for the MBF tutorial. If you mean the actual bit-banging itself, that's pretty well covered in the online documentation. --
From: James Tursa on 12 Jul 2010 17:37 dpb <none(a)non.net> wrote in message <i1g0ar$hfh$1(a)news.eternal-september.org>... > > <http://support.microsoft.com/kb/42980> > > for the MBF tutorial. That was a very useful link, in which it is stated: Sign Bits Exponent Bits Mantissa Bits --------- ------------- ------------- IEEE 1 11 52 + 1 (Implied) MBF 1 8 56 So it looks like MBF does *not* have a hidden leading mantissa bit. Bottom line is the 'VAXD' options stuff will not work with this format as is. So custom code will be required. However, it should be fairly easy to modify the custom 'VAXD' code to work with this. James Tursa
From: James Tursa on 12 Jul 2010 19:20
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i1g1u0$g2l$1(a)fred.mathworks.com>... > dpb <none(a)non.net> wrote in message <i1g0ar$hfh$1(a)news.eternal-september.org>... > > > > <http://support.microsoft.com/kb/42980> > > > > for the MBF tutorial. > > That was a very useful link, in which it is stated: > > Sign Bits Exponent Bits Mantissa Bits > --------- ------------- ------------- > IEEE 1 11 52 + 1 (Implied) > MBF 1 8 56 > > So it looks like MBF does *not* have a hidden leading mantissa bit. Bottom line is the 'VAXD' options stuff will not work with this format as is. So custom code will be required. However, it should be fairly easy to modify the custom 'VAXD' code to work with this. But now that I look at it again, it is confusing. 1 + 8 + 56 = 65. This doesn't add up to 64, so something is wrong with the description. Maybe the 56 should have been 55 + 1 ?? I need to find a more descriptive link ... James Tursa |