I'd like to format url with number parameters as following:
http://someurl.com/?param1=100¶m2=200
The problem is, it's always truncated after first number parameter e.g:
http://someurl.com/?param1=100. Not sure why.
In my contract I'm using strings library https://github.com/Arachnid/solidity-stringutils
contract code:
import "strings.sol";
contract MyContract {
using strings for *;
function bytes32ToString (bytes32 data) constant returns (string) {
bytes memory bytesString = new bytes(32);
for (uint j=0; j<32; j++) {
byte char = byte(bytes32(uint(data) * 2 ** (8 * j)));
if (char != 0) {
bytesString[j] = char;
}
}
return string(bytesString);
}
function uintToBytes(uint v) constant private returns (bytes32 ret) {
if (v == 0) {
ret = '0';
}
else {
while (v > 0) {
ret = bytes32(uint(ret) / (2 ** 8));
ret |= bytes32(((v % 10) + 48) * 2 ** (8 * 31));
v /= 10;
}
}
return ret;
}
function uintToString(uint v) constant private returns (string ret) {
return bytes32ToString(uintToBytes(v));
}
function formatUrl(uint param1, uint param2) constant returns(string) {
var parts = new strings.slice[](4);
parts[0] = "http://someurl.com/?param1=".toSlice();
parts[1] = uintToString(param1).toSlice();
parts[2] = "&?param2=".toSlice();
parts[3] = uintToString(param2).toSlice();
return "".toSlice().join(parts);
}
}
So calling formatUrl(100, 200) returns "http://someurl.com/?param1=100"
If I changed function like this:
function formatUrl(uint param1, uint param2) constant returns(string) {
var parts = new strings.slice[](4);
parts[0] = "http://someurl.com/?param1=".toSlice();
parts[3] = uintToString(param1).toSlice();
parts[1] = "&?param2=".toSlice();
parts[2] = uintToString(param2).toSlice();
return "".toSlice().join(parts);
}
I get output: "http://someurl.com/?param1=&?param2=200"
It always ends after inserting first number converted to string.
http://someurl.com/?param1=100&?param2=200, which as far as I understand is the desired output. How do you test this? – 4gn3s Sep 01 '16 at 12:50Version: 0.3.5-48238c9f/Release-Darwin/appleclang/JIT. Browser solidity is using now0.3.6-3fc68da5/Release-Emscripten/clang. I don't know maybe it's that. Anyways 0.3.5 is latest version on npm – ma2s Sep 01 '16 at 14:52strings.solcode? Can you maybe try pasting thestrings.solcode from github directly into your file to make sure that the correct file gets imported? – 4gn3s Sep 01 '16 at 14:59