Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to cast to int #1203

Open
fadhil-riyanto opened this issue Aug 21, 2024 · 3 comments
Open

Question: how to cast to int #1203

fadhil-riyanto opened this issue Aug 21, 2024 · 3 comments

Comments

@fadhil-riyanto
Copy link

fadhil-riyanto commented Aug 21, 2024

Hi, I have following map

std::map<std::string, int> db;

which in future, I want load data from leveldb to the memory by looping all of keys and insert to the std::map, I was do this

leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());

for (it->SeekToFirst(); it->Valid(); it->Next()) {
        db.insert(
                {
                        it->key().ToString(),
                        it->value()
                }
        );
}
assert(it->status().ok());

which get the key as string, then get the value. my question how do I can cast back the "value" to int?

@paweenwich
Copy link

For me, depend on how to you put data in LevelDB
if you put it as string (Ex. "1234" for 1234) then just convert parse it bact it int
if you put it as int32_t's binary (Ex 00 00 00 0F for 15) then just cast to back from string.data() to int32_t* and then copy to any where you like.

@fadhil-riyanto
Copy link
Author

I think converting the data back to int is little bit slow. But thanks for your reply and time!!

@Amr-Shams
Copy link

you simply can't do this unless you cast or decod which is not recommended in many cases espically if you are sending this through and responce it is better to be bytes and then casted in the presentation layer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants