-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should Base64 decoder have a return type of Uint8List? #31784
Comments
Also |
I think it would be reasonable to change the return type to Fair warning, though - the buffer of the returned var bytes = BASE64.decode(s);
var ints = bytes.buffer.asInt32List(0, bytes.length ~/ 4); so you have the correct number if 32-bit integers. |
@lrhn Could other similar cases be changed as well?
|
It's probably possible to make |
Yet another case: |
The new top level function |
It should, good catch!. |
why not returning a string? that's what most languages do
doesn't it make sense? |
A base-64 text represents a sequence of bytes. That's why decoding it returns a list of bytes. You can convert a sequence of bytes to a string again, but there is no single unique way to do that, and any one way we pick might turn out to be wrong. You could choose to always do UTF-8 decoding of the bytes into a string, but then it would fail if the bytes are not valid UTF-8 (not all byte sequences are). Or you could do LATIN-1 decoding, then all byte sequences are valid, but you might not get what you want (if you wanted UTF-8). So, it's better to create the bytes and let the user tell us the correct interpretation of those bytes, than it is to guess and sometimes guess wrong. |
Thank you very much for this excellent answer. it does make sense, and right on point with the problem I have. my string is neither UTF8 or Latin1, but BINARY ! thank you |
You have to first explain what a binary string is. Dart strings are sequences of unsigned 16-bit integers. Your bytes are unsigned 8-bit integers, so you have to say which 16-bit value/character each byte should correspond to. You can embed the 8-bit integers in a string, using the byte value as the low eight bits of the 16-bit code unit. That is actually what the LATIN-1 decoder does because LATIN-1 exactly is the characters of the first 256 Unicode codepoints. So: If you started with a Python |
Thank you, this is very helpful |
It now returns |
In Dart version 2.0.0-dev.15.0 the following does not work:
Fixing this would only require changing the return type of
List<int> convert
inBase64Decoder
class to
Uint8List convert
. The_Base64.decode
method already has typeUint8List
.Is there some reason is missing for not doing this?
The text was updated successfully, but these errors were encountered: