0
点赞
收藏
分享

微信扫一扫

flutter 整型(int)与16进制字符(hex)相互转换

E_topia 2021-09-21 阅读 76

话不多说,直接进入正题

  //int ---> hex
  String _intToHex(int num) {
    String hexString = num.toRadixString(16);
    return hexString;
  }
 //hex ---> int
  int _hexToInt(String hex) {
    int val = 0;
    int len = hex.length;
    for (int i = 0; i < len; i++) {
      int hexDigit = hex.codeUnitAt(i);
      if (hexDigit >= 48 && hexDigit <= 57) {
        val += (hexDigit - 48) * (1 << (4 * (len - 1 - i)));
      } else if (hexDigit >= 65 && hexDigit <= 70) {
        // A..F
        val += (hexDigit - 55) * (1 << (4 * (len - 1 - i)));
      } else if (hexDigit >= 97 && hexDigit <= 102) {
        // a..f
        val += (hexDigit - 87) * (1 << (4 * (len - 1 - i)));
      } else {
        throw new FormatException("Invalid hexadecimal value");
      }
    }
    return val;
  }
 //int ---> 指定长度的hex (如指定长度为6的情况,0x000001 0x001234, 0xefab23)
  String _intToFormatHex(int num) {
    String hexString = num.toRadixString(16);
    print("hexString=$hexString");
    String formatString = hexString.padLeft(6, "0");
    print("formatHexString=$formatString");
    return formatString;
  }

点击一下floatingActionButton,测试结果如下:


完整代码如下:

import 'package:flutter/material.dart';
import 'package:flutter_app_log_test/zz_log.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {

          String a = _intToHex(998);
          int b = _hexToInt(a);
          print("a=$a");
          print("b=$b");

          String c = _intToFormatHex(998);
          print("c=$c");

        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  //int ---> hex
  String _intToHex(int num) {
    String hexString = num.toRadixString(16);
    return hexString;
  }

  //hex ---> int
  int _hexToInt(String hex) {
    int val = 0;
    int len = hex.length;
    for (int i = 0; i < len; i++) {
      int hexDigit = hex.codeUnitAt(i);
      if (hexDigit >= 48 && hexDigit <= 57) {
        val += (hexDigit - 48) * (1 << (4 * (len - 1 - i)));
      } else if (hexDigit >= 65 && hexDigit <= 70) {
        // A..F
        val += (hexDigit - 55) * (1 << (4 * (len - 1 - i)));
      } else if (hexDigit >= 97 && hexDigit <= 102) {
        // a..f
        val += (hexDigit - 87) * (1 << (4 * (len - 1 - i)));
      } else {
        throw new FormatException("Invalid hexadecimal value");
      }
    }
    return val;
  }

  //int ---> 指定长度的hex (如指定长度为6的情况,0x000001 0x001234, 0xefab23)
  String _intToFormatHex(int num) {
    String hexString = num.toRadixString(16);
    print("hexString=$hexString");
    String formatString = hexString.padLeft(6, "0");
    print("formatHexString=$formatString");
    return formatString;
  }


}

结尾

今天的分享先到这里了,后续会分享更多的干货,欢迎点赞,加关注了,祝君好运!!

举报

相关推荐

0 条评论