VBA Code umwandeln zum Java Code

zorey

Grünschnabel
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:
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:
Hi und Willkommen bei tutorials.de :)

Hast du schon mal mit dem Debugger versucht, die Stelle zu finden
(wo die Werte vorher noch richtig waren, dann aber nicht mehr)?
 
also beim debuggen kriege ich im vba code

ampl0 = bytes(2) // 20
ampl1 = bytes(3) // 20
offset0 = bytes(4) // 0
offset1 = bytes(5)// 0
offset_error0 = bytes(6) // ..
offset_error1 = bytes(7) //..
Time = bytes(8) // 0,00002
samples = bytes(9)//...

und in java ganz andere ergebnisse

ich mache was beim einlesen oder beim umwandeln in float was falsch denke ich die variablen in vba sind in single in der definition stand 4 byte und in java wäre das denke ich float
 
Dim bytes() As Single
Dim file_length As Long
Dim i, t, wert, row As Integer
Dim ampl0, ampl1, offset0, offset1, offset_error0, offset_error1 As Single
Dim Time, samples, zeit As Single
Dim U0, U1 As Single
 
Bei VBA muss man aufpassen, es irritiert oft, dass MS Office seine Objekte mit 1 beginnend nummeriert. Jedoch werden Arrays wie bei Java auch mit 0 beginnend nummiert (wenn nicht anders mit "Option Base 1" angegeben), d.h.
Java:
       float ampl0 = bytes[2];
       float ampl1 = bytes[3];
       float offset0 = bytes[4];
       float offset1 = bytes[5];
       float offset_error0 = bytes[6];
       float offset_error1 = bytes[7];
       float Time = bytes[8];
       float samples = bytes[9];

Edit: Hab das ReDim übersehen... Was bekommst du für Ergebnisse in Java?
 
Zuletzt bearbeitet:
Ich kriege in Java bei den Code diese Werte:

bytes[0]=is.readFloat(); //3.5442647E9
bytes[1]=is.readFloat();//5.7488E-41
bytes[2]=is.readFloat();//5.7488E-41
bytes[3]=is.readFloat();//0.0
bytes[4]=is.readFloat();//0.0
bytes[5]=is.readFloat();//5.7477E-41
bytes[6]=is.readFloat();//5.7477E-41
bytes[7]=is.readFloat();//-5.6176413E-12
bytes[8]=is.readFloat();//0.0

Das Problem konnte ich leider immer noch nicht lösen können. :(
 
Weil du dich in dem Moment vom Namen irritieren lässt, ein float besteht aus 4 Byte. Ich habe mich davon auch am Anfang irriteren lassen, vielleicht ein guter Tipp an Zorey, bitte den Namen des Arrays ändern, damit Verwirrungen ausgeschlossen sind :)
 
Hi.

Das Problem ist, dass Windows Little Endian verwendet, Java allerdings Big Endian. Du mußt also die Bytes erst ein bißchen umsortieren:
Java:
byte[] b = new byte[4];
in.readFully(b, 0, 4);
	
float x = Float.intBitsToFloat(b[0]&0xff | (b[1]&0xff) << 8 | (b[2]&0xff) << 16 | b[3] << 24);
Gruß
 
Zurück