Tcl: как объединить два двоичных значения?

231
Fisher

Хотите объединить два двоичных значения, чтобы получить 16-битное значение и сохранить в файл.
Первый двоичный файл имеет постоянную 6 битов 000111, второй двоичный файл начинается с 0 и увеличивается на 1 для каждого цикла.

#!/usr/bin/tclsh set output_file "output.dat" set data_number "10"  set output_fpt [open ./$ w]  for {$x < $data_number} { set y [expr $x * (2 ** 22)] binary scan [binary format I $y] B32 var set data [binary format B6B12 000111 $var]  fconfigure $output_fpt -translation binary puts -nonewline $output_fpt $data }  close $output_fpt 

enter image description here

Ожидаемый результат: 1C00 1C01 1C02 ...

0

2 ответа на вопрос

0
Fisher
#!/usr/bin/tclsh set output_file "output.txt" set data_number "10"  set output_fpt [open ./$ w]  for {$x < $data_number} { set q [expr ($x + (7 * (2 ** 10))) * (2 ** 16)] binary scan [binary format I $q] B16 var_q  set data_q [binary format B16 $var_q]  fconfigure $output_fpt -translation binary puts -nonewline $output_fpt $data_q }  close $output_fpt 

enter image description here

0
glenn jackman

С двоичными данными вы можете использовать побитовые арифметические операторы:

$ tclsh % set fixed 0b000111 0b000111 % for {$i < 4} { set n [expr {($fixed << 2 | $i) << 8}] puts [format {%d => %d = %b = %x} $i $n $n $n] } 0 => 7168 = 1110000000000 = 1c00 1 => 7424 = 1110100000000 = 1d00 2 => 7680 = 1111000000000 = 1e00 3 => 7936 = 1111100000000 = 1f00 
Обратите внимание, что формат% b нуждается в tclsh8.6. Fisher 5 лет назад 0

Похожие вопросы