将kafka消息中的hex字符串转为Google.Protobuf.ByteString
上代码
//hex 字符串转为base64字符串 public string HexString2B64String(string input) { return System.Convert.ToBase64String(HexStringToHex(input)); } // hex字符串转为hex数组 public byte[] HexStringToHex(string inputHex) { var resultantArray = new byte[inputHex.Length / 2]; for (var i = 0; i < resultantArray.Length; i++) { resultantArray[i] = System.Convert.ToByte(inputHex.Substring(i * 2, 2), 16); } return resultantArray; }
如何使用
//将kafka 中的hex字符串去除空格和换行符
var base64String = HexString2B64String("1A066661696C6564"); var instace = Activator.CreateInstance<Test>(); instace.MergeFrom(ByteString.FromBase64(base64String));