Convert String & Number To Byte Array In Flutter & Dart

This blog will explore the Convert String & Number To Byte Array In Flutter & Dart. We perceive how to execute a demo program. We will show you how to convert a string or a number to a byte array in your Dart and Flutter applications.
If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.
Table Of Contents::
Converting a String into a Byte Array
Converting a Number into a Byte Array

Converting a String into a Byte Array:
There is more than one method for transforming a given string into a byte array in Dart.
> Using the utf8.encode() method:
The utf8.encode() technique can be imported from Dart: convert takes a string and returns a list of UTF-8 bytes.
The code:
import 'dart:convert';
void main() {
String text = 'FlutterDevs.com';
List<int> bytes = utf8.encode(text);
print(bytes);
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
[70, 108, 117, 116, 116, 101, 114, 68, 101, 118, 115, 46, 99, 111, 109]
Process finished with exit code 0
- > Using the codeUnits property of the String class:
The codeUnits property of the String class returns a list of UTF-16 code units.
The code:
void main() {
String text = 'FlutterDevs.com';
List<int> bytes = text.codeUnits;
print(bytes);
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
[70, 108, 117, 116, 116, 101, 114, 68, 101, 118, 115, 46, 99, 111, 109]
Process finished with exit code 0
- > Using the runes property of the String class:
The runes property gives you an iterable of Unicode code points of a string.
The code:
void main() {
String text = 'FlutterDevs.com Welcome you';
Iterable<int> bytes = text.runes;
List<int> list = bytes.toList();
print(list);
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
[70, 108, 117, 116, 116, 101, 114, 68, 101, 118,
115, 46, 99, 111, 109, 32, 87, 101, 108, 99, 111,
109, 101, 32, 121, 111, 117]
Process finished with exit code 0
Converting a Number into a Byte Array:
To convert a number over entirely to a byte array, you can initially change it over completely to a string and afterward utilize one of the methodologies referenced in the past section to get the job done.
The code:
import "dart:convert";
void main() {
int myInt = 2023;
print(utf8.encode(myInt.toString()));
double myDouble = 7.4235;
print(utf8.encode(myDouble.toString()));
}
When we run the application, we ought to get the screen’s output like the underneath screen Console Output.
[50, 48, 50, 51]
[55, 46, 52, 50, 51, 53]
Process finished with exit code 0
Conclusion:
In the article, I have explained the Convert String & Number To Byte Array In Flutter & Dart; you can modify this code according to your choice. This was a small introduction to Convert String & Number To Byte Array In Flutter & Dart User Interaction from my side, and it’s working using Flutter.
I hope this blog will provide you with sufficient information on Trying the Convert String & Number To Byte Array In Flutter & Dart of your projects. So please try it.
❤ ❤ Thanks for reading this article ❤❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter-related queries.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.
