[ http://issues.apache.org/jira/browse/IO-101?page=comments#action_12455796 ] Henri Yandell commented on IO-101: ----------------------------------
[That has to be the most annoying thing.... I'd done a beautiful write up as I worked through this and the damn machine froze... I'd got used to that not being a problem] Anyways.... Your first double of 34.345 leads to the following input and output when passed through writeSwapped and readSwapped: 4630030446347063132 is 0x40412C28,F5C28F5C 4630030442052095836 is 0x40412C27,F5C28F5C Output of writeXxx is 92, -113, -62, -11, 40, 44, 65, 64 which is: 5C, 8F, C2, F5, 28, 2C, 41, 40 so by inspection that's correct. So the problem is in the readSwapped method. In readSwapped we get two longs, a high and a low. The high is 1078012968, which is 0x40412C28. So that's good. The low is -171798692. The negative is a bit worrisome there. According to OS X Calculator it should be 4123168604, which I can confirm as that gives the correct answer when added to (high << 32). Looking at the calculation of the low long, we get four numbers that are added together: 92, 36608, 12713984, -184549376 Again the negative is worrisome. Assuming it is incorrect and the others are correct, it should be 4110417920. This is a big number, so I bet we have an int which is wrapping. Changing the line from: ( data[ offset + 3 ] & 0xff ) << 24; to ( (long) ( data[ offset + 3 ] & 0xff )) << 24; gives the correct answer. Shifting 0xff by 8, 16 and 24, only 24 goes over the top. So I think we only need this on the two '<< 24' lines. > The method EndianUtils.writeSwappedDouble() and > EndianUtils.readSwappedDouble() do not match! > --------------------------------------------------------------------------------------------- > > Key: IO-101 > URL: http://issues.apache.org/jira/browse/IO-101 > Project: Commons IO > Issue Type: Bug > Components: Streams/Writers > Affects Versions: 1.2 > Environment: I was running Windows XP SP2 and using Commons IO 1.2, > Java 1.5 update 9 when I got this problem. > Reporter: José Pinto > Priority: Critical > Fix For: 1.4 > > > Code: > public static void main(String[] args) { > double[] tests = new double[] {34.345, -345.5645, 545.12, > 10.043, 7.123456789123}; > for (int i = 0; i< tests.length ;i++) { > byte[] buffer = new byte[8]; > EndianUtils.writeSwappedDouble(buffer, 0, tests[i]); > double val = EndianUtils.readSwappedDouble(buffer, 0); > System.out.println(val); > } > > } > Result: > 34.344969482421874 > -345.5645 > 545.11951171875 > 10.043 > 7.123456789123 > Note: > In my opinion the values shouldn't be changed at all. -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
