ERLEDIGT
JA
JA
ANTWORTEN
8
8
ZUGRIFFE
434
434
EMPFEHLEN
-
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 :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
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.
Code 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
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
Geändert von sheel (07.10.11 um 14:14 Uhr) Grund: Codetags
-
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)?Netiquette (vA §15) und Nutzungsregeln (vA §4.8) einhalten! Programmcode in Codetags/Codeboxen.
Sehr gute Beiträge bitte Bewerten (Stern darunter oder "Danke").
"Funktioniert nicht" ist zu ungenau! Code, Fehlermeldungen, Verhalten des Programms, ...?
-
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
-
07.10.11 17:19 #5
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.
Code java:1 2 3 4 5 6 7 8
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?Geändert von HonniCilest (07.10.11 um 17:36 Uhr)
Jeder Fehler, aus dem wir lernen, ist ein Erfolg...
...Aber mach' nicht den Fehler, nicht aus deinen Fehlern zu lernen.
-
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.
-
10.10.11 19:07 #7
- Registriert seit
- Jun 2009
- Beiträge
- 870
warum nicht
?Code java:1
bytes[i]=is.readByte();
Code bitte so einfügen: [java]System.out.println("Hallo");[/java] (Analog für andere Programmiersprachen)
hilfreich zu Java: Really Big Index, Java ist auch eine Insel Band 1 und Band 2.Code java:1
System.out.println("Hallo");
___________
Ubuntu Bug #1: Microsoft has a majority market share
Casecon: Projekt leiser Käse
-
11.10.11 10:29 #8
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
Jeder Fehler, aus dem wir lernen, ist ein Erfolg...
...Aber mach' nicht den Fehler, nicht aus deinen Fehlern zu lernen.
-
11.10.11 11:26 #9
- Registriert seit
- Jun 2005
- Beiträge
- 8.168
Hi.
Das Problem ist, dass Windows Little Endian verwendet, Java allerdings Big Endian. Du mußt also die Bytes erst ein bißchen umsortieren:
GrußCode java:1 2 3 4
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);
If at first you don't succeed, try again. Then quit. No use being a damn fool about it.
Ähnliche Themen
-
AS1/2 Code in AS3 Code umwandeln
Von TarAldarion im Forum Flash PlattformAntworten: 4Letzter Beitrag: 06.06.10, 02:21 -
java code zu html code parsen
Von dontschew im Forum JavaAntworten: 1Letzter Beitrag: 22.08.07, 21:42 -
Assembler Code in C code umwandeln
Von BaYan im Forum C/C++Antworten: 3Letzter Beitrag: 01.12.05, 19:15 -
Tool das automatisch den Java Code an die Code Conventions anpasst
Von The-God im Forum JavaAntworten: 1Letzter Beitrag: 30.10.05, 13:29 -
PHP Code in JS Code umwandeln
Von LoMo im Forum Javascript & AjaxAntworten: 9Letzter Beitrag: 31.03.05, 08:31





Zitieren

Login





