473,395 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# to VB.NET Conversion

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
Nov 16 '05 #1
11 1309
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.
--
"Matthew Hood" <Dr***********@Yahoo.com> wrote in message
news:OZ**************@TK2MSFTNGP12.phx.gbl... 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

Nov 16 '05 #2
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
= Binary Shift to the Right

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

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Matthew Hood" <Dr***********@Yahoo.com> wrote in message
news:OZ**************@TK2MSFTNGP12.phx.gbl... 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

Nov 16 '05 #3
"Matthew Hood" <Dr***********@Yahoo.com> wrote:
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.
Nov 16 '05 #4
That definately helps! Thanks a lot!

"Matthew Hood" <Dr***********@Yahoo.com> wrote in message
news:OZ**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #5
Paul E Collins wrote:
public const int LOCSIG = 'P' | ('K' << 8) |
(3 <<16) | (4 << 24)
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?

--
Helge
Nov 16 '05 #6
Helge Jensen <he**********@slog.dk> wrote:

<snip>
[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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Matthew,

You never know if you still see this, will be handy

Code C# and VB
http://www.harding.edu/USER/fmccown/...omparison.html

Language compare
http://msdn.microsoft.com/library/en...quivalents.asp

I hope this helps?

Cor
Nov 16 '05 #8
Helge Jensen wrote:
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.
Nov 16 '05 #9
>
[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
Nov 16 '05 #10
Those sites will do quite nicely.
Thanks Cor.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...
Matthew,

You never know if you still see this, will be handy

Code C# and VB
http://www.harding.edu/USER/fmccown/...omparison.html

Language compare
http://msdn.microsoft.com/library/en...quivalents.asp

I hope this helps?

Cor

Nov 16 '05 #11
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.

Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
3
by: Steve Richter | last post by:
here is a warning I am getting in a C++ .NET compile: c:\SrNet\jury\JuryTest.cpp(55) : warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while...
0
by: Lou Evart | last post by:
DOCUMENT CONVERSION SERVICES Softline International (SII) operates one of the industry's largest document and data conversion service bureaus. In the past year, SII converted over a million...
0
by: dataentryoffshore | last post by:
Get a Discount up to 60% on data entry, data capture, dataentry services, large volume data processing and data conversion services through offshore facilities in India. Offshore data entry also...
21
by: REH | last post by:
It it permissible to use the constructor style cast with primitives such as "unsigned long"? One of my compilers accepts this syntax, the other does not. The failing one chokes on the fact that the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.