I'm using code below to convert a list of numbers to a Base64 encoded string.
The problem is that as soon as I try something over 255 I get System.OverflowException since it overflows the byte capacity.
What would be a good way of doing this? There is an example here but I was just wondering if there are other ways of making this work.
private string DecimalToBase64(List<int> lst)
{
byte[] arr = new byte[lst.Count];
for(int i = 0; i < arr.Length; i++)
{
arr[i] = Convert.ToByte(lst[i]);
}
return Convert.ToBase64String(arr);
}
Try this code:
public static string DecimalToBase64(List<int> lst)
{
var bytes = new List<byte>();
foreach (var item in lst)
bytes.AddRange(BitConverter.GetBytes(item));
return Convert.ToBase64String(bytes.ToArray());
}
Read more about bytes order wiki
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With