How to generate the hash of public key in Bitcoin I'm trying to hash the public key with SHA256 and then ripemed160 and get the hash. but it gives the wrong hash

Public key:  2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737

Hash should be: 93ce48570b55c42c2af816aeaba06cfee1224fae

Imports System.Security.Cryptography
Imports System.Text
Imports System.Globalization
Imports System.Numerics

Private Function HexStringToByteArray(ByVal shex As String) As Byte()
    Dim B As Byte() = Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
    Return Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
End Function


Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click

Dim public_key as String = "2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737"
   Dim pubkey_hash As String

    Dim sha256 As SHA256 = SHA256Managed.Create()
    Dim bytes As Byte() = System.Text.Encoding.Unicode.GetBytes(public_key)
    Dim hash As Byte() = sha256.ComputeHash(bytes)
    Dim stringBuilder1 As New StringBuilder()

    For i As Integer = 0 To hash.Length - 1
        stringBuilder1.Append(hash(i).ToString("X2"))
    Next

    bytes = HexStringToByteArray(stringBuilder1.ToString())

    Dim ripemd160 As RIPEMD160 = RIPEMD160Managed.Create()
    Dim hash_160 As Byte() = ripemd160.ComputeHash(bytes)

    Dim stringBuilder2 As New StringBuilder()

    For i As Integer = 0 To hash_160.Length - 1
        stringBuilder2.Append(hash_160(i).ToString("X2"))
    Next

    Dim arr() As Char = stringBuilder2.ToString 

    pubkey_hash = arr.tostring
    TextBox14.Text = pubkey_hash


   End Sub


and is there any way I can shorten this code? loops &amp; if statements etc
I'm starting to wonder how difficult it is to find people who are helping others in this site


I don't have your same idea, but sorry for the delay in the answer but in Italy exist at the moment the alert "coronavirus".

My test to resolve your problem

I don't know the language of the code that you have public in the question but I have a project to decompiler script bitcoin but is in the start phases.

I'm answering with C++ code.

Exist two types of Public key, that is compressed and uncompressed, the difference is:


Compressed public key started with a flag "02" (I think this is your cases)
Uncompressed start with the flag "03"
If the you key start with "04" doesn't is the public key but is a P2PK script.


But a good reference that explains the compressed public key is this What is a compressed Bitcoin key? 

So, I think your error is the public key input,your public key is 02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737 and not 2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737, you have lose the the part of flag of the compressed public key.

So, my C++ code to test my idea is this:

    cout &lt;&lt; "Test for Bitcoin.stack question" &lt;&lt; endl;
    cout &lt;&lt; "Public Key: " &lt;&lt; "02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737\n";
    string keyTmp = "02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737";
    string flag = keyTmp.substr(0, 2);
    if(flag == "02"){
          cout &lt;&lt; "Compressed with y event";
    }else if(flag == "03"){
      cout &lt;&lt; "Compressed with y odd";
    }else{
      assert(flag == "04");
    }
    Bytes bytesTmp = hexBytes(keyTmp.c_str());

    //SHA256
    Sha256Hash shaHash = Sha256::getHash(bytesTmp.data(), bytesTmp.size());
    uint8_t result[Ripemd160::HASH_LEN];
    Ripemd160::getHash(shaHash.value, sizeof(shaHash), result);

    cout &lt;&lt; "Hash Public key: " &lt;&lt; ToString(result);


The hash public key ofter run this code is: 93ce48570b55c42c2af816aeaba06cfee1224fae 

References

The reference of the project are off-topics here but I think without it this post can be fake, so I'm adding the reference of the library used to implement my code in this answer.


Bitcoin-Cryptography-Library
decompiler-bitcoin-script: this is my start project to undestend how bitcoin script work, so you can found the idea if you want implement other feature inside your work. 
Github gits code