473,806 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_cas t?

Jan 30 '07 #1
10 7989
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.co mwrote:
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_cas t?
Yes, use reinterpret_cas t. 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...@po st.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_cas t?

Yes, use reinterpret_cas t. 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_cas t<int>(reinterp ret_cast<int*>( const_cast<char *>((ptr))))
;

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

Jan 30 '07 #5

Allen napsal:
On 1ÔÂ30ÈÕ, ÏÂÎç4ʱ18·Ö, "Ondra Holub" <ondra.ho....@p ost.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_cas t?
Yes, use reinterpret_cas t. 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_cas t<int>(reinterp ret_cast<int*>( const_cast<char *>((ptr))))
;

But the compiler gives a warning, warning C4311: "reinterpret_ca st":
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************ ************@gm ail.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.h pp"

BOOST_STATIC_AS SERT(sizeof(voi d*) <= sizeof(int));

See boost.org for the boost library.

-dr
Jan 30 '07 #8
On Jan 30, 12:29 am, "Allen" <che...@naritec h.cnwrote:
I do like this:
int address =
reinterpret_cas t<int>(reinterp ret_cast<int*>( const_cast<char *>((ptr))))
;

But the compiler gives a warning, warning C4311: "reinterpret_ca st":
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_cas t 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.goo glegroups.com.. .
On Jan 30, 12:29 am, "Allen" <che...@naritec h.cnwrote:
>I do like this:
int address =
reinterpret_ca st<int>(reinter pret_cast<int*> (const_cast<cha r*>((ptr))))
;

But the compiler gives a warning, warning C4311: "reinterpret_ca st":
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_cas t 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<intstr uct 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

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

Similar topics

18
5958
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 has decided that they are integers, I keep getting division by zero errors
12
6359
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 = (int*)i; int result = (*arg + 1); return (void*)result; }
3
2295
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 DataSet ' Create connection Dim cn As New OleDbConnection With cn .connectionstring =
6
2320
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 ? John-Arne Lillebø
12
2633
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 came across this in a document on multithreading in C..I can post the exact portion of code which works correctly with such an assignment if you people want. Please clarify my doubt.I hope I have conveyed it properly.
9
2729
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: 0x00000000 through 0xFFFFFFFE : Valid byte addresses
11
1793
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 the object is (i.e.: is it still cast as a Derived class, or is it cast as a Base class?) Here is some pseudo code to show the example. Class Base .... End Class
3
1453
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 the proverbial design that is simple, elegant... and wrong. Before I give up, I'd like to see if anyone in this forum has a solution. I seems that I need to perform a run-time cast to a type that is specified by an integer. Ideally, the...
7
3953
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
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10623
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10373
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.