get value from an address

Hi all..

I have a string containing an address:

string str = "0x0CA8";

assume 0x0CA8 is the address of some value.
my question is, how to get the value of address 0x0CA8 ?

Thanks.
you need to use pointers but i don't have a clue how will you know its data type
well void pointers dont know the data type but i dont think its good practice. Also i dont know if you can just goto an address like that. I dont even know how you would get that address of a variable into a string. like this

string str = &somestring; // i dont think this is correct at all

What exactly are you trying to do p4ilul. Are you trying to get a value from a pointer by dereferencing maybe?? We need to know in order to help but i dont think the above is possible or usefeul really
hi blackcoder41,

that's the first thing crossed in my mind, use pointer. But how to assign the pointer to 0x0CA8 via str?

i use Qt's QVariant to store the value. QVariant is able to store various data type.
btripp,

yes something like that.. dereferencing. But i don't have any idea to do it.

actually it comes from a device which gives a response like this:

"01 03 08 0C A8 41 E1..."

i store it in a string variable, parse it to get the 0CA8 part, then get the value of 0CA8.

i'm sorry if my question isn't clear enough.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[]) {
    string str = "0x0CA8";
    long int num = strtol (str.c_str(),NULL,16);//convert hex string to numeric
    cout << num << '\n'; //prove it
    char* p1 = 0;
    p1 = (char*)num; //cast num to pointer (note this might not work)
    cout << *p1 << '\n'; //note dereferencing might not work

    return 0;
}
didn't work, blackcoder..
I see what your saying I just dont know how to go about that though
it is expected not to work, LOL
blackcoder hit the nail on the head as far as I can tell.

1
2
3
volatile char* ptr = volatile char*(0x0CA8);

cout << int(*ptr);  // read the number at that address 


The thing that's really the problem is.... what makes you think that 0x0CA8 is a valid address? Raw I/O like this isn't wise unless you're doing really low-level hardware I/O (like writing a driver or something).
actually i'm developing a Modbus RTU application. Do you have any recommendation what Windows open source Modbus library for me?

sorry for my late response. Thanks
Topic archived. No new replies allowed.