I am trying to convert a Uint8List to a string using Dart (in a Flutter project).
I am using the Flutter Android USB Serial plugin ( https://github.com/altera2015/usbserial)
The data are coming from a usb device and are returned from the library as a Stream.
If outputed as a string, it looks like:
[255,0,0,0,255....]
When I try:
String newTag = ascii.decode(asyncSnapshot.data);
I get the error :
FormatException:Invalid value in input: 255
I don't know how to solve this issue, my result should be :
"352206000079439"
Try this one;
List<int> list = 'someData'.codeUnits;
Uint8List bytes = Uint8List.fromList(list);
String string = String.fromCharCodes(bytes);
If data has compressed as a blob type.
Uint8List bytes = Uint8List.fromList(tcp_socket_blob_data);
var inflated = zlib.decode(bytes);
var data = utf8.decode(inflated);
More: flutter/dart: How to decompress/inflate zlib binary string in flutter
Related
`byte[] bytes = System.Text.Encoding.GetEncoding("ISO-8859-9").GetBytes(str);
byte[] hashingbytes = sha.ComputeHash(bytes);
String hash = Convert.ToBase64String(hashingbytes);`
this is a c# code When I try to hash similarly in flutter, the data is not the same. How can I do this in Flutter
Basically I'm trying to convert a base64 jpeg image to normal image in flutter using
Image.memory(base64Decode(stringBase64))
the image initially used to be jp/2 format which isn't supported by flutter so i converted the jp/2 base64 string to bitmap in java and then to base64 string jpeg to be able to decode it in flutter using this code :
public static String encodeToBase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
how ever when i try to decode this base64 string in flutter i'm getting this error
Invalid character (at character 77)
/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAIQAABtbnRyUkdC
which is pointing to the last C in the given line.
i don't seem understand where does the issue come from since i can convert my base64 string to image online but in flutter it throws that exception every time
thank you very much #Jamesdlin for the solution that was given in the comments
The issue was due to whitespace in the base64 string , solved by using
base64.decode(photoBase64.replaceAll(RegExp(r'\s'), '')),
If your URI contains data after the comma as it is defined by RFC-2397. Dart's Uri class is based on RFC-3986, so you can't use it.
Split the string by a comma and take the last part of it:
String uri = 'data:image/gif;base64,...';
Uint8List _bytes = base64.decode(uri.split(',').last);
REFERENCE: https://stackoverflow.com/a/59015116/12382178
I have a compress function like below, which has a return value List<int>
List<int> compressData() {
var stringBytes = utf8.encode(plainText);
return BZip2Encoder().encode(stringBytes);
}
then I want to encrypt the data with salsa20 from the package encrypt, but must be string data as input data, I've tried with toString() but it makes the data 2X bigger, how to solve the problem?
encrypt.key not accept digest value and it's accepting the list format value
final bytes = utf8.encode(stringKey + plainEmail);
Digest sha256Key = sha256.convert(bytes);
final key = encrypt.Key(sha256Key.bytes);
Please check below image
enter image description here
Digest.bytes returns a List<int> which, under the hood, is a Uint8List. So you need to cast it with:
final key = encrypt.Key(sha256Key.bytes as Uint8List);
since the constructor of Key expects a Uint8List.
Any time that you have a List<int> that isn't actually a Uint8List under the hood (so cannot be cast) you can convert it with:
final uInt8List = Uint8List.fromList(anyOldIntList);
i am using following code to encode PDF file to base 64
File f = new File(_path);
// String base64pdf = base64Encode(f.readAsBytesSync());
String base64pdf = base64.encode(f.readAsBytesSync());
String fileName = _fileName;
print('base64pdf: '+base64pdf);
I tried to encode the pdf file on here: encoding website
the result from the website shows that the base64 encoder inside flutter(dart) is trimming a big section of the String Generated and give only little first part of it ?
how to solve that , is this related with byte type?
RaisedButton(
onPressed: () async {
List<int> imageBytes = await sampleImage.readAsBytes();
base64Image = base64Encode(imageBytes);
print(base64Image);},),
SizedBox(height: 30,),
Image.memory(base64Decode(base64Image)),
Base 64 code in my git hub repo