将c#的代码转换为tcl如何创建这个数组或者它是什么以及如何应用条件'

时间:2016-06-15 04:44:06

标签: tcl

将下面给出的代码转换为tcl: -

ReportLog("\nCheck 4th and 5th TLPs.");
UInt32[] tlp4 = new UInt32[4];
UInt32[] tlp5 = new UInt32[4];
captureMemory.GetTlp(3, tlp4);
captureMemory.GetTlp(4, tlp5);

for (int i = 0; i < 4; i++)
{
    if (tlp4[i] != tlp5[i])
    {
        ReportLog("\nRetry buffer not persistent through link recovery. Test failed.");
        ReportResult(ETestResultCode.RESULT_ERROR, "Retry buffer not persistent through link recovery. Test failed.");
        return 1;
    }

这里的值new UInt32[4]正在从其他一些函数中填充......  主要是我在如何编写下面给出的部分时感到困惑....

UInt32[] tlp4 = new UInt32[4];
UInt32[] tlp5 = new UInt32[4];
captureMemory.GetTlp(3, tlp4);
captureMemory.GetTlp(4, tlp5);

for (int i = 0; i < 4; i++)
{
    if (tlp4[i] != tlp5[i])
    {

1 个答案:

答案 0 :(得分:1)

Tcl以完全与C#不同的方式管理内存和数字。这可能会改变正确的方法。但是,我们可能希望界面是这样的:

set tlp4 [lrepeat 4 0]; # Have to specify the default value; C# just uses 0 anyway.
set tlp5 [lrepeat 4 0]

# These lines might vary quite a bit; the code in question isn't standard Tcl
# Note that we're passing the variable *names* in.
captureMemory getTlp 3 tlp4
captureMemory getTlp 4 tlp5

# Foreach can iterate over multiple lists at once; this is *NICE*
foreach element4 $tlp4 element5 $tlp5 {
    if {$element4 != $element5} {
        # Do the error handling...

更简单但更直接的方法是使用字节数组。

set tlp4 [binary format "iu4" {0 0 0 0}]
set tlp5 [binary format "iu4" {0 0 0 0}]

captureMemory getTlp 3 tlp4
captureMemory getTlp 4 tlp5

# Convert to lists
binary scan $tlp4 "iu4" uints4
binary scan $tlp5 "iu4" uints5

foreach element4 $uints4 element5 $uints5 {
    if {$element4 != $element5} {
        # Do the error handling...

请注意,仅从Tcl 8.6开始支持iu4格式字符串(对于4个无符号整数,每个大小为4个字节)。对于旧版本,您需要使用i4并处理值已签名的事实。

此外,如果你只是比较这些值,你可以直接在$tlp4$tlp5之间使用字符串相等。