0

Want to concatenate two binary values to get a 16 bits value and save to a file.
First binary is 6 bits constant 000111, second binary starts from 0 and increment by 1 for each loop.

#!/usr/bin/tclsh
set output_file "output.dat"
set data_number "10"   
set output_fpt [open ./${output_file} w]

for {set x 0} {$x < $data_number} {incr x} {
   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

Expected output: 1C00 1C01 1C02 ...

Fisher
  • 757

2 Answers2

0
#!/usr/bin/tclsh
set output_file "output.txt"
set data_number "10"

set output_fpt [open ./${output_file} w]

for {set x 0} {$x < $data_number} {incr x} {
   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

Fisher
  • 757
0

With binary data, you might want to use bitwise arithmetic operators:

$ tclsh
% set fixed 0b000111
0b000111
% for {set i 0} {$i < 4} {incr i} {
    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
glenn jackman
  • 27,524