TCL 中的零填充 (zero padding in TCL)


問題描述

TCL 中的零填充 (zero padding in TCL)

我的 tcl 代碼如下所示:

set writes 1a8000028020900

binary scan [binary format H* $writes] B* bits
puts "$bits"

輸出:

0 0 0 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

如果輸入的十六進制值是偶數長度,則沒有填充。如果它是奇數,就像上面的例子一樣,零被填充。

如果格式化的位數沒有在字節邊界處結束,最後一個字節的剩餘位將為零。如何避免這種填充


參考解法

方法 1:

If the number of digits formatted does not end at a byte boundary, the remaining bits of the last byte will be zeros. How to avoid this padding?

Well, you will have to compute and tell binary scan the targeted number of bits. Maybe there is a more elegant way than this, but why not simply DIY?

% set writes 1a8000028020900
1a8000028020900
% set hexLength [string length $writes]
15
% binary scan [binary format H* $writes] B[expr {$hexLength * 4}] bits
1
% puts $bits
000110101000000000000000000000101000000000100000100100000000

方法 2:

The manual page says:

If arg has fewer than count digits, then zeros will be used for the remaining digits.

The value you are supplying does not specify all 64 bits. The binary command cannot guess what you want, and pads zeros on to the end of the supplied string.

Edit:

To avoid this, simply define all bits in the value to be converted:

set writes 01a8000028020900

方法 3:

Here's a thought about padding the source string:

binary scan [binary format H* $writes] B* bits
puts "$bits"

binary scan [binary format H* [format "%016s" $writes]] B* bits
puts "$bits"

outputs

0001101010000000000000000000001010000000001000001001000000000000
0000000110101000000000000000000000101000000000100000100100000000

(by Manoj KumarmrcalvinBrad Lanamglenn jackman)

參考文件

  1. zero padding in TCL (CC BY‑SA 2.5/3.0/4.0)

#padding #formatting #zero #TCL






相關問題

'android:paddingRight' 與 'android:drawableRight' 一起使用 (’android:paddingRight’ working with ‘android:drawableRight’)

雙行導航欄 - 是什麼原因造成的? (double row navigation bar- what's causing it?)

在 Twitter 引導程序中為行添加背景顏色和填充 (Adding background color and padding to row in Twitter bootstrap)

如何控制 addView 到佈局 (How to control addView to a layout)

Ці можа выкарыстанне працэнтаў для вышыні запаўнення/поля/межы быць лепшым, чым em? (Can using percentages for height of padding/margin/border be better than em?)

填充對絕對定位的下拉菜單有意想不到的影響 (Padding has unexpected effect on drop down menu with absolute postioning)

內部 div 相對於包裝器具有不需要的空間 (Inner div has unwanted space in relation to wrapper)

css @font-face 不會在所有瀏覽器中垂直對齊文本 (css @font-face doesn't align text vertically across all browsers)

填充佔用了額外空間,我希望我的 div 像引導 div 一樣 (padding is taking up extraspaces,i want my div's to act like bootstrap div's)

Android -- 使用帶有自定義背景的 ListView 時如何刪除那一點額外的填充? (Android -- How to remove that little extra padding when using a ListView with custom background?)

使用顯示時沒有底部填充:網格和滾動 (No bottom padding when using display: grid and scroll)

TCL 中的零填充 (zero padding in TCL)







留言討論