Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert.ToBase64String() with numbers larger than 255

Tags:

c#

base64

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);
}

1 Answers

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

like image 68
Aleks Andreev Avatar answered Feb 02 '26 03:02

Aleks Andreev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!