473,326 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to cast an specified type pointer to integer?

I want to package an address to byte buffer. So I need to cast it to
integer value.
How to cast it?

const char * ptr = 0x0013e328;
int value = <cast(ptr); // use reinterpret_cast?

Jan 30 '07 #1
10 7938
Allen wrote:
I want to package an address to byte buffer. So I need to cast it to
integer value.
Why? Surely if you are putting it in a byte buffer, you want a pointer?

--
Ian Collins.
Jan 30 '07 #2
On 1ÔÂ30ÈÕ, ÏÂÎç4ʱ12·Ö, Ian Collins <ian-n...@hotmail.comwrote:
Allen wrote:
I want to package an address to byte buffer. So I need to cast it to
integer value.

Why? Surely if you are putting it in a byte buffer, you want a pointer?

--
Ian Collins.

Yes.
I package RPC output parameter to a byte buffer. So I need to remember
the output variable address.
How to do it?

Jan 30 '07 #3

Allen napsal:
I want to package an address to byte buffer. So I need to cast it to
integer value.
How to cast it?

const char * ptr = 0x0013e328;
int value = <cast(ptr); // use reinterpret_cast?
Yes, use reinterpret_cast. Or you can use union

union Cast
{
const char* szptr;
const int* iptr;
};

In both cases are you taking the responsibility for this conversion
(you claim you know how is it stored there). But for casting pointers
there should be no problem.

Jan 30 '07 #4
On 1ÔÂ30ÈÕ, ÏÂÎç4ʱ18·Ö, "Ondra Holub" <ondra.ho...@post.czwrote:
Allen napsal:
I want to package an address to byte buffer. So I need to cast it to
integer value.
How to cast it?
const char * ptr = 0x0013e328;
int value = <cast(ptr); // use reinterpret_cast?

Yes, use reinterpret_cast. Or you can use union

union Cast
{
const char* szptr;
const int* iptr;

};

In both cases are you taking the responsibility for this conversion
(you claim you know how is it stored there). But for casting pointers
there should be no problem.
I do like this:
int address =
reinterpret_cast<int>(reinterpret_cast<int*>(const _cast<char*>((ptr))))
;

But the compiler gives a warning, warning C4311: "reinterpret_cast":
from int* to int pointer cut off.

Jan 30 '07 #5

Allen napsal:
On 1ÔÂ30ÈÕ, ÏÂÎç4ʱ18·Ö, "Ondra Holub" <ondra.ho....@post.czwrote:
Allen napsal:
I want to package an address to byte buffer. So I need to cast it to
integer value.
How to cast it?
const char * ptr = 0x0013e328;
int value = <cast(ptr); // use reinterpret_cast?
Yes, use reinterpret_cast. Or you can use union

union Cast
{
const char* szptr;
const int* iptr;

};

In both cases are you taking the responsibility for this conversion
(you claim you know how is it stored there). But for casting pointers
there should be no problem.

I do like this:
int address =
reinterpret_cast<int>(reinterpret_cast<int*>(const _cast<char*>((ptr))))
;

But the compiler gives a warning, warning C4311: "reinterpret_cast":
from int* to int pointer cut off.
I see you are trying to assign pointer value to int. It is not good
idea. It is quite common, that pointer does not fit into int value
(for example on 64-bits systems). That's the reason for such warning.

Jan 30 '07 #6
Allen wrote:
I want to package an address to byte buffer. So I need to cast it to
integer value.
Why an integer? What if a pointer does not fit into
an integer? On my system
sizeof(void*)==8, sizeof(int)==4, what then?

I would try pointing a char* pointer to the
pointer in question (not to the address!) and
then read subsequent char's, incrementing the
pointer and storing them into a vector<char>
that would be sizeof(void*) elements long.

HTH,
- J.
Jan 30 '07 #7
On Tue, 30 Jan 2007 09:54:30 +0100, Jacek Dziedzic
<ja************************@gmail.comwrote:
>Allen wrote:
>I want to package an address to byte buffer. So I need to cast it to
integer value.

Why an integer? What if a pointer does not fit into
an integer? On my system
sizeof(void*)==8, sizeof(int)==4, what then?

I would try pointing a char* pointer to the
pointer in question (not to the address!) and
then read subsequent char's, incrementing the
pointer and storing them into a vector<char>
that would be sizeof(void*) elements long.
I suspect the OP is dealing with some I/O protocol that specifies a certain
number of bytes for pointer types, regardless of platform.

OP: This is very low level code and you'd better know what you're doing. It
helps to do the following as well:

#include "boost/static_assert.hpp"

BOOST_STATIC_ASSERT(sizeof(void*) <= sizeof(int));

See boost.org for the boost library.

-dr
Jan 30 '07 #8
On Jan 30, 12:29 am, "Allen" <che...@naritech.cnwrote:
I do like this:
int address =
reinterpret_cast<int>(reinterpret_cast<int*>(const _cast<char*>((ptr))))
;

