Login  Register

Re: Float.floatToIntBits & decrements in byte values

Posted by Michael Schmid on Dec 06, 2010; 10:28am
URL: http://imagej.273.s1.nabble.com/Float-floatToIntBits-decrements-in-byte-values-tp3686270p3686271.html

Hi Neil,

maybe you have somewhere forgotten the '&255' on conversion from the  
bytes back to an int? You did not enclose the code for this direction.

Michael
________________________________________________________________

On 3 Dec 2010, at 18:24, Thomson Neil (East Kent Hospitals NHS Trust)  
wrote:

> Dear fellow listers
>
> I'm writing a plugin to export a medical image format. Some of the  
> header values are floats. The values are in a hash table and I then  
> save the floats into my header-block by doing a  
> Float.floatToIntBits conversion and then save the 4 byte values,  
> which is (more or less) copied and pasted from the IJ tiff encoder.
>
> I currently have this code snippet in my format encoder:
>
> System.out.println("" + o_value);  //o_value is float value of some  
> hashmap entry
> int v = Float.floatToIntBits((Float) o_value);System.out.println(""  
> + v);
> System.out.println("" + (byte) ((v >>> 24) & 255));
> System.out.println("" + (byte) ((v >>> 16) & 255));
> System.out.println("" + (byte) ((v >>> 8) & 255));
> System.out.println("" + (byte) (v & 255));
> When float values of o_value these are small (say 1E5) everything  
> seems fine, but when they get larger (say 1E7) the byte output does  
> not seem to be as I would expect. If I import and then export ann  
> image without manipulation, the float value decreases, which I've  
> traced to the byte values: if I start by importing an image where  
> this particular float header field has bytes reading (in big-
> endian)...
>
> 76,-46,-20,-18
>
> ... I then export the image and re-import it I get:
>
> 75,-47,-21,-18
>
> ... and again...:
>
> 74,-48,-22,-18
>
> A decrement in three of the four bytes. I'm at a loss as to where  
> to go next and I would greatly appreciate some wise words.
>
> Many thanks, Neil
>
> Neil Thomson,
> Nuclear Medicine Physics Section,
> Medical Physics,
> Kent and Canterbury Hospital,
> UK. CT1 3NG
> +44 (0) 1227 766877
>
> **********************************************************************
> **********************************************
>
> This message may contain confidential information. If you are not  
> the intended recipient please inform the
> sender that you have received the message in error before deleting it.
> Please do not disclose, copy or distribute information in this e-
> mail or take any action in reliance on its contents:
> to do so is strictly prohibited and may be unlawful.
>
> Thank you for your co-operation.
>
> NHSmail is the secure email and directory service available for all  
> NHS staff in England and Scotland
> NHSmail is approved for exchanging patient data and other sensitive  
> information with NHSmail and GSi recipients
> NHSmail provides an email address for your career in the NHS and  
> can be accessed anywhere
> For more information and to find out how you can switch, visit  
> www.connectingforhealth.nhs.uk/nhsmail
>
> **********************************************************************
> **********************************************