Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unpack

Status
Not open for further replies.

malpa

Technical User
Feb 8, 2004
122
CO
Hi,

I have binary file, and I want to decode it.
This is the structure

#campo1: secuencia OCTET STRING (SIZE(4) CODE("LEFT") ) OPTIONAL,
#campo2_4: OCTET STRING (SIZE(2) CODE("LEFT") ) OPTIONAL, -- 2:TIPO DE REGISTRO, 4:TIPO ABONADO LLAMANTE, 10:HAZ ENTRANTE
#campo5_7: OCTET STRING (SIZE(1) CODE("LEFT") ) OPTIONAL, -- 5:CANAL ISDN, 2:TIPO DESTINACION DE LLAMADA, 1:FILLER
#campo8_9: OCTET STRING (SIZE(1) CODE("LEFT") ) OPTIONAL, -- 4:LONG RADICAL DEL LLAMANTE, 4:LONG TELF. LLAMANTE
#campo10: abonadoAOCTET STRING (SIZE(6) CODE("LEFT") ) OPTIONAL,
#campo11_12: OCTET STRING (SIZE(1) CODE("LEFT") ) OPTIONAL, -- 4:TIPO LLAMADA, 4:TIPO SERVICIO
#campo13: cifrasTecleadasOCTET STRING (SIZE(9) CODE("LEFT") ) OPTIONAL,
#campo14: duracionOCTET STRING (SIZE(4) CODE("RIGHT")) OPTIONAL,
#campo15: fecha OCTET STRING (SIZE(4) CODE("LEFT") ) OPTIONAL,
#campo16: impulsosOCTET STRING (SIZE(2) CODE("LEFT") ) OPTIONAL,
#campo17_21: OCTET STRING (SIZE(2) CODE("LEFT") ) OPTIONAL, -- 2:RESULT LLAMADA, 1:INDICADOR ACT FECHA/HORA, 1:SOBREFLUJOS DE IMPULSOS, 6:FILLER, 6:TIPO ORIGEN
#campo22_25: OCTET STRING (SIZE(2) CODE("LEFT") ) OPTIONAL, -- 2:IND. RESPUESTA, 2:IND. REGISTRO LLAMADA LARGA DURACION, 10:COD. HAZ SALIENTE, 2:TIPO CONECTIVIDAD
#campo26: numeracionAbrvOCTET STRING (SIZE(1) CODE("LEFT") ) OPTIONAL,
#campo27_28: OCTET STRING (SIZE(1) CODE("LEFT") ) OPTIONAL -- 4:CIFRA DISCRIMINATORIA DE IDIOMA, 4:FILLER


And this is my programm, but it doesn´t prints the fields correctly.
I think I'm not using unpack command correctly.

40 = means bytes

while ( sysread (FILE1, my $rec,40) )
{
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
my @reg3 = unpack ("H2H2H2H2b2b4b10b5b2b1b4b4H2H2H2H2H2H2b4b4H2H2H2H2H2H2H2H2H2H2H2H2H2H2H2H2H2H2H2b2b1b1b6b6b2b2b10b2H2b4b4",
$rec);

$SecRec=$reg3[1]."".$reg3[0]."".$reg3[3]."".$reg3[2];
$Sec = hex ( $SecRec );
$t_p=$reg3[4] ; # tipo registro
$t_ab_ll=$reg3[5] ; # tipo abonado llamanta
$h_in=$reg3[6] ; # haz entrante
$c_i=$reg3[7] ; # canal isdn
$t_d=$reg3[8]; # tipo destinacion de llamada
$fill=$reg3[9]; # filler
$l_r=$reg3[10]; # long radical del llamante
$l_t=$reg3[11]; # long tel llamante
$Abonado1 = $reg3[12] ;
$Abonado2 = $reg3[13] ;
$Abonado3 = $reg3[14] ;
$Abonado4 = $reg3[15] ;
$Abonado5 = $reg3[16] ;
$Abonado6 = $reg3[17] ;
$tipo_llamada = $reg3[18] ; # tipo llamada
$tipo_servicio = $reg3[19] ; #tipo servicio

$ct1 = $reg3[20]; #cifras tecleadas
$ct2 = $reg3[21]; #cifras tecleadas
$ct3 = $reg3[22]; #cifras tecleadas
$ct4 = $reg3[23];
$ct5 = $reg3[24];
$ct6 = $reg3[25];
$ct7 = $reg3[26];
$ct8 = $reg3[27];
$ct9 = $reg3[28]; #cifras tecleadas
$cte = $ct1.":".$ct2.":".$ct3.":".$ct4.":".$ct5.":".$ct6.":".$ct7.":".$ct8.":".$ct9."\n";
$dura = $reg3[30]."".$reg3[29]."".$reg3[32]."".$reg3[31];
$Segundos = hex ( $dura );

$f1 = $reg3[34];
$f2 = $reg3[33];
$f3 = $reg3[36];
$f4 = $reg3[35];

$impu1 = $reg3[37];
$impu2 = $reg3[38];
$campo17 = $reg3[39];
$campo18 = $reg3[40];
$campo19 = $reg3[41];
$campo20 = $reg3[42];
$campo21 = $reg3[43];
$campo22 = $reg3[44];
$campo23 = $reg3[45];
$campo24 = $reg3[46];
$campo25 = $reg3[47];
$num_abr = $reg3[48];
$campo27 = $reg3[49];
$campo28 = $reg3[50];

for ($i=4;$i<=12;$i++){
# if ($i==0){
print $reg3[$i]."!"
#}
}
print "\n";
}
close(FILE1);

Thanks a lot for your assistance.
 
Hi I´m sorry.

Again

This is my binary file

od -x -w40 file | head -5

0000000 c800 1bce 7c04 01aa 1346 3337 9300 0178 6568 f9ff ffff ffff 0000 2002 1f49 cc25 0000 0014 803e ff00
0000050 c800 1cce fc04 0188 3135 9503 0000 0178 1382 f2ff ffff ffff 0000 3a00 1f49 b427 0000 0014 d000 ff00
0000120 c800 1dce fc02 01aa 1333 2967 6200 0178 9332 f8ff ffff ffff 0000 a200 1f49 4e27 0000 0014 d000 ff00
0000170 c800 1ece 7c04 01aa 1357 5106 3400 0168 4008 f0ff ffff ffff 0000 4100 1f49 af27 0000 0014 d000 ff00
0000240 c800 1fce 7c04 01aa 1337 3840 9000 0178 8772 f6ff ffff ffff 0000 1000 1f49 ea27 0000 0014 6000 ff00


and this is the structure

Campo1: Secuencia 4 bytes
Campo2: Tipo de Registro 2 bits
Campo3: Tipo de Abonado 4 bits
Campo4: Haz Entrante 10 bits
Campo5: Canal ISDN 5 bits
Campo6: Tipo Destino 2 bits
Campo7: Filler 1 bits
Campo8: Longitud Radical 4 bits
Campo9: Longitud Abonado A 4 bits
Campo10: Abonado A 6 bytes
Campo11: Tipo de Llamada 4 bits
Campo12: Tipo de Servicio 4 bits
Campo13: Abonado B 9 bytes
Campo14: Duraciòn Llamada (en segundos) 4 bytes
Campo15: Año Mes dia hora Minutos Segundos 4 bytes
Campo16: Impulsos 2 bytes
Campo17: Resultado 2 bits
Campo18: Indice Fecha 1 bits
Campo19: Indice Sobre Flujo 1 bits
Campo20: Filler 6 bits
Campo21: Tipo Origen 6 bits
Campo22: Indice Respuesta 2 bits
Campo23: Indice Registro Larga Duraciòn 2 bits
Campo24: Haz Saliente 10 bits
Campo25: Tipo Conectividad 2 bits
Campo26: Numero Abreviado 1 bytes
Campo27: Idioma 4 bits
Campo28: Filler 4 bits

I want to convert the original file to this output:
---------------------------------------------------

1234567|0|0|1000|0|1|0|8|8|54215165|1|0|0553509123|749|20000131172232|0|0|0|0|1|5|0|0|14|1|15|15|0

I was working on it and this is my initial programm

#!/usr/bin/perl

$[=1;
$file1 = $ARGV[0];
open(FILE1,"<$file1") or die "Input file: Cannot open file\n\n";

while ( sysread (FILE1, my $rec,40) )
{

my @reg3 = unpack ("H2H2H2H2 B16 B8 B8 h2h2h2h2h2h2 B8 h2h2h2h2h2h2h2h2h2 H2H2H2H2 H2H2H2H2 H2H2 B16 B16 H2 B8",$rec);

$SecRec=$reg3[2]."".$reg3[1]."".$reg3[4]."".$reg3[3];
$Sec = hex ( $SecRec );
$tr=bin2dec(substr($reg3[5],1,2));
$ta=bin2dec(substr($reg3[5],3,4));
$haz_entr=bin2dec(substr($reg3[5],7,10));

}
close(FILE1);

#I took this piece of program to turn or convert to decimal

sub bin2dec {
return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}

I feel my English is very bad!!

Thanks a lot





 
Hi,


First, thank you.


My initial post, I made a mistake with the unpack function. But the previous I solve the problem.

Regarding with this function only convert a binary string to decimal.

Only, adjust to 32 ceros binary string the variable.

substr("0" x 32 . shift, -32) after this

------
$a=9999;
$c=b($a);
print $c."\n";
sub b {
return substr("0" x 32 . shift , -32 );
}
otuput
00000000000000000000000000009999
---------


I finish the programm. But my real problem is that I dont know the structure of these fields.

Campo4: Haz Entrante 10 bits
Campo24: Haz Saliente 10 bits

Because my output for these fields is diferent from my report generated for my switch or exchange. With these fields, I must to recognize the operator who originate the traffic.

I will request to the vendor support to solve it.


Thanks malpa
 
That makes two of us, I was confused from jump street.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top