-
Notifications
You must be signed in to change notification settings - Fork 8
Open
Description
I use this simple ASN1:
CAM DEFINITIONS ::= BEGIN
Latitude ::= INTEGER (-900000000..900000001)
Longitude ::= INTEGER (-1800000000..1800000001)
CAM ::= SEQUENCE {
latitude Latitude,
longitude Longitude
}
END
When I encode and decode 2 long values, I get negative values in the decoded array.
Here is the code I used:
try {
// R.raw.cam is the CAM ASN1
InputStream asn1Stream = getResources().openRawResource(R.raw.cam);
// Initialize ASN.1 Translator
// ByteArrayInputStream schemaStream = new ByteArrayInputStream(camASN1Schema.getBytes());
ASN1Translator translator = new ASN1Translator(
new PERTranslatorFactory(true), // true for aligned PER
Collections.singletonList(asn1Stream)
);
System.out.println("ASN1 Translator successfully created!");
// Sample values for testing
long latitude = 48825214;
long longitude = 9097241;
// ASN.1 JSON structure
String camMessageAsJSON = "{ \"latitude\": " + latitude + ", \"longitude\": " + longitude + " }";
InputStream inputStream = new ByteArrayInputStream(camMessageAsJSON.getBytes());
JSONFormatReader reader = new JSONFormatReader(inputStream, "CAM");
BitArray bitArray = new BitArray();
translator.encode("CAM", bitArray, reader);
byte[] encodedMessage = bitArray.getBinaryArray();
// Create an InputStream from the byte array
InputStream inputStream2 = new ByteArrayInputStream(encodedMessage);
// Create a JSONFormatWriter
JSONFormatWriter formatWriter = new JSONFormatWriter();
// Decode the message
translator.decode("CAM", inputStream2, formatWriter);
// Get the decoded data as a JsonNode
JsonNode decodedMessageJson = formatWriter.getJsonNode();
// Extract latitude and longitude
latitude = decodedMessageJson.get("latitude").asInt();
longitude = decodedMessageJson.get("longitude").asInt();
// Convert latitude and longitude to decimal degrees
double latitudeDeg = latitude / 10000000.0;
double longitudeDeg = longitude / 10000000.0;
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error while encoding or decoding the CAM message: " + e.getMessage());
if (e instanceof NullPointerException) {
// Log further information to debug
System.err.println("NullPointerException occurred: " + e.getStackTrace());
}
}
If I use the boundaries (0..900000001) and (0..1800000001) the encoding and decoding works, but the original ANS1 grammar uses the boundaries I stated above.
Metadata
Metadata
Assignees
Labels
No labels