Converting reference address to character string

I've been searching for a while, and haven't been able to find anything on this so I could really use some help.

I'm wanting to convert the reference address held by a pointer into a character string, combine the hexes into a single unsigned long int(using bitwise operators )so I can use the 32bits in conjunction with a separate algorithm to develop a more efficient, but less 'random', number, or should I say bit, generator that I'll be using in a Neural Network, with Genetic Algorithms used to modify the weights.

Any help on this subject would be greatly appreciated.
convert the reference address held by a pointer into a character string
First convert pointer to unsigned integral value as shown in code below, then use std::to_string() function to convert it to string. If you want to see it in hexadecimal, you will need to use stringstreams.

combine the hexes into a single unsigned long int(using bitwise operators )so I can use the 32bits in conjunction with a separate algorithm to develop a more efficient, but less 'random', number
This sounds like hashing.

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<iomanip>


int main()
{
    int a;
    int* x = &a;
    uintptr_t y = reinterpret_cast<uintptr_t>(x);
    std::cout << std::hex << y << ' ' << &a << ' ' << &y;
}

Wow...I can't believe I didn't think of that. But ya that's what I meant, hashing, I just couldn't think of it's proper name. Thank you very much though, I really appreciate the help.

I actually have one more question though, and I don't mean to be rude, but is this the most efficient way to reliably get the bit equivalent of a reference value, or couldn't I just convert the reference to an unsigned int and use bitwise operators to access the individual bits of the reference? Or even store the reference in a void* and access the bit using the bitwise operators?
Last edited on
couldn't I just convert the reference to an unsigned int
Unsigned int may be not large enough to hold pointer value.

You should use uintptr_t type which is unsigned integral value which is guaranteed to be large enough to hold a pointer.

You can then use bitwise operators on it, or you can use std::bitset class for easy access to individual bits:
http://en.cppreference.com/w/cpp/utility/bitset
http://en.cppreference.com/w/cpp/utility/bitset/bitset
http://en.cppreference.com/w/cpp/utility/bitset/operator_at
Ok, well I did some testing of my own and I got what I needed by doing this:
1
2
3
4
5
6
Uint32 funcBuf;//Buffer value used in functions
Uint32 refToUint32(void* ref)
{
	funcBuf = reinterpret_cast<Uint32>(ref);
	return funcBuf;
}

which returns the 32bit equivalent of any pointer or reference value, no matter the type, so thank you again because you basically showed me how to do it, just in a much more simple method than I though it would take.
Last edited on
Ow wow, thanks for mentioning the bitset class, I'll have to look at it a bit more, but it looks as though it would be a huge help with my functions and algorithms, so thanks yet again lol.
Topic archived. No new replies allowed.