Cookbook
Type conversion

Type conversion

This page shows examples of converting between primitive types and obtaining them from composite types.

IntString

How to convert a String to an Int

// Defining a new extension function for type String that returns value of type Int
// Caution: produces unexpected results when String contains non-numeric characters!
extends fun toInt(self: String): Int {
    // Cast the String as a Slice for parsing
    let string: Slice = self.asSlice();
 
    // A variable to store the accumulated number
    let acc: Int = 0;
 
    // Loop until the String is empty
    while (!string.empty()) {
        let char: Int = string.loadUint(8); // load 8 bits (1 byte) from the Slice
        acc = (acc * 10) + (char - 48);     // using ASCII table to get numeric value
        // Note, that this approach would produce unexpected results
        //   when the starting String contains non-numeric characters!
    }
 
    // Produce the resulting number
    return acc;
}
 
fun runMe() {
    let string: String = "26052021";
    dump(string.toInt());
}

How to convert an Int to a String

let number: Int = 261119911;
 
// Converting the [number] to a String
let numberString: String = number.toString();
 
// Converting the [number] to a float String,
//   where passed argument 3 is the exponent of 10^(-3) of resulting float String,
//   and it can be any integer between 0 and 76 including both ends
let floatString: String = number.toFloatString(3);
 
// Converting the [number] as coins to a human-readable String
let coinsString: String = number.toCoinsString();
 
dump(numberString); // "261119911"
dump(floatString);  // "261119.911"
dump(coinsString);  // "0.261119911"

Struct or MessageCell or Slice

How to convert an arbitrary Struct or Message to a Cell or a Slice

struct Profit {
    big: String?;
    dict: map<Int, Int as uint64>;
    energy: Int;
}
 
message(0x45) Nice {
    maybeStr: String?;
}
 
fun convert() {
    let st = Profit{
        big: null,
        dict: null,
        energy: 42,
    };
    let msg = Nice{ maybeStr: "Message of the day!" };
 
    st.toCell();
    msg.toCell();
 
    st.toCell().asSlice();
    msg.toCell().asSlice();
}

How to convert a Cell or a Slice to an arbitrary Struct or Message

struct Profit {
    big: String?;
    dict: map<Int, Int as uint64>;
    energy: Int;
}
 
message(0x45) Nice {
    maybeStr: String?;
}
 
fun convert() {
    let stCell = Profit{
        big: null,
        dict: null,
        energy: 42,
    }.toCell();
    let msgCell = Nice{ maybeStr: "Message of the day!" }.toCell();
 
    Profit.fromCell(stCell);
    Nice.fromCell(msgCell);
 
    Profit.fromSlice(stCell.asSlice());
    Nice.fromSlice(msgCell.asSlice());
}
🤔

Didn't find your favorite example of type conversion? Have cool implementations in mind? Contributions are welcome! (opens in a new tab)