But the compiler gives a warning, warning C4311: "reinterpret_cast":
from int* to int pointer cut off.- Hide quoted text -
Yes, like the other poster said, the sizeof int on your system may not
be big enough to hold a pointer value. reinterpret_cast is definitely
the right way to cast it. Instead of putting the result into an int,
you can use intptr_t which is guaranteed to be a typedef to an integer
type big enough to hold a pointer on your system. intptr_t is not
part of the C++ standard, but I believe it is part of the C standard,
and is available in g++, and may even be in the queue to be added to
the C++ standard in the future.

Ivan
http://www.0x4849.net

Jan 30 '07 #9
"Ivan Novick" <iv***********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
On Jan 30, 12:29 am, "Allen" <che...@naritech.cnwrote:
>I do like this:
int address =
reinterpret_cast<int>(reinterpret_cast<int*>(cons t_cast<char*>((ptr))))
;

But the compiler gives a warning, warning C4311: "reinterpret_cast":
from int* to int pointer cut off.- Hide quoted text -

Yes, like the other poster said, the sizeof int on your system may not
be big enough to hold a pointer value. reinterpret_cast is definitely
the right way to cast it. Instead of putting the result into an int,
you can use intptr_t which is guaranteed to be a typedef to an integer
type big enough to hold a pointer on your system. intptr_t is not
part of the C++ standard, but I believe it is part of the C standard,
and is available in g++, and may even be in the queue to be added to
the C++ standard in the future.

Ivan
http://www.0x4849.net
-----code-----------------------------------------------
#include <cstddef>

template<intstruct intselector;
template<struct intselector<1>
{ typedef unsigned char type; };
template<struct intselector<2>
{ typedef unsigned short type; };
template<struct intselector<3>
{ typedef unsigned int type; };
template<struct intselector<4>
{ typedef unsigned long type; };

// and if your platform supports it (non-standard):
//template<struct intselector<5>
// { typedef unsigned long long type; };

template<size_t Sstruct inttype
{
typedef typename intselector
<
S <= sizeof(unsigned char)
? 1
: S <= sizeof(unsigned short)
? 2
: S <= sizeof(unsigned int)
? 3
: S <= sizeof(unsigned long)
? 4
: 5
>::type type;
};

typedef inttype<sizeof(void*)>::type intptr_t;
-----end code-------------------------------------------
Now you have an intptr_t on every platform ;)

- Sylvester
Jan 31 '07 #10

"Sylvester Hesp" <s.****@oisyn.nlwrote in message
news:45*********************@news.xs4all.nl...
"Ivan Novick" <iv***********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
>On Jan 30, 12:29 am, "Allen" <che...@naritech.cnwrote:
>>I do like this:
int address =
reinterpret_cast<int>(reinterpret_cast<int*>(con st_cast<char*>((ptr))))
;

But the compiler gives a warning, warning C4311: "reinterpret_cast":
from int* to int pointer cut off.- Hide quoted text -

Yes, like the other poster said, the sizeof int on your system may not
be big enough to hold a pointer value. reinterpret_cast is definitely
the right way to cast it. Instead of putting the result into an int,
you can use intptr_t which is guaranteed to be a typedef to an integer
type big enough to hold a pointer on your system. intptr_t is not
part of the C++ standard, but I believe it is part of the C standard,
and is available in g++, and may even be in the queue to be added to
the C++ standard in the future.

Ivan
http://www.0x4849.net

-----code-----------------------------------------------
-----end code-------------------------------------------
Now you have an intptr_t on every platform ;)

- Sylvester
It has come to my attention that intptr_t is always signed, and the
uintptr_t is always unsigned (in C99). To adopt this behaviour the changes
to my code are of course straightforward.

- Sylvester
Jan 31 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Graham Nicholls | last post by:
Hi. I'm having some fun with numbers. I've extraced an image sizes from a jpeg file img_x,img_y=image.getsize() then I'm trying to use those sizes to scale the image, but because python...
12
by: Andreas Schmidt | last post by:
I am trying to understand the behavior of void pointers. Can someone explain to me why I get a segfault for the following program? #include <stdio.h> void* plusone(void* i){ int* arg =...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
6
by: John-Arne Lillebø | last post by:
Hi. I run into this problem and i could need some help to solve it. The project is an ASP.NET Web project. Including code sample of the problem. Any idea what is causing the error message ?...
12
by: Abhishek | last post by:
now suppose I have declared an integer value inside a function as int x; now if the return type of the function is of type (void *) then can I write return((void *)x) in side the function? I...
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
11
by: Dinsdale | last post by:
I am trying to determine what the current cast of an object is. Essentially, if I use GetType, it always returns the original type of the object created but I want to know what the current cast of...
3
by: NorseMN | last post by:
I suppose my problem really started when I decided to implement a top-down design, because management always wants to see the frosting before they let you bake a cake. In any case, I now seem to have...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.