When I convert the hex value to ASCII and pass on the ASCII to textField, it doubles the text length. Below is the conversion I used and if you see from the screenshot, the total characters for both the textfields are 16, but it is showing as 32. May I know why the length is doubling?
String hexToAscii(String hexString) => List.generate(
hexString.length ~/ 2,
(i) => String.fromCharCode(
int.parse(hexString.substring(i * 2, (i * 2) + 2), radix: 16)),
).join();
// Adding Textfield
final modelController = TextEditingController();
modelController.text = hexToAscii(tempData[0]); // Passing Data. I used your method in place of hexToAscii method. No Change
_buildExpandableContent(int selectedENPIndex) {
List<Widget> columnContent = [];
for (int i = 0; i < namePlates[selectedENPIndex].contents.length; i++) {
TextEditingController dynamicController = TextEditingController();
//var allowedCharacters = 'r\'^[a-zA-Z0-9. ]*\'';
var allowedCharacters = namePlates[selectedENPIndex].acceptingString[i];
//print("Allowed Characters: $allowedCharacters");
var allowedRegEx = RegExp(escape(allowedCharacters));
//print("Allowed RegEx: $allowedRegEx");
if (selectedENPIndex == 0) {
if (namePlates[selectedENPIndex]
.contents[i]
.replaceAll(RegExp('[^A-Za-z0-9]'), '')
.toLowerCase() ==
ENPTextFieldNames.model.name) {
dynamicController = modelController;
}
}
columnContent.add(Container(
padding: const EdgeInsets.only(left: 50, right: 50, bottom: 10),
child: TextFormField(
enableInteractiveSelection: false,
maxLength: selectedENPIndex == 2 ? 24 : 16,
// autofocus: true,
cursorColor: _selectedTheme.primaryColorDark,
// inputFormatters: <TextInputFormatter>[
// FilteringTextInputFormatter.allow(RegExp(allowedCharacters))
// ],
onChanged: (value) {
checkRegEx();
_selectedField = dynamicController;
},
controller: dynamicController,
validator: (value) {
if (value == null || value.trim().isEmpty) {
return "Please enter ${namePlates[selectedENPIndex].contents[i]}";
}
return null;
},
decoration: InputDecoration(
border: UnderlineInputBorder(
borderSide:
BorderSide(color: _selectedTheme.primaryColorDark)),
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: _selectedTheme.primaryColorDark)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: _selectedTheme.primaryColorDark, width: 2)),
labelText: namePlates[selectedENPIndex].contents[i],
hintText: namePlates[selectedENPIndex].acceptingString[i],
labelStyle: TextStyle(color: _selectedTheme.primaryColorDark)),
),
));
}
columnContent.add(const Padding(
padding: EdgeInsets.only(top: 20, bottom: 20),
));
return columnContent;
void main() {
var hexString = "004D006F00640065006C0020004D006F00640065006C0020004D006F00640065";
print(hexToAscii(hexString)); // Output is Model Model Mode
print(hexToAscii(hexString).length); // Output is 32, this should be 16. Attached the Dart Pad screenshot
}
String hexToAscii(String hexString) => List.generate(
hexString.length ~/ 2,
(i) => String.fromCharCode(
int.parse(hexString.substring(i * 2, (i * 2) + 2), radix: 16)),
).join();
The issue you're facing is likely due to the presence of null characters (i.e., \u0000)
in your ASCII string.
These null characters are often used as padding in UTF-16
encoding and may cause the text length to double when processed as a string in Dart.
You can improve your hexToAscii function by filtering out the null characters after converting the hex string to ASCII
and ensuring the conversion process correctly handles the input string.
This should properly work for you.
String hexToAscii(String hexString) {
// Convert hex string to ASCII, then remove null characters
return List.generate(
hexString.length ~/ 4, // Using ~/4 for UTF-16 (each char is 4 hex digits)
(i) {
var charCode = int.parse(hexString.substring(i * 4, (i * 4) + 4), radix: 16);
return String.fromCharCode(charCode);
},
).join().replaceAll('\u0000', '');
}
Alternatively, you can use the dart:convert
package.
import 'dart:convert';
import 'dart:typed_data';
void main() {
var hexString = "004D006F00640065006C0020004D006F00640065006C0020004D006F00640065";
print(hexToAscii(hexString)); // Output should be "Model Model Model"
print(hexToAscii(hexString).length); // Output should be 15
}
String hexToAscii(String hexString) {
Uint8List bytes = hexStringToBytes(hexString);
String asciiString = utf8.decode(bytes, allowMalformed: true);
return asciiString.replaceAll('\u0000', '');
}
Uint8List hexStringToBytes(String hexString) {
List<int> bytes = [];
for (int i = 0; i < hexString.length; i += 4) {
String byteString = hexString.substring(i, i + 4);
int byte = int.parse(byteString, radix: 16);
bytes.add(byte >> 8);
bytes.add(byte & 0xFF);
}
return Uint8List.fromList(bytes);
}