VB.NET and sizeof

為{幸葍}努か 提交于 2019-12-01 04:04:35

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){}

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.

Joel Coehoorn

I'm hashing a password and was following stackoverflow.com/a/10380166/1113475

The top answer there is wrong, in spite of high vote count. The code for that answer still uses an encoding (Unicode), because that's how all strings are encoded internally in .NET. Even if it didn't, the encoding still matters, because you need to be able to decrypt the string on a different system than the one that encrypted it and obtain meaningful results. Even with the same system doing the encrypting/decrypting, something as simple as a Windows Update patch to the .NET Framework could break this. Pick an encoding (like Unicode or UTF-8), and just call its GetBytes() method:

Dim bytes = Encoding.Unicode.GetBytes(password)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!