Cannot implicitly convert type string to byte []

I have a class that encrypts a password using a salt hash.

But if I want to pass null to a class, I get the following error: Cannot implicitly convert type string to byte[]

Here is the class code:

 public class MyHash { public static string ComputeHash(string plainText, string hashAlgorithm, byte[] saltBytes) { Hash Code } } 

When I use the class, I get the error: "It is not possible to implicitly convert a string of type to byte []"

 //Encrypt Password byte[] NoHash = null; byte[] encds = MyHash.ComputeHash(Password, "SHA256", NoHash); 
+6
source share
2 answers

The return type of your ComputeHash function is a string. You are trying to assign the result of your encds function, which is the byte []. The compiler points out this discrepancy to you because there is no implicit conversion from string to byte [].

0
source

This is because the "ComputeHash" method returns a string, and you are trying to assign this return value to a byte array with:

 byte[] encds = MyHash.ComputeHash(Password, "SHA256", NoHash); 

There is no implicit conversion for a string to byte [], because there are many different encodings for representing a string as bytes, such as ASCII or UTF8.

You need to explicitly convert the bytes using the appropriate encoding class, for example:

 string x = "somestring"; byte[] y = System.Text.Encoding.UTF8.GetBytes(x); 
+14
source

Source: https://habr.com/ru/post/916872/


All Articles