Kelly Elton
Kelly Elton

Reputation: 4427

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.

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

Upvotes: 0

Views: 113

Answers (2)

Gnat
Gnat

Reputation: 2921

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

Upvotes: 1

MRAB
MRAB

Reputation: 20654

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.

Upvotes: 0

Related Questions