codecamp

字面量和基本类型之间的转换

整数类型

十进制和十六进制数字文字可以隐式转换为任何足够大的整数类型,无需截断即可表示:

uint8 a = 12; // fine
uint32 b = 1234; // fine
uint16 c = 0x123456; // fails, since it would have to truncate to 0x3456

笔记

在 0.8.0 版本之前,任何十进制或十六进制数字文字都可以显式转换为整数类型。从 0.8.0 开始,此类显式转换与隐式转换一样严格,即,仅当文字符合结果范围时才允许它们。

固定大小的字节数组

十进制数字文字不能隐式转换为固定大小的字节数组。十六进制数字文字可以,但前提是十六进制数字的数量完全适合字节类型的大小。作为一个例外,具有零值的十进制和十六进制文字都可以转换为任何固定大小的字节类型:

bytes2 a = 54321; // not allowed
bytes2 b = 0x12; // not allowed
bytes2 c = 0x123; // not allowed
bytes2 d = 0x1234; // fine
bytes2 e = 0x0012; // fine
bytes4 f = 0; // fine
bytes4 g = 0x0; // fine

字符串文字和十六进制字符串文字可以隐式转换为固定大小的字节数组,如果它们的字符数与字节类型的大小匹配:

bytes2 a = hex"1234"; // fine
bytes2 b = "xy"; // fine
bytes2 c = hex"12"; // not allowed
bytes2 d = hex"123"; // not allowed
bytes2 e = "x"; // not allowed
bytes2 f = "xyz"; // not allowed

地址

地址文字中所述,通过校验和测试的正确大小的十六进制文字属于address类型。没有其他文字可以隐式转换为该address类型。

bytes20或任何整数类型的显式转换address导致.address payable

可以转换为via 。address aaddress payablepayable(a)

基本类型之间的转换
单位
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }