Hallo Leute,
Ich habe einen Skript bekommen in VBA was ich in Java bräuchte. Ich habe versucht es in Java umzuwandeln, aber irgendwie kriege ich nicht das richtige rausgelesen.
VBA Code:
Das habe ich in Java geschrieben.
kriege immer falsche Fliesskommazahlen.
Ich hoffe einer kann mir helfen

Ich habe einen Skript bekommen in VBA was ich in Java bräuchte. Ich habe versucht es in Java umzuwandeln, aber irgendwie kriege ich nicht das richtige rausgelesen.
VBA Code:
Code:
Dim arrSource(0 To 10005) As ImportRow
zeit = 0
t = 2
On Error GoTo error
file_length = FileLen(Filename)
ReDim bytes(1 To file_length / 4)
Open Filename For Binary Access Read As #1
Get #1, 1, bytes
Close #1
ampl0 = bytes(2)
ampl1 = bytes(3)
offset0 = bytes(4)
offset1 = bytes(5)
offset_error0 = bytes(6)
offset_error1 = bytes(7)
Time = bytes(8)
samples = bytes(9)
ReDim daten(1 To file_length / 2)
Open Filename For Binary As #1
For i = 1 To file_length / 2
Get #1, , daten(i)
Next i
Close #1
For i = 33 To file_length / 2
wert = daten(i)
U1 = (wert / 32768 - 1) * ampl1 / 2 + offset1 - offset_error1
i = i + 1
wert = daten(i)
U0 = (wert / 32768 - 1) * ampl0 / 2 + offset0 - offset_error0
If U0 < -5 Then U0 = U0 + 20
arrSource(t).Time = zeit
arrSource(t).U0 = U0
arrSource(t).U1 = U1
t = t + 1
zeit = Round(zeit + Time, 5)
Next i
Das habe ich in Java geschrieben.
Java:
public static void main(String[] args) {
float zeit = 0;
short wert;
float U0,U1;
ArrayList werteliste = new ArrayList();
String filename="C:\\Dokumente und Einstellungen\\sueyilma\\Desktop\\Abschlussprojekt\\002F6BC9.MSA";
File file = new File(filename);
DataInputStream is = null;
try {
is = new DataInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int size = (int)file.length();
float[] bytes = new float[size/4];
try {
bytes[0]=is.readFloat();
bytes[1]=is.readFloat();
bytes[2]=is.readFloat();
bytes[3]=is.readFloat();
bytes[4]=is.readFloat();
bytes[5]=is.readFloat();
bytes[6]=is.readFloat();
bytes[7]=is.readFloat();
bytes[8]=is.readFloat();
} catch (IOException e) {
e.printStackTrace();
}
float ampl0 = bytes[1];
float ampl1 = bytes[2];
float offset0 = bytes[3];
float offset1 = bytes[4];
float offset_error0 = bytes[5];
float offset_error1 = bytes[6];
float Time = bytes[7];
float samples = bytes[8];
short[] daten = new short[size/2];
for(int j=0;j<size/2;j++)
{
try {
daten[j]= is.readShort();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int c = 33; c < size / 2;c++)
{
wert = daten[ c];
U1 = (wert / 32768 - 1) * ampl1 / 2 + offset1 - offset_error1;
wert = daten[ c];
U0 = (wert / 32768 - 1) * ampl0 / 2 + offset0 - offset_error0;
if (U0 < -5) {U0 = U0 + 20;}
werteliste.add(zeit);
werteliste.add(U0);
werteliste.add(U1);
zeit = Round(zeit + Time,5);
}
for (int k = 0; k < werteliste.size(); k++) {
System.out.println(werteliste.get(k));
}
float g = (float) 4.2344555643545;
System.out.println(Round(g,5));
}
public static float Round ( float Rval, int Rpl ) {
float p = ( float ) Math.pow ( 10 ,Rpl ) ;
Rval = Rval * p;
float tmp = Math.round ( Rval ) ;
return ( float ) tmp/p;
}
kriege immer falsche Fliesskommazahlen.
Ich hoffe einer kann mir helfen


Zuletzt bearbeitet von einem Moderator: