Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET and sizeof

I'm converting some code from C# to VB.NET. I have the following line in C#

var bytes = new byte[password.Length * sizeof(char)];

Looking on MSDN it appears that VB.NET does not seem to have the sizeof operator. I understand there is a Marshal.SizeOf but further MSDN documentation states that the value returned can be different to that of sizeof.

Can anybody help? Is there an equivalent statement in VB.NET?

Additional Information

My aim is to convert a password into an array of bytes which I can then hash and then either store in a database or compare to a previously stored hash. But I don't necessarily want an answer relating to my particular situation.

Dim bytes(password.Length * xxx) As Byte
System.Buffer.BlockCopy(password.ToCharArray(), 0, bytes, 0, bytes.Length)
Dim sha512 = System.Security.Cryptography.SHA512.Create()
Dim hash = sha512.ComputeHash(bytes)

' compare hash or stroe in database
like image 475
Kevin Brydon Avatar asked Oct 16 '14 14:10

Kevin Brydon


People also ask

Is there a sizeof operator in VB NET?

Looking on MSDN it appears that VB.NET does not seem to have the sizeof operator. I understand there is a Marshal.SizeOf but further MSDN documentation states that the value returned can be different to that of sizeof. Can anybody help? Is there an equivalent statement in VB.NET?

What is the difference between sizeof (object) and sizeof<T>?

[Supported in the .NET Framework 4.5.1 and later versions] Returns the size of an unmanaged type in bytes. SizeOf<T>(T) [Supported in the .NET Framework 4.5.1 and later versions] Returns the unmanaged size of an object of a specified type in bytes. SizeOf(Object) Caution SizeOf(Object) may be unavailable in future releases.

What is sizeof<T> in C++?

SizeOf<T>() [Supported in the .NET Framework 4.5.1 and later versions] Returns the size of an unmanaged type in bytes. public: generic <typename T> static int SizeOf(); public static int SizeOf<T> (); static member SizeOf : unit -> int Public Shared Function SizeOf(Of T) () As Integer Type Parameters T The type whose size is to be returned.

What does [sizeof (object) ] return?

[Supported in the .NET Framework 4.5.1 and later versions] Returns the unmanaged size of an object of a specified type in bytes. SizeOf(Object)


Video Answer


2 Answers

The 'Len' operator in VB will do this (but it works on instances, so you need to adjust accordingly):

Dim bytes = New Byte((someString.Length * Len(New Char)) - 1){}
like image 115
Dave Doknjas Avatar answered Oct 08 '22 23:10

Dave Doknjas


VB.NET's Char maps to .NET's System.Char, which is defined in ECMA 335 to be a 16-bit Unicode character. Meaning, Char has a fixed size (no matter on which platform you compile or run your code), you don't actually need sizeof.

Therefore, just multiply by 2.

like image 2
stakx - no longer contributing Avatar answered Oct 08 '22 23:10

stakx - no longer contributing