manbytesgnu_site

Source files for manbytesgnu.org
git clone git://holbrook.no/manbytesgnu_site.git
Info | Log | Files | Refs

tohex.sol (830B)


      1 contract NFT {
      2 
      3 	uint256[] token;
      4 	mapping(uint256 => uint256) tokenIndex; // map token id to master token array index position
      5 
      6 	[...]
      7 
      8 	function tokenURI(uint256 _tokenId) public pure returns(string memory) {
      9 		bytes32 token_bytes;
     10 		bytes memory out;
     11 		uint8 t;
     12 		uint256 c;
     13 
     14 		token_bytes = bytes32(token[tokenIndex[_tokenId]]);
     15 
     16 		out = new bytes(64 + 7);
     17 		out[0] = "s";
     18 		out[1] = "h";
     19 		out[2] = "a";
     20 		out[3] = "2";
     21 		out[4] = "5";
     22 		out[5] = "6";
     23 		out[6] = ":";
     24 		
     25 		c = 7;	
     26 		for (uint256 i = 0; i < 32; i++) {
     27 			t = (uint8(_data[i]) & 0xf0) >> 4;
     28 			if (t < 10) {
     29 				out[c] = bytes1(t + 0x30);
     30 			} else {
     31 				out[c] = bytes1(t + 0x57);
     32 			}
     33 			t = uint8(_data[i]) & 0x0f;
     34 			if (t < 10) {
     35 				out[c+1] = bytes1(t + 0x30);
     36 			} else {
     37 				out[c+1] = bytes1(t + 0x57);
     38 			}
     39 			c += 2;
     40 		}
     41 		return string(out);
     42 	}
     43 }