c# - ReadInt32 vs. ReadUInt32 -


i tinkering ip packet 'parsers' when noticed odd.

when came parsing ip addresses, in c#

private uint srcaddress;  // stuff srcaddress = (uint)(binaryreader.readint32()); 

does trick, you'd think vb.net equivallent

private srcaddress uinteger '' stuff srcaddress = cuint(binaryreader.readint32()) 

would trick too. doesn't. :

srcaddress = reader.readuint32() 

however will.

took time discover, have dicovered -- if ? why ?

vb.net, default, c# doesn't default. check numerical overflow. , will trigger in code, ip addresses last bit 1 instead of 0 produce negative number , cannot converted uinteger. data type can store positive 32-bit numbers.

c# has option too, you'd have explicitly use checked keyword in code. or use same option vb.net projects have turned on default: project + properties, build tab, advanced, tick "check arithmetic overflow/underflow" checkbox. same option in vb.net project named "remove integer overflow checks", off default.

do note how these defaults affected syntax of languages well. in c# have write cast convert value incompatible value type. not necessary in vb.net, runtime check keeps out of trouble. bad kind of trouble have, overflow can produce drastically bad results. not in case, happens, ip address unsigned number.

do keep other quirk ip-addresses in mind, sockets first invented on unix machines powered lsd , big-endian processors. must use ipaddress.networktohostorder() address in proper order. has overloads take signed integer type argument. using readint32() correct, assuming ipv4 address, pass directly networktohostorder(). no fear of overflow.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -