简体   繁体   English

C ++到vb.net,情侣转换问题

[英]c++ to vb.net, Couple conversion questsions

Ok, here's the function I'm a bit mystified by(Little rusty bitwise operators) 好的,这是我有点困惑的功能(小生锈的按位运算符)

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

I marked the two places that I don't quite understand. 我标记了我不太了解的两个地方。 If anyone could help me convert those pieces to Vb.Net, or at least help me understand what's going on there, that'd be awesome. 如果有人可以帮助我将这些片段转换为Vb.Net,或者至少帮助我了解那里发生的事情,那将是很棒的。

Thanks. 谢谢。

Update 更新资料

So this is what I came up with, but it's not quite giving me back the right data...Anything look wrong here? 所以这是我想出的,但是并不能完全给我正确的数据……这里看起来有什么问题吗?

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

tmpc <<= 4 shifts the bits in tmpc 4 places to the left, then assigns the value back to tmpc. tmpc <<= 4tmpc的位向左移动tmpc <<= 4位,然后将值分配回tmpc。 Hence if tmpc was 00001101 , it becomes 11010000 因此,如果tmpc00001101 ,就成了11010000

out[i/2]|=tmpc bitwise-ors the array value with tmpc . out[i/2]|=tmpc按位或与tmpc数组值。 Hence if out[i/2] is 01001001 and tmpc is 10011010 , then out[i/2] becomes 11011011 因此,如果out[i/2]01001001tmpc10011010 ,那么out[i/2]变成11011011

EDIT (updated question): The lines tmpc-='0'; 编辑(更新的问题): tmpc-='0'; in the original are not exactly the same as your new code tempc = "0"c . 原始代码中的代码与新代码tempc = "0"c并不完全相同。 -= subtracts the value from the variable, and hence you need tempc = tempc - "0"c or similar -=从变量中减去值,因此需要tempc = tempc - "0"c或类似的值

tmpc<<=4; (or tmpc = tmpc << 4; ) shifts tmpc left by 4 bits. (或tmpc = tmpc << 4; )将tmpc左移4位。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM