【C++学习入门】18. 类型转换之基础类型

#include <iostream>
#include <string>
#include <vector>
using namespace std;
void string2char()
{
    cout << "> string to char" << endl;
    string s = "hello world";
    const char *c = s.c_str();
    cout << c[2] << endl;
}

void string2int()
{
    cout << "> string to int" << endl;
    string s = "10086";
    int i = std::stoi(s); // atoi不行,atoi针对的是c字符串
    cout << i << endl;
}

void string2byte()
{
    cout << "> string to bytes" << endl;
    string s = "10086 world";
    vector<uint8_t> bytes{s.begin(), s.end()};
    std::cout << "string: " << s << std::endl;
    cout << "string[3]: " << bytes[3] << endl;
    cout << "Bytes: ";
    for (auto b : bytes)
    {
        cout << hex << static_cast<int>(b) << " ";
    }
    cout << endl;
}

void int2long()
{
    cout << "> int2long" << endl;
    int i = 13;
    long l = (long)i;          // c中的类型转换,没有过多的类型校验。
    l = static_cast<long>(13); // c++中的类型转换,编译器会检查类型。
    cout << dec << l << endl;
}

void int2string()
{
    cout << "> int2string" << endl;
    int i = 123;
    string s = to_string(i); // c++ 11
    cout << s << endl;
}

// void int2bytes()
// {
//     cout << "> int2bytes" << endl;
//     unsigned char bytes[] = {0x1, 0x2, 0x3, 0x4};
//     int result = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
//     cout << dec << result << endl;
// }


int main()
{
    string2char();
    string2int();
    string2byte();
    int2long();
    int2string();
    // int2bytes();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值