简体   繁体   中英

Conversion between Base64String and Hexadecimal

我在我的 C++/CLI 项目中使用ToBase64String来给出一个像/MnwRx7kRZEQBxLZEkXndA==这样的字符串我想将此字符串转换为十六进制表示,我如何在 C++/CLI 或 C# 中做到这一点?

FromBase64String will take the string to byte s

byte[] bytes = Convert.FromBase64String(string s);

Then, BitConverter.ToString() will convert a byte array to a hex string ( byte[] to hex string )

string hex = BitConverter.ToString(bytes);
public string Base64ToHex(string strInput)
{
    try
    {
        var bytes = Convert.FromBase64String(strInput);
        var hex = BitConverter.ToString(bytes);
        return hex.Replace("-", "").ToLower();
    }
    catch (Exception)
    {
        return "-1";
    }
}

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

Convert the string to a byte array and then do a byte to hex conversion

string stringToConvert = "/MnwRx7kRZEQBxLZEkXndA==";

byte[] convertedByte = Encoding.Unicode.GetBytes(stringToConvert);

string hex = BitConverter.ToString(convertedByte);

Console.WriteLine(hex);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM