Skip to content

Instantly share code, notes, and snippets.

@bubbobne
Last active Aug 29, 2019
Embed
What would you like to do?
deccode data from a magnetic striped card data
package org.andreis.daniele.myApp;
import android.util.Log;
/**
* Created by daniele
*/
public class UsbDecoder {
private String rawData = null;
private String field3 = null;
private String field2 = null;
private String field1 = null;
private boolean isComplete = false;
private boolean isStart = false;
private int position = 0;
private char marker = '!';
public void UsbDecoder(String rawData) {
this.rawData = rawData;
}
public String getField1() {
return field1;
}
public String getField2() {
return field2;
}
public String getField3() {
return field3;
}
public boolean addChar(Character c) {
if (isComplete) {
reset();
}
int cUnicode = (int) c;
if (!Character.isWhitespace(cUnicode)) {
if (c.equals(marker) && (rawData == null || rawData.isEmpty())) {
rawData = String.valueOf(c);
isStart = true;
} else if (c.equals(marker) && isStart) {
rawData = rawData + c;
isComplete = true;
decode();
} else if (isStart && Character.isLetterOrDigit(cUnicode)) {
rawData = rawData + c;
}
}
if(Character.isWhitespace(c) && rawData!=null){
position = rawData.length();
}
return isComplete;
}
private void reset() {
rawData = null;
field1 = null;
field2 = null;
field3 = null;
isComplete = false;
isStart = false;
}
private void decode() {
if (rawData.length() > 19) {
this.field1 = rawData.substring(1, 17);
if (position > 17) {
this.field2 = rawData.substring(17, position);
this.field3 = rawData.substring(position, rawData.length() - 1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment