如何将字符串转换为地址?

时间:2017-12-01 10:08:40

标签: c++ string qt casting int

int qs = 465;
std::ostringstream os;
os << &qs;
QString tt = QString::fromStdString(os.str());
qDebug() << &qs; // output: 0x28fda8
qDebug() << tt; // output: "0x28fda8"
int *pp = tt; // how can i do this?

我知道这很愚蠢,但我想尝试这样做

2 个答案:

答案 0 :(得分:4)

你不应该。但是,如果你真的必须抓住地址的价值并将其投射到int*。一个真正凌乱的方式是:

#include <iostream>
#include <sstream>
#include <cstdint>

int main() {
    int qs = 465;
    std::stringstream ss;
    ss << &qs;
    std::uintptr_t temp;
    ss >> std::hex >> temp;
    int* pp = reinterpret_cast<int*>(temp);
}

答案 1 :(得分:-1)

谢谢Ron

int qs = 465;
std::ostringstream os;
os << &qs;
QString tt = QString::fromStdString(os.str());
qDebug() << &qs; // output: 0x28fda8
qDebug() << tt; // output: "0x28fda8"
bool *r;
int* pp = reinterpret_cast<int*>(tt.toInt(r, 16));
qDebug() << *pp; // output: 465