0

好的,这是我有点迷惑的函数(小生锈的位运算符)

void two_one(unsigned char *in,int in_len,unsigned char *out)
{
unsigned char tmpc;
int i;

for(i=0;i<in_len;i+=2){
    tmpc=in[i];
    tmpc=toupper(tmpc);
    if((tmpc<'0'||tmpc>'9') && (tmpc<'A'||tmpc>'F'))tmpc='F';
    if(tmpc>'9')
        tmpc=toupper(tmpc)-'A'+0x0A;
    else
        tmpc-='0';
    tmpc<<=4; //Confused here
    out[i/2]=tmpc;

    tmpc=in[i+1];
    tmpc=toupper(tmpc);
    if((tmpc<'0'||tmpc>'9') && (tmpc<'A'||tmpc>'F'))tmpc='F';
    if(tmpc>'9')
         tmpc=toupper(tmpc)-'A'+0x0A;
    else
         tmpc-='0';

    out[i/2]|=tmpc; //Confused Here
}
}

我标记了两个我不太明白的地方。如果有人可以帮助我将这些部分转换为 Vb.Net,或者至少帮助我了解那里发生了什么,那就太棒了。

谢谢。

更新

所以这就是我想出的,但它并没有给我正确的数据......这里看起来有什么问题吗?

Public Function TwoOne(ByVal inp As String) As String
    Dim temp As New StringBuilder()
    Dim tempc As Char
    Dim tempi As Byte
    Dim i As Integer
    Dim len = inp.Length
    inp = inp + Chr(0)
    For i = 0 To len Step 2
        If (i = len) Then Exit For
        tempc = Char.ToUpper(inp(i))
        If ((tempc < "0"c Or tempc > "9"c) AndAlso (tempc < "A"c Or tempc > "F"c)) Then
            tempc = "F"c
        End If
        If (tempc > "9"c) Then
            tempc = Char.ToUpper(tempc)
            tempc = Chr(Asc(tempc) - Asc("A"c) + &HA)
        Else
            tempc = Chr(Asc(tempc) - Asc("0"c))
        End If
        tempc = Chr(CByte(Val(tempc)) << 4)
        Dim tempcA = tempc

        tempc = Char.ToUpper(inp(i + 1))
        If ((tempc < "0"c Or tempc > "9"c) AndAlso (tempc < "A"c Or tempc > "F"c)) Then
            tempc = "F"c
        End If
        If (tempc > "9"c) Then
            tempc = Char.ToUpper(tempc)
            tempc = Chr(Asc(tempc) - Asc("A"c) + &HA)
        Else
            tempc = Chr(Asc(tempc) - Asc("0"c))
        End If
        temp.Append(Chr(Asc(tempcA) Or Asc(tempc)))
    Next
    TwoOne = temp.ToString()
End Function
4

2 回答 2

1

tmpc <<= 4将位向左移动tmpc4 位,然后将值分配回 tmpc。因此,如果tmpc00001101,它变成11010000

out[i/2]|=tmpc用 对数组值进行按位或运算tmpc。因此如果out[i/2]is01001001tmpcis 10011010,则out[i/2]变为11011011

编辑(更新的问题):tmpc-='0';原始代码中的行与您的新代码不完全相同tempc = "0"c-=从变量中减去值,因此您需要tempc = tempc - "0"c或类似

于 2012-06-15T00:50:01.367 回答
0

tmpc<<=4;(或tmpc = tmpc << 4;tmpc左移 4 位。

out[i/2]|=tmpc;(或out[i/2] = out[i/2] | tmpc;out[i/2]tmpc.

于 2012-06-15T00:46:52.473 回答