C# to VB.NET Conversion

  • Thread starter Thread starter Matthew Hood
  • Start date Start date
M

Matthew Hood

I'm playing around with converting some C# code to VB.NET as a learning
exercise and I'm running into a little confussion for my lack of
understanding some C# syntax.

The C# code is:
public const int LOCSIG = 'P' | ('K' << 8) | (3 <<16) | (4 << 24)

Can anybody explain to me what this is doing and possibly give me a VB.NET
equivalent?
I'm confused about the characters 'P' & 'K' as part of an integer constant
and what the pipes are along with what << means.

TIA,
-Matt
 
In college, we used to challenge eachother. We'd give eachother blocks of
really really obscure C code and see if the other person could figure out
what it does without running it. The author of this bit of code must have
attended my school :-)

The pipes are bitwise OR. The << operator is a logical left shift. In C,
placing a character between single quotes is the same as getting the numeric
value of the character. see http://www.asciitable.com/

So, this expression is simply an integer value created using a series of
rather obtuse binary expressions.
public const int LOCSIG = 'P' | ('K' << 8) | (3 <<16) | (4 << 24)
which is equal to 0000 0100 0000 0011 0100 1001 0101 0000
or hex 0403 4B50

HTH,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Public Const LOCSIG As Int32=AscW("P"c)Or(AscW("K"c)<<8)Or(3<<16)Or(4<<24)

Characters need to be converted to their ASCII value before they can be used
in this kind of equation.

| = Or

<< = Binary Shift to the Left

Example table to help explain Binary Shift.

Binary (Base 2) | Decimal Equivelant | Alternative Equation
----------------------------------------------------------------
001010 << 1 = 010100 10 << 1 = 20 10 * (2 * 1) = 20
001011 << 1 = 010110 11 << 1 = 22 11 * (2 * 1) = 22
001010 << 2 = 101000 10 << 2 = 40 10 * (2 * 2) = 40
001011 << 2 = 101100 11 << 2 = 44 11 * (2 * 2) = 44

1010 >> 1 = 0101 10 >> 1 = 5 10 \ (2 * 1) = 5
1011 >> 1 = 0101 11 >> 1 = 5 11 \ (2 * 1) = 5
1010 >> 2 = 0010 10 >> 2 = 2 10 \ (2 * 2) = 2
1011 >> 2 = 0010 11 >> 2 = 2 11 \ (2 * 2) = 2
 
Matthew Hood said:
public const int LOCSIG = 'P' | ('K' << 8) |
(3 <<16) | (4 << 24)
Can anybody explain to me what this is doing

- The characters will be treated as ASCII values (so 'P', for
instance, is 80).
- The pipes represent a bitwise OR operation: the binary
representation of the result has a 1 in each digit where either of the
two expressions around | would have a 1.
- The << operator is a bitwise left shift operation: the binary
representation of the result is "rotated" to the left by the number of
positions indicated by the right-hand operand.

Whoever wrote that line of code and didn't comment it should be shot.

P.
 
Whoever wrote that line of code and didn't comment it should be shot.

Ahem.

The code (tries to?[1]) compose a 32-bit pattern from the
(supposedly?[1]) 8-bit patterns for 4,3,'K','P' in that order (MSB),
it's the standard way to do it.

what would you like the comment to say? please not:

int four = 2 + 2 // set four equal to the sum of 2 plus 2

Comments that points out comment common idioms or language features:

- doesn't really add any information not already available in code
- needs maintenance when refactoring
- water out the value of comments, so you don't spot the ones that
matter, like:
// must keep list sorted to maintain far-away-class Foo invariant

The code-reader will need to understand the language and idioms to
understand the code anyway, and you don't want a page of explanation on
every expression or statement.

Besides, there is an easy way to find out what the code does, check the
language reference or a book on C#, or even just a newsgroup :)

The fact that I have to reference [1] shows that I lack understanding of
the language/complier to adequately evaluate what's happening. No
*simple* comment could be given here to explain why the coder chose this
implementation.

What would be nice would probably be a reference to WHERE it is defined
that LOCSIG should be this value, for example "see ProtocolFoo rev1.4,
p523, table 15". And that site should probably explain why the coder has
chosen to use the compiler-charater-set for two of the values, and int's
for two other.

BTW: LOGSIC should *probably* be an unsigned int, since it seems to
really be a bit-pattern. and you could get a nasty surpise when doing
LOGSIC >> 24 on an int (not a problem here, since (LOGSIC & 80000000) ==
0) (Another thing I wouldn't like to write in a comment :)

[1]. I'm not entirely up to scratch on C# compilers, character-sets and
the "'" operator. Isn't the C# compiler using some default char-set of
the environment (like C-compilers)? and 'P' might have some
non-ascii-'P'-encoding in, for example, a EBCDIC environment?
 
[1]. I'm not entirely up to scratch on C# compilers, character-sets and
the "'" operator. Isn't the C# compiler using some default char-set of
the environment (like C-compilers)? and 'P' might have some
non-ascii-'P'-encoding in, for example, a EBCDIC environment?

No - C# *always* uses Unicode.
 
Helge said:
The code (tries to?[1]) compose a 32-bit
pattern from the (supposedly?[1]) 8-bit
patterns for 4,3,'K','P' in that order
(MSB), it's the standard way to do it.

The comment doesn't need to explain what the operators mean, but why
'43KP' is significant. The OP didn't make it clear what "LOCSIG" is
meant to signify, and why it should have that value, so I assumed it
wasn't clear from the code either.

P.
 
[1]. I'm not entirely up to scratch on C# compilers, character-sets and
the "'" operator. Isn't the C# compiler using some default char-set of
the environment (like C-compilers)? and 'P' might have some
non-ascii-'P'-encoding in, for example, a EBCDIC environment?

No - C# *always* uses Unicode.
And when it is converted to VB.Net does that as well *always* uses Unicode
in a String and Char

:-)

Cor
 
It appears as if you are examining code that decompresses .zip files as
the bytes you show represent the header of a zip file.

You might download the code for the ISharpZip library and see how they
handle that code it may also help your translation to VB code.
 
Back
Top