英文:
Splitting a binary bit stream in N equal size using Tcl
问题
我有一个数字,比如10101100100011101010111010。我想把它分成N个相等大小的块,比如我想要的输出是:
1010 1100 1000 1110 1010 1110 10
我想用Tcl完成这个任务。有什么想法吗?
我之前使用了for循环,成功拆分了第一个块,也就是输出中的1010,但没有拆分其余的块。
英文:
I have a number say, 10101100100011101010111010. And I want to split it in N equal sized chunks, let's say I want an output as:
1010 1100 1000 1110 1010 1110 10
I want it to be done using Tcl . Any ideas?
I was using for loop and I was able to split the first chunk that is in Output I was able to get 1010 but not the rest chunks.
答案1
得分: 2
我不懂tcl,但查阅了一些手册后,我找到了以下代码:
#!/usr/bin/tclsh
proc str2chunksize { s cs } {
set len [ string length $s ]
for {set i 0; set j -1} {$i < $len} {incr i $cs} {
incr j $cs
lappend resultList [ string range $s $i $j ]
}
return $resultList
}
proc str2numchunks { s nc } {
set len [ string length $s ]
set cs [ expr {1 + ($len / $nc)} ]
set excess [ expr {$len % $nc} ]
for {set n 0; set i 0; set j -1} {$n < $nc} {incr n} {
if {$n == $excess} {incr cs -1}
incr j $cs
lappend resultList [ string range $s $i $j ]
incr i $cs
}
return $resultList
}
set chunks [ str2chunksize "10101100100011101010111010" 4 ]
puts [ join $chunks " " ]
set chunks [ str2chunksize "10101100100011101010111010" 7 ]
puts [ join $chunks " " ]
set chunks [ str2numchunks "10101100100011101010111010" 4 ]
puts [ join $chunks " " ]
set chunks [ str2numchunks "10101100100011101010111010" 7 ]
puts [ join $chunks " " ]
set chunks [ str2numchunks "10101100100011101010111010" 17 ]
puts [ join $chunks " " ]
set chunks [ str2numchunks "10101100100011101010111010" 30 ]
puts [ join $chunks ":" ]
输出结果:
1010 1100 1000 1110 1010 1110 10
1010110 0100011 1010101 11010
1010110 0100011 101010 111010
1010 1100 1000 1110 1010 111 010
10 10 11 00 10 00 11 10 10 1 0 1 1 1 0 1 0
1:0:1:0:1:1:0:0:1:0:0:0:1:1:1:0:1:0:1:0:1:1:1:0:1:0::::
英文:
I don't speak tcl but a few manpage lookups gave me:
#!/usr/bin/tclsh
proc str2chunksize { s cs } {
set len [ string length $s ]
for {set i 0; set j -1} {$i < $len} {incr i $cs} {
incr j $cs
lappend resultList [ string range $s $i $j ]
}
return $resultList
}
proc str2numchunks { s nc } {
set len [ string length $s ]
set cs [ expr {1 + ($len / $nc)} ]
set excess [ expr {$len % $nc} ]
for {set n 0; set i 0; set j -1} {$n < $nc} {incr n} {
if {$n == $excess} {incr cs -1}
incr j $cs
lappend resultList [ string range $s $i $j ]
incr i $cs
}
return $resultList
}
set chunks [ str2chunksize "10101100100011101010111010" 4 ]
puts [ join $chunks " " ]
set chunks [ str2chunksize "10101100100011101010111010" 7 ]
puts [ join $chunks " " ]
set chunks [ str2numchunks "10101100100011101010111010" 4 ]
puts [ join $chunks " " ]
set chunks [ str2numchunks "10101100100011101010111010" 7 ]
puts [ join $chunks " " ]
set chunks [ str2numchunks "10101100100011101010111010" 17 ]
puts [ join $chunks " " ]
set chunks [ str2numchunks "10101100100011101010111010" 30 ]
puts [ join $chunks ":" ]
output:
1010 1100 1000 1110 1010 1110 10
1010110 0100011 1010101 11010
1010110 0100011 101010 111010
1010 1100 1000 1110 1010 111 010
10 10 11 00 10 00 11 10 10 1 0 1 1 1 0 1 0
1:0:1:0:1:1:0:0:1:0:0:0:1:1:1:0:1:0:1:0:1:1:1:0:1:0::::
答案2
得分: 0
使用 regexp
呢?
regexp -all -inline -- {.{1,4}} 10101100100011101010111010
你需要为不同的量词边界 {m, n}
提供,其中 m = 1
,n = N
。
英文:
What about using regexp
?
regexp -all -inline -- {.{1,4}} 10101100100011101010111010
You would have to provide for different quantifier bounds {m, n}
, with m = 1
, and n = N
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论