Hello everyone! I need help. I can’t convert my ja...
# getting-started
l
Hello everyone! I need help. I can’t convert my java utils class to kotlin. Let me show an example:
Java
Copy code
public class Ut {
    public static String sha256(String input) throws NoSuchAlgorithmException {
        MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
        byte[] result = mDigest.digest(input.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte aResult : result) {
            sb.append(Integer.toString((aResult & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    }
}
Kotlin auto converter
Copy code
@Throws(NoSuchAlgorithmException::class)
fun String.sha256(): String {
    val mDigest = MessageDigest.getInstance("SHA-256")
    val result = mDigest.digest(this.toByteArray())
    val sb = StringBuilder()
    for (aResult in result) {
        sb.append(Integer.toString((aResult and 0xff) + 0x100, 16).substring(1))
    }
    return sb.toString()
}
And I got error: http://prntscr.com/j1lw9b Autofix fixed it: http://prntscr.com/j1lwkr And test do not pass well 😞 http://prntscr.com/j1lx30 Please suggest me how to fix this line
sb.append(Integer.toString((aResult and 0xff) + 0x100, 16).substring(1))