Isn't there any built-in solution for Tact?
I have used this function to do so but I want it to return a string instead of Slice:
inline fun addressToHex(value: Int): Slice {
if (value == 0) {
let buffer: Builder = beginCell();
let i: Int = 0;
while (i < 64){
buffer = buffer.storeUint(0, 8);
i = i + 1;
}
return buffer.endCell().beginParse().preloadBits(i * 8);
} else {
let j: Int = value;
let length: Int = 0;
while (j != 0) {
length = length + 1;
j = j >> 4;
}
let mask: Int = 15;
let buffer: Builder = beginCell();
while (value != 0) {
let curr: Int = (value & mask);
if(curr > 9){
buffer = buffer.storeUint(55 + curr, 8);
} else {
buffer = buffer.storeUint(48 + curr, 8);
}
value = value >> 4;
}
let final_cell: Builder = beginCell();
let reversed_buffer: Slice = buffer.endCell().beginParse();
let i: Int = 64;
while(i > 0){
i = i - 1;
let chr: Slice = reversed_buffer.preloadBitsOffset(i * 8, 8);
final_cell = final_cell.storeSlice(chr);
}
return final_cell.endCell().beginParse().preloadBits(512);
}
}
Nov 30, 2023, 11:33 AM
i recognize this code
arent string just slice in tact?
arent string just slice in tact?
Nov 30, 2023, 11:35 AM
This is what stonfi uses to convert address hash to hex string.
I don't know how to convert the slice to string.
I don't know how to convert the slice to string.
Nov 30, 2023, 11:36 AM
you are using an old and bugged version
use this one
https://github.com/ston-fi/funcbox/blob/9664ac18d5ac0128b20cd524d2dc28425417bc1f/contracts/utils.fc#L7
Nov 30, 2023, 11:37 AM
👍
Thanks
Nov 30, 2023, 11:37 AM
https://docs.tact-lang.org/language/ref/strings#sliceasstring
seems asString(self: Slice): String; should do the job
seems asString(self: Slice): String; should do the job
just add asString() to the last instruction
Nov 30, 2023, 11:39 AM
I will try that
Thank you so much
Thank you so much
Nov 30, 2023, 11:39 AM
do not use that function for computing stuff onchain, only in offchain "get-methods". its really expansive
Nov 30, 2023, 11:40 AM
No. You can invoke FunC function from Tact using native fun ....
Nov 30, 2023, 11:41 AM