0

How can I convert a git hash to a byte array with length 20 in C#? At the moment I represent the git hash as a string:

string gitHash = "0x29932f3915935d773dc8d52c292cadd81c81071d";

I have tried this:

byte[] gitHashBytes = System.Text.Encoding.ASCII.GetBytes(gitHash);
Array.Resize(ref gitHashBytes, 20);

But I actually want to convert the hex number to the byte array and not the string representation of it.

I need this to save the git hash in a smart contract where I am using bytes20 as data structure.

Why does the way I tried it above not work and just cut off the the hash after 20 digits? And what's the difference to the conversion in the proposed answer here: How can I convert a hex string to a byte array?

Cryt1c
  • 97
  • 1
  • 1
  • 11
  • 1
    Does your string really start with `0x`? That's not the normal representation of git hashes, and it slightly complicates matters here. –  Jun 22 '18 at 07:41
  • No, you are right. I can leave the 0x out. This was in there because I have copied it from a web3.js project. – Cryt1c Jun 22 '18 at 07:48

0 Answers0