Flutter 16进制数字符串转 int

本文介绍了Flutter中将16进制数字符串转换为int以创建Color对象的两种方法,适用于从后台获取颜色数据并处理包含前缀和透明度的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flutter 中 Color 接收 int 型十六进制数,如果颜色数据是从后台获取,则一般拿到字符串,此时需要转为 int

Flutter 中 Color 定义:

(new) Color(int value) → Color
dart.ui

Construct a color from the lower 32 bits of an [int].

The bits are interpreted as follows:

Bits 24-31 are the alpha value.
Bits 16-23 are the red value.
Bits 8-15 are the green value.
Bits 0-7 are the blue value.
In other words, if AA is the alpha value in hex, RR the red value in hex, GG the green value in hex, and BB the blue value in hex, a color can be expressed as const Color(0xAARRGGBB).

For example, to get a fully opaque orange, you would use const Color(0xFFFF9000) (FF for the alpha, FF for the red, 90 for the green, and 00 for the blue).
复制代码

第一种方法 16进制数字符串转 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;
}
复制代码

第二种方法

如果后台返回的字符串中包含 '#', '0x' 前缀, 或者包含透明度,可处理一下

class HexColor extends Color {
  static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    hexColor = hexColor.replaceAll('0X', '');
    if (hexColor.length == 6) {
     hexColor = "FF" + hexColor;
    }
    return int.parse(hexColor, radix: 16);
  }

  HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}
复制代码
Flutter中,将字符串换为整数可以使用内置的int.parse()函数。该函数将字符串作为参数,并尝试将其换为整数。如果字符串不是有效的整数表示形式,则会引发FormatException。 以下是将Flutter字符串换为整数的示例代码: ```dart String str = "123"; int num = int.parse(str); ``` 在上面的示例中,字符串"123"被换为整数123并赋值给变量num。 需要注意的是,如果字符串无法换为整数,将引发FormatException。因此,在进行字符串换之前,我们需要确保字符串是一个有效的整数表示形式。 对于提到的数据溢出问题,可以使用try-catch语句来捕捉异常并处理它。例如: ```dart String str = "12345678901234567890"; try { int num = int.parse(str); // 处理成功换的整数 num } catch (e) { // 处理字符串换为整数时的异常情况 } ``` 通过在try块中执行换操作并在catch块中处理异常,可以更好地控制字符串换为整数时可能出现的数据溢出问题。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [string化成int,double,bool](https://download.youkuaiyun.com/download/walker19900515/8800053)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Flutter 16进制数字符串 int](https://blog.youkuaiyun.com/weixin_34319999/article/details/91369275)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值