Skip to content

Instantly share code, notes, and snippets.

@ingyesid
Created January 28, 2025 14:49
Show Gist options
  • Save ingyesid/5ddeeda43c4536b79a2422992627b74e to your computer and use it in GitHub Desktop.
Save ingyesid/5ddeeda43c4536b79a2422992627b74e to your computer and use it in GitHub Desktop.
fun parseISOMessageWithDebug(message: String): Map<Int, String> {
val result = mutableMapOf<Int, String>()
// Extraer MTI (primeros 4 caracteres)
val mti = message.substring(0, 4)
result[0] = mti
println("MTI: $mti")
// Extraer el Bitmap primario (64 bits = 16 caracteres hexadecimales)
val primaryBitmap = message.substring(4, 20)
val bitmapBits = primaryBitmap.toBigInteger(16).toString(2).padStart(64, '0')
println("Bitmap: $bitmapBits")
// Mostrar qué campos están presentes según el bitmap
for ((index, bit) in bitmapBits.withIndex()) {
if (bit == '1') {
println("Campo ${index + 1} está presente")
}
}
// Iniciar el análisis de los campos después del bitmap
var currentPosition = 20
for ((index, bit) in bitmapBits.withIndex()) {
if (bit == '1') {
when (index + 1) {
2 -> { // Campo 2: PAN (LLVAR)
val length = message.substring(currentPosition, currentPosition + 2).toInt()
currentPosition += 2
result[index + 1] = message.substring(currentPosition, currentPosition + length)
currentPosition += length
}
3, 4, 7, 11 -> { // Campos de longitud fija
val fieldLength = when (index + 1) {
3 -> 6 // Código de procesamiento
4 -> 12 // Monto
7 -> 10 // Fecha y hora
11 -> 6 // Número de referencia
else -> 0
}
result[index + 1] = message.substring(currentPosition, currentPosition + fieldLength)
currentPosition += fieldLength
}
41 -> { // Campo 41: ID del terminal (longitud fija 8)
result[index + 1] = message.substring(currentPosition, currentPosition + 8)
println("Campo 41 (ID del terminal): ${message.substring(currentPosition, currentPosition + 8)}")
currentPosition += 8
}
49 -> { // Campo 49: Código de moneda (longitud fija 3)
result[index + 1] = message.substring(currentPosition, currentPosition + 3)
println("Campo 49 (Código de moneda): ${message.substring(currentPosition, currentPosition + 3)}")
currentPosition += 3
}
else -> {
// Ignorar otros campos por ahora
}
}
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment