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
}
}

よくわからない2箇所に印を付けました。誰かがそれらの部分を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 shifts the bits in tmpc 4 places to the left, then assigns the value back to tmpc. Hence if tmpc was 00001101, it becomes 11010000

out[i/2]|=tmpc bitwise-ors the array value with tmpc. Hence if out[i/2] is 01001001 and tmpc is 10011010, then out[i/2] becomes 11011011

EDIT (updated question): The lines tmpc-='0'; in the original are not exactly the same as your new code tempc = "0"c. -= subtracts the value from the variable, and hence you need tempc = tempc - "0"c or similar

于 2012-06-15T00:50:01.367 に答える
0

tmpc<<=4; (or tmpc = tmpc << 4;) shifts tmpc left by 4 bits.

out[i/2]|=tmpc; (or out[i/2] = out[i/2] | tmpc;) bitwise-ORs out[i/2] with tmpc.

于 2012-06-15T00:46:52.473 に答える