-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntConvert.java
169 lines (157 loc) · 4.23 KB
/
IntConvert.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* File: IntConvert.java
*
* Copyright (C) 2004 Chris M. Bouzek
* adapted from FORTRAN routines
* originally written by Freddie Akeroyd, ISIS
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Contact : Dennis Mikkelson <[email protected]>
* Chris Bouzek <[email protected]>
* Department of Mathematics, Statistics and Computer Science
* University of Wisconsin-Stout
* Menomonie, WI 54751, USA
*
* This work was supported by the Intense Pulsed Neutron Source Division
* of Argonne National Laboratory, Argonne, IL 60439-4845, USA and by
* the National Science Foundation under grant number DMR-0218882.
*
* For further information, see <http://www.pns.anl.gov/ISAW/>
* $Log$
* Revision 1.4 2004/06/16 21:01:54 kramer
* Now the source displays the cvs logs for the file.
*
*/
/*
* File: IntConvert.java
* Original Author: Freddie Akeroyd, ISIS
*
* Converted to Java March 2004 Chris M. Bouzek
*
*/
package ISIS.JLibGet;
/**
* Routines to convert from VAX to local integer representations
*/
public class IntConvert {
//~ Methods ******************************************************************
/**
* Converts VAX int to local int.
*
* @param i The int to convert.
*
* @return The converted int.
*/
public int VAXToLocalInt( int i ) {
return swapInt( i );
}
/**
* Converts a VAX int array to a local int array.
*
* @param ia The VAX int array to convert.
*/
public void VAXToLocalInts( int[] ia ) {
for( int i = 0; i < ia.length; i++ ) {
ia[i] = swapInt( ia[i] );
}
}
/**
* Converts a VAX short to a local short.
*
* @param s The short to convert.
*
* @return The converted short.
*/
public short VAXToLocalShort( short s ) {
return swapShort( s );
}
/**
* Converts a VAX short array to a local short array.
*
* @param sa The short array to convert.
*/
public void VAXToLocalShorts( short[] sa ) {
for( int i = 0; i < sa.length; i++ ) {
sa[i] = swapShort( sa[i] );
}
}
/**
* Converts the local int to a VAX int.
*
* @param i The local integer.
*
* @return The converted VAX integer.
*/
public int localToVAXInt( int i ) {
return swapInt( i );
}
/**
* Converts a local short to a VAX short.
*
* @param s The short to convert.
*
* @return The converted short.
*/
public short localToVAXShort( short s ) {
return swapShort( s );
}
/**
* Converts a local array to a VAX array.
*
* @param sa
*/
public void localToVAXShorts( short[] sa ) {
for( int i = 0; i < sa.length; i++ ) {
sa[i] = swapShort( sa[i] );
}
}
/**
* Converts a local int array to a VAX int array.
*
* @param ia The int array to convert.
*/
public void localToVaxInts( int[] ia ) {
for( int i = 0; i < ia.length; i++ ) {
ia[i] = swapInt( ia[i] );
}
}
/**
* Swaps the bytes around in the int, e.g.<br>
* <br>
* 0100 1100 0100 1111 0101 0001 0011 1000<br>
* <br>
* goes to<br>
* <br>
* 0011 1000 0101 0001 0100 1111 0100 1100
*
* @param a The int to swap around.
*/
private int swapInt( int a ) {
//tested and passed
return ( int )( ( ( a ) << 24 ) | ( ( ( a ) << 8 ) & 0x00ff0000 ) |
( ( ( a ) >> 8 ) & 0x0000ff00 ) | ( ( long )( a ) >> 24 ) );
}
/**
* Swaps the bytes around in the short.
*
* @param a The short to swap.
*
* @return The byte swapped short.
*/
private short swapShort( short a ) {
return ( short )( ( ( a & 0xff ) << 8 ) | ( ( short )( a ) >> 8 ) );
}
}