Convert a hex string to base64

后端 未结 2 877
北恋
北恋 2021-01-29 12:04
byte[] ba = Encoding.Default.GetBytes(input);
var hexString = BitConverter.ToString(ba);
hexString = hexString.Replace(\"-\", \"\");
Console.WriteLine(\"Or: \" + hexStri         


        
2条回答
  •  失恋的感觉
    2021-01-29 12:42

    public string HexToBase64(string strInput)
    {
        try
        {
            var bytes = new byte[strInput.Length / 2];
            for (var i = 0; i < bytes.Length; i++)
            {
                bytes[i] = Convert.ToByte(strInput.Substring(i * 2, 2), 16);
            }
            return Convert.ToBase64String(bytes);
        }
        catch (Exception)
        {
            return "-1";
        }
    }
    

    On the contrary: https://stackoverflow.com/a/61224900/3988122

提交回复
热议问题