Convert a hexadecimal string into list of integers - flutter

Similar to this question here but for flutter/dart: Easier / better way to convert hexadecimal string to list of numbers in Python?
I have a string with hexadecimal value like 09F911029D74E35BD84156C5635688C0 and I want to return [9, 249, 17, 2, 157, 116, 227, 91, 216, 65, 86, 197, 99, 86, 136, 192]. In python I can just use list(binascii.unhexlify('09F911029D74E35BD84156C5635688C0')). Is there such a function for flutter/dart?

Try the Package convert to decode or encode your hex values into desired values
var hex = 09F911029D74E35BD84156C5635688C0;
var result = hex.decode(hex);
also import 'package:convert/convert.dart';
let me know if this works .

Related

Convert from decimal to ASCII the data present in a column of a .csv file

i should convert the data of a column of a csv file in powershell
This data is often in decimal and I would need to convert it to ASCII as in the example
80, 77, 79, 32, 83, 50, 52, 50, 45, 86, 70, 67 -> to Ascii > PMO S242-VFC
the table is composed as follows
Monitor
Modello
wkst1
80, 77, 79, 32, 83, 50, 52, 50, 45, 86, 70, 6
wkst2
V246HL
wkst3
V256IL
wkst4
65, 99, 101, 114, 32, 86, 50, 52, 54, 72, 76
this is the result
Monitor
Modello
wkst1
PMO S242-VFC
wkst2
V246HL
wkst3
V256IL
wkst4
Acer V246HL
Thanks
You need to iterate your CSV file rows, and check each row, if the row is a Byte Data row, convert it using: [System.Text.Encoding]::ASCII.GetString
So for example:
foreach ($row in Import-Csv c:\filepath.csv)
{
if ($row.Modello -is [array] # just an example, needs more validation
{
[System.Text.Encoding]::ASCII.GetString([byte]$Row.Modello) # save it somewhere
}
}
As you did not provided any code or showing your work, I'm just showing an example of what you can probably do,

How can I extrapolate the reversed hex string with Flutter?

through flutter I am reading a series of beacons with nfc_manager, the string I read is the one reported in the log below, how can I extrapolate the reversed hex string to be able to print it from the string represents in the log?
Flutter Code:
NfcManager.instance.startSession(onDiscovered: (NfcTag tag) async {
log(tag.data.toString());
});
Log print:
{nfca: {identifier: [4, 7, 255, 82, 190, 78, 129], atqa: [68, 0],
maxTransceiveLength: 253, sak: 0, timeout: 618},
mifareultralight: {identifier: [4, 7, 255, 82, 190, 78, 129],
maxTransceiveLength: 253, timeout: 618, type: 1}, ndefformatable: {identifier: [4, 7, 255, 82, 190, 78, 129]}}
Decode the json string like
var convert = json.decode(tag.data.toString());
Then you can use String.fromCharCodes like the following
List<int> charCodes = convert['nfca']['identifier'];
print(new String.fromCharCodes(charCodes));

How can I convert a compressed .gz file that I'm getting from a bluetooth Low-energy device to an actual decompressed file in Swift 4.2, using gzip?

I am very new to Xcode and iOS, I have a device, let's call it Brains, that I'm connecting to via Bluetooth LE using an app I built with Swift 4 and Xcode 10 on my iPhone 5, call it Body. Brains is similar to an arduino board, but not exactly. I can connect and get all the data with BLE with no problems, until I tried to get a compressed file filled with json strings.
I am receiving the compressed bytes but I can't seem to know what to do next. How can I get the compressed file, decompress it and read the data inside?
I have tried many things from using the Modules: GzipSwift,
DataCompression and SSZipArchive
I have used gunzipped(), gunzip() and decompress() but none of them seem to work.
I have read this thread: iOS :: How to decompress .gz file using GZIP Utility? and it say that I have to get all the compressed bytes stream and convert that to NSData and then decompress it, trouble is he's Using objective-c and I cant seem to translate into swift 4.
I'm getting the bytes from the Bluetooth LE characteristic in a [UInt8] array, in this function:
func received_logs(data: [UInt8]) {
let data_array_example = [31, 139, 8, 8, 16, 225, 156, 92, 2, 255, 68, 97, 116, 97, 0, 181, 157, 107, 110, 220, 56, 16, 6, 175, 226, 3, 248, 71, 63, 73, 234, 44, 193, 222, 255, 26, 171, 30, 35, 192, 90, 20, 18, 121, 182, 11, 112, 16, 35, 48, 10, 31, 154, 197, 22, 135, 34, 227, 95, 191, 76, 244, 16, 183, 248, 252, 48, 137, 229, 38, 242, 249, 161, 231, 87, 156, 127, 207, 113, 126, 227, 159, 31, 231, 183, 110, 223, 255, 200, 239, 47, 203, 252, 253, 173, 255, 231, 159, 235, 235, 108, 105, 110, 101, 48, 47, 50, 48]
for data_byte in stride(from: 0, to: data_array_example.count, by: 1) {
let byte = String(data_array_example[data_byte])
sourceString = sourceString + byte //getting all the bytes and converting to string to store in a variable
}
/******************************************************************/
let text = sourceBuffer
do {
try text.write(to: path!, atomically: false, encoding: String.Encoding.utf8)
}
catch {
print("Failed writing")
} //dump the var into a txt file
/**********UPDATED**********/
var file_array : [UInt8] = []
let byte2 = NSData(data: data_array_example.data)
let asc_array = Data(bytes: byte2.data)
let decompressedData: Data
do {
try decompressedData = asc.gunzipped()
print("Decom: ", String(data: decompressedData, encoding: .utf8))
}
catch {
print(error) //Gives me the "unknown compression method error"
}
}
I expect to see the Uncompressed file's contents but I only get:
GzipError(kind: Gzip.GzipError.Kind.data, message: "incorrect header check")
Maybe I'm just making it more complicated than It needs to be. Any help would be greatly appreciated!
Thank you very much :)
UPDATE:
I created a .gz file and used the both the gunzipped() and gunzip() functions and both of them worked.
UPDATE:
Tried to directly convert the data to NSData and then gunzip() but now getting the error:
GzipError(kind: Gzip.GzipError.Kind.data, message: "unknown compression method")
The updated example data has a correct gzip header, and so would not be giving you an incorrect header check if you are feeding the data correctly to the gunzipper.
I solve my issue. Turns out I was miscounting the bytes and some of them were in the wrong order. Thank you guys for your help!

Hash is not working on Array<UInt8> datatype in swift

I'm trying to convert a payload using CBOR.encode method from SwiftCBOR which gives me a result in Array<UInt8> format and when the result is being hashed its gives the result in Array<UInt8> format but i need the hash of it
payload = {productName:"Philips Hue White A19 4-Pack 60W Equivalent Dimmable LED Smart Bulb"}
let payloadBytes = CBOR.encode(payload)
print("payloadbytes:",payloadBytes); // payloadbytes: [120, 83, 123,..., 98, 34, 125]
print("hashed payload",payloadBytes.sha512()); //[176, 26, 154,..., 85, 75, 77]
the expected output of hashing the payloadbytes is
6097B04A89468E7AAA8A1784B9CBAF0D2E968AFFC7F5AFE3B0B30CDF44A79EB41464EC773D0D729FA7E6AD6F7462EDEA09B34177CDB0D0DF4A6DDF3C6117C01E

ZXing truncading negative bytes

In ZXing i'm creating a string of a binary data using the encode "ISO-8859-1"
but somehow negative bytes in the data get truncated to byte 63 when reading the produced QR code
Example: String before QR code (as bytes)
-78, 99, -86, 15, -123, 31, -11, -64, 77, -91, 26, -126, -68, 33
String read from QR code:
63, 99, 63, 15, 63, 31, 63, 63, 77, 63, 26, 63, 63, 33
How do I prevent that without using base64?
For some reason ZXing assembles the QR matrix with the correct data, it's the reading that truncates the bytes. I ended up sidestepping the problem by encoding my binary data to base64 and dealing with the increased message size