I am try to convert a image file in flutter:
File _img=new File('/data/user/0/com.example.test3/app_flutter/2020-10-29T17:18:56.210347.png');
List<int> imageBytes = _img.readAsBytesSync();
String imageB64 = base64Encode(imageBytes);
print(imageB64);
But it look like is a wrong base64 String and I cannot decode to image on convert website:
https://codebeautify.org/base64-to-image-converter
iVBORw0KGgoAAAANSUhEUgAAAhwAAALECAYAAABDk+k+AAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAACAASURBVHic7d1ngFx1ucDhd9N77z0hbdMhCSAJVZoURS4C0psColcxFBXFKypKUywUQYp0EdtVQHpHTQLpZZOQnpDee9v7QeUSksxsyP7Ptuf5tnPeybwfCPvLmTNnIgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgNJRUFYvXFxcXFxWrw0AVVVBQUGZ/O6vVhYvCgBULYIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhMcAEByggMASE5wAADJCQ4AIDnBAQAkJzgAgOQEBwCQnOAAAJITHABAcoIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhMcAEByggMASE5wAADJCQ4AIDnBAQAkJzgAgOQEBwCQnOAAAJITHABAcoIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhMcAEByggMASE5wAADJCQ4AIDnBAQAkJzgAgOQEBwCQnOAAAJITHABAcoIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhMcAEByggMASE5wAADJCQ4AIDnBAQAkJzgAgOQEBwCQnOAAAJITHABAcoIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhM
Is the dart base64 format is different to another?
Thanks.
You have to convert your bytes into an Uint8List object not a List<int>:
File _img = File(
'/data/user/0/com.example.test3/app_flutter/2020-10-29T17:18:56.210347.png');
final bytes = Uint8List.fromList(_img.readAsBytesSync());
final imgBase64 = base64Encode(bytes);
print(imgBase64);
I find that this case by the 'print' function cannot display fully base64 code.
If want to verify it, need to export to text file:
_write(String text) async {
final File file = File('/storage/emulated/0/xxx/my_file.txt');
await file.writeAsString(text);
print(file);
}
Related
I have a string:
var myString:String = "My String"
How can I convert it to an InputStream in Kotlin?
Kotlin has an extension for String to convert directly.
val inputStream: InputStream = myString.byteInputStream()
The argument on byteInputStream is defaulted to charset: Charset = Charsets.UTF_8.
You can look at the extension by writing it and then cmd+click on it or in the package kotlin.io file IOStream.kt
Relying on the Java version is not wrong, but rather using a more kotlin idiomatic way when possible
val myString = "text"
val targetStream: InputStream = ByteArrayInputStream(initialString.toByteArray())
Pst.
If you copy some java code, for example:
String myString = "text";
InputStream targetStream = new ByteArrayInputStream(myString.getBytes());
Android Studio will popup "Clipboard content seems to be Java code. Do you want to convert it to Kotlin?
My case is to send a file to the backend but the only file content as byte array eg:
public saveFile(file: File, name: string, description: string): Observable<SymbolResponseEditModel> {
let formData: FormData = new FormData();
formData.append('FileName', name);
formData.append('FileDescription', description);
formData.append('Attachment', file); // <--- this must be a byte array instead of File
return this.http.post<SymbolResponseEditModel>(
'http://senddata.com',
formData
);
Result of this code is that I send to the backend a file containing additional information at the begining
a file containing additional information at the begining as you can see on the picture.
To do this I try to convert the file using file reader but I cannot convert a file to bytearray according to: Getting byte array through input type = file
var reader = new FileReader();
reader.onload = function() {
var arrayBuffer = this.result,
array = new Uint8Array(arrayBuffer),
binaryString = String.fromCharCode.apply(null, array);
console.log(binaryString);
}
reader.readAsArrayBuffer(this.files[0]);
And the error
TS2345: Argument of type 'File' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer | ArrayLike<number>'.
Type 'File' is missing the following properties from type 'SharedArrayBuffer': byteLength, length, [Symbol.species], [Symbol.toStringTag]
The second idea is to remove two lines from the file while downloading because the reasponse content type is octet-stream:
const reader = new FileReader();
reader.addEventListener('loadend', (e) => {
const text = e.srcElement.result; // <-- this text is encoded but I don't want it. Anyway the file is different than I
// sent and i am not able to read it.
let splittedString = text.split('\n');
splittedString = splittedString.splice(2);
let concatenedArray = splittedString.join('\n');
this.fetch(concatenedArray, symbolName);
});
reader.readAsArrayBuffer(response);
This whould work but the reader encode the file.
Have you got any other idea how to send a file as byte array without additional information? Is it possible?
I want to read a wav file sample rate and size from its header in Angular.
I am trying to use the Audio context to decode the data but I am unable to make it work.
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
audioCtx.decodeAudioData(uint8Array, function (buffer) {
//read data
})
I have an array of bytes but I get an error : parameter 1 is not of type ArrayBuffer.Then I convert it to ArrayBuffer but still get an error
Any ideas?
I tried to encode a byte array using glib2.0. After I add the encoded data to a json object, after I send the encoded data through pubnub client,
I got doble slashed string from json.
This is my code :
//code for encoding data
const guchar *data=inputEncodingData;
encodedData=g_base64_encode (data,datalength);
g_print("Encoded data==>%s\n",encodedData);
It produces encoded string with slashes. like this
KA4IChkAGQAAAAAAAAAAAOAO4A4uDgAAAAAAAAoAGAAmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQD//wAAwf/A/8D/AAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAEA//8CAP7/BAD6/wgA9P8SAOb/JwA2/A39mP0AAAIA4P/wD939pf2w/eL87vxz
/Nn7zasAAHMKAADIlAkKGAAYAAAAAAAAAAAA4Jbgls6UAAAAAAAACgAYACYAAAAAAAAAyf+9/7//
vv/B/8D/wP+8/77/v//C/8D/vf+//8D/wP++/77/
This is the code for creating json object:
struct json_object *obj1;
obj1 = json_object_new_object();
json_object_object_add(obj1, "result", json_object_new_string(encodedData));
This will print like this:
KA4IChkAGQAAAAAAAAAAAOAO4A4uDgAAAAAAAAoAGAAmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQD\/\/wAAwf\/A\/8D\/AAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAEA\/\/8CAP7\/BAD6\/wgA9P8SAOb\/JwA2\/A39mP0AAAIA4P\/wD939
pf2w\/eL87vxz\/Nn7zasAAHMKAADIlAkKGAAYAAAAAAAAAAAA4Jbgls6UAAAAAAAACgAYACYAAA
AAAAAAyf+9\/7\/\/vv\/B\/8D\/wP+8\/77\/v\/\/C\/8D\/vf+\/\/8D\/wP++\/77\/
Why is it like this? Any idea? I need data with one slash.
I can't decode the DUKPT swipe Data, I'm trying using differers examples but the credit card information is encoded yet.
I had a headache trying of decoding the swipe information:
This example can help you to do it:
To Download the Java Example here: https://github.com/ricardojava/mobile/tree/master/TEST_GATE2all/src/com/bbpos
To Modify the file: https://github.com/ricardojava/mobile/blob/master/TEST_GATE2all/src/com/bbpos/SimpleMain.java
String bdk = "0123456789ABCDEFFEDCBA9876543210";
String ksn = "00000232100117e00027";
String tk1 = "de8bfe769dca885cf3cc312135fe2cccfacf176235f4bdee773d1865334315ed2aefcab613f1884b5d63051703d5a0e2bd5d1988eeabe641bd5d1988eeabe641";
String key = DUKPTServer.GetDataKey(ksn, bdk);
String decryptedTLV = TripleDES.decrypt_CBC(tk1, key);
byte[] s = DatatypeConverter.parseHexBinary(decryptedTLV);
System.out.println(new String(s));
}
I hope it can help you!........