---
title: 类型转换
description: 常见的在基本类型之间转换并从复合类型中获取它们的示例
---

本页展示了在[原始类型][p]之间进行转换以及从[复合类型](/zh-cn/book/types#composite-types)中获取这些类型的示例。

## `Int` ↔ `String` {#int-string}

### 如何将 `String` 转换为 `Int` {#how-to-convert-a-string-to-an-int}

```tact
// 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());
}
```

### 如何将 `Int` 转换为 `String` 字符

```tact
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"
```

:::note[Useful links:]

  [`Int.toString(){:tact}` 在核心库中](/zh-cn/ref/core-strings#inttostring)/
  [`Int.toFloatString(){:tact}` 在核心库中](/zh-cn/ref/core-strings#inttofloatstring)/
  [`Int.toCoinsString(){:tact}` 在核心库中](/zh-cn/ref/core-strings#inttocoinsstring)

:::

## `Struct` 或 `Message` ↔ `Cell` 或 `Slice` {#structmessage-cellslice}

### 如何将任意的 `Struct` 或 `Message` 转换成一个 `Cell` 或 `Slice`

```tact {19-20, 22-23}
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();
}
```

:::note[Useful links:]

  [`Struct.toCell(){:tact}` 在核心库中](/zh-cn/ref/core-cells#structtocell)/
  [`Message.toCell(){:tact}` 在核心库中](/zh-cn/ref/core-cells#messagetocell)

:::

### 如何将 `Cell` 或 `Slice` 转换为任意的 `Struct` 或 `Message`

```tact {19-20, 22-23}
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());
}
```

:::note[Useful links:]

  [`Struct.fromCell(){:tact}` in Core library](/zh-cn/ref/core-cells#structfromcell)\
  [`Struct.fromSlice(){:tact}` in Core library](/zh-cn/ref/core-cells#structfromslice)\
  [`Message.fromCell(){:tact}` in Core library](/zh-cn/ref/core-cells#messagefromcell)\
  [`Message.fromSlice(){:tact}` in Core library](/zh-cn/ref/core-cells#messagefromslice)

:::

:::tip[Hey there!]

  没有找到您最喜欢的类型转换示例？  您有很酷的实施方案吗？ [欢迎投稿！](https://github.com/tact-lang/tact/issues)

:::

[p]: /zh-cn/book/types#primitive-types
