So, we have this Public Key Hash:
7773D807892CB200E6A4785428294452C9E3B4B9
And we want to make it a Witness Public Key Hash. Bech32 encoding converts this to a 5-bit unsigned int array:
0E1D191D10011C0905121900011915040F010A02100A0A040A0B041E070D0519
To do this, I wrote some code, just for exercise. It's in C#:
public string squashBits(string scriptHash) {
string bits = hex2binary(scriptHash);
int n = 5;
string splitString = string.Join(string.Empty, bits.Select((x, i) => i > 0 && i % n == 0 ? string.Format(" {0}", x) : x.ToString()));
string[] arr = splitString.Split(' ');
string squashed = "";
for (int i = 0; i < arr.Length; i++) {
if (HexConverted(arr[i]).Length == 1) {
squashed += "0" + HexConverted(arr[i]);
} else {
squashed += HexConverted(arr[i]);
}
}
return squashed;
}
// copied from stackoverflow:
public static string hex2binary(string hexvalue) {
return String.Join(String.Empty, hexvalue.Select(c => Convert.ToString(Convert.ToUInt32(c.ToString(), 16), 2).PadLeft(4, '0')));
}
string HexConverted(string strBinary) {
string strHex = Convert.ToInt32(strBinary, 2).ToString("X");
return strHex;
}
To explain the above, your 160 bits script hash is an 4-bit array of unsigned integers.
0111 0111 0111 0011 1101 1000 0000 0111 1000 1001 0010 1100 1011 0010 0000 0000 1110 0110 1010 0100 0111 1000 0101 0100 0010 1000 0010 1001 0100 0100 0101 0010 1100 1001 1110 0011 1011 0100 1011 1001
7 7 7 3 D 8 0 7 8 9 2 C B 2 0 0 E 6 A 4 7 8 5 4 2 8 2 9 4 4 5 2 C 9 E 3 B 4 B 9
To convert it to an array of 5-bit integers, you need to "squash" the bits, like that:
01110 11101 11001 11101 10000 00001 11100 01001 00101 10010 11001 00000 00001 11001 10101 00100 01111 00001 01010 00010 10000 01010 01010 00100 01010 01011 00100 11110 00111 01101 00101 11001
0E 1D 19 1D 10 01 1C 09 05 12 19 00 01 19 15 04 0F 01 0A 02 10 0A 0A 04 0A 0B 04 1E 07 0D 05 19
Resulting in "
0E1D191D10011C0905121900011915040F010A02100A0A040A0B041E070D0519".
To continue further, you need to add the witness byte in front (0x00):
000E1D191D10011C0905121900011915040F010A02100A0A040A0B041E070D0519
And compute the checksum of the above, which is specified here:
https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki. Besides Electrum, it's also written in Python by Pieter Wuille:
https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py. Later, it's a matter of bech32 encoding.