Utf8Utils.dart 771 B

123456789101112131415161718192021222324252627282930313233
  1. import 'dart:convert';
  2. class Utf8Utils {
  3. static String encode(String origin) {
  4. if (origin == null || origin.length == 0) {
  5. return null;
  6. }
  7. List<int> list = utf8.encode(origin);
  8. StringBuffer sb = StringBuffer();
  9. for (int i in list) {
  10. sb.write("$i,");
  11. }
  12. String result = sb.toString();
  13. return result.substring(0, result.length - 1);
  14. }
  15. static String decode(String encodeStr) {
  16. if (encodeStr == null || encodeStr.length == 0) {
  17. return null;
  18. }
  19. List<String> list = encodeStr.split(",");
  20. if (list != null && list.isNotEmpty) {
  21. List<int> intList = List();
  22. for (String s in list) {
  23. intList.add(int.parse(s));
  24. }
  25. return utf8.decode(intList);
  26. }
  27. return null;
  28. }
  29. }