Hat jemand das 'somefunc' unten, um Bytes (insbesondere Bytes4) als Zeichenfolge zu speichern? Idealerweise etwas, um method_s im folgenden Code zu erhalten:
contract Test{
bytes4 public method;
string public method_s;
function Test() {
method = this.get_f.selector;
}
function makeString(){
method_s = somefunc(method);
}
}
Also brauche ich method = 0x0cb9ef7a und method_s = "0x0cb9ef7a"
Vielen Dank im Voraus
Dieser Code konvertiert ein bytes4 in eine hexadezimale Zeichenfolge
function toHexDigit(uint8 d) pure internal returns (byte) {
if (0 <= d && d <= 9) {
return byte(uint8(byte('0')) + d);
} else if (10 <= uint8(d) && uint8(d) <= 15) {
return byte(uint8(byte('a')) + d - 10);
}
revert();
}
function fromCode(bytes4 code) public view returns (string) {
bytes memory result = new bytes(10);
result[0] = byte('0');
result[1] = byte('x');
for (uint i=0; i<4; ++i) {
result[2*i+2] = toHexDigit(uint8(code[i])/16);
result[2*i+3] = toHexDigit(uint8(code[i])%16);
}
return string(result);
}
auf 0,8+ angepasst
function toHexDigit(uint8 d) pure internal returns (bytes1) {
if (0 <= d && d <= 9) {
return bytes1(uint8(bytes1('0')) + d);
} else if (10 <= uint8(d) && uint8(d) <= 15) {
return bytes1(uint8(bytes1('a')) + d - 10);
}
revert();
}
function fromCode(bytes4 code) public pure returns (string memory) {
bytes memory result = new bytes(10);
result[0] = bytes1('0');
result[1] = bytes1('x');
for (uint i = 0; i < 4; ++i) {
result[2 * i + 2] = toHexDigit(uint8(code[i]) / 16);
result[2 * i + 3] = toHexDigit(uint8(code[i]) % 16);
}
return string(result);
}
das Fett
Ismael