如何在 solidity 中发出 utf8 消息

How to emit utf8 message in solidity

如何在下面的代码中发出utf8信息(如中文)?

   pragma solidity ^0.7.2;
   modifier buyerOnly() {
    require(
        msg.sender == buyer,
        "For buyer ONLY"  //<<==utf8?
    );

中文报错

我想你是在问如何将 unicode 字符插入字符串。

免责声明:我不懂中文,但我使用Google翻译将“For buyer only”转换为简体中文:只适用于购买。然后我用an online tool将这个字符串转换成下面的\U和\x转义序列。

根据文字字符串编码的可靠性文档:

\xNN takes a hex value and inserts the appropriate byte, while \uNNNN takes a Unicode codepoint and inserts an UTF-8 sequence.

所以不要尝试这个:

modifier buyerOnly() {
 require(
     msg.sender == buyer,
     "For buyer ONLY"  //<<==utf8?
 );

这(UTF-16 转义将转换回 UTF-8)

modifier buyerOnly() {
 require(
     msg.sender == buyer,
     "\u4ec5\u9002\u7528\u4e8e\u4e70\u65b9"
 );

或者这个(单独插入 UTF-8 字节):

modifier buyerOnly() {
 require(
     msg.sender == buyer,
     "\xe4\xbb\x85\xe9\x80\x82\xe7\x94\xa8\xe4\xba\x8e\xe4\xb9\xb0\xe6\x96\xb9"
 );