473,385 Members | 1,409 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,385 software developers and data experts.

casting problem

Hi ppl

Some problem regarding pointer:
ULONG *addr;
CHAR *ptr=NULL;
addr=new ULONG;
//calling a function and getting the addr value
//ULONG is 4 bytes. I am getting the Ip address which is naturally 4
bytes and
//storing the value in the location given by addr
ptr=(char *)addr;

printf("\n%X\n",*addr); //gives the IP address correctly. total 32
bits

//next line is the problem
printf("%X.%X.%X.%X\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3));

The above line should give the same value as *addr. But its reading
wrongly from the memory. First the byte position have interchanged(i
converted it to network order and saved this value in addr and *addr
gives the network order but the last line is printing in host order).
Secondly, the last byte read is garbage.

any suggestions??...basically i want to convert the ULONG into
CHAR[4]. Any other way to do it i.e. without casting??

thanks
Harry
Jul 22 '05 #1
4 1997
Harry wrote:

any suggestions??...basically i want to convert the ULONG into
CHAR[4]. Any other way to do it i.e. without casting??

thanks
Harry


what about a union?

union
{
ULONG u;
CHAR c[4];
} an_ip_address;
Jul 22 '05 #2

"Harry" <ar******@fht-esslingen.de> skrev i en meddelelse
news:f9**************************@posting.google.c om...
Hi ppl

Some problem regarding pointer:
ULONG *addr;
CHAR *ptr=NULL;
addr=new ULONG;
//calling a function and getting the addr value
//ULONG is 4 bytes. I am getting the Ip address which is naturally 4
bytes and
//storing the value in the location given by addr
ptr=(char *)addr;

printf("\n%X\n",*addr); //gives the IP address correctly. total 32
bits
so it seems that *addr holds the ip-address.
//next line is the problem
printf("%X.%X.%X.%X\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3));

The above line should give the same value as *addr. But its reading
wrongly from the memory. First the byte position have interchanged(i
converted it to network order and saved this value in addr and *addr
gives the network order but the last line is printing in host order).
Secondly, the last byte read is garbage.

any suggestions??...basically i want to convert the ULONG into
CHAR[4]. Any other way to do it i.e. without casting??
Why CHAR?? I would presume a char is correct. Without knowing what a CHAR
is, i don't know if that is bothering you. Another thing i fail to
understand why you do not simply use simple arithmetic (division and
modulus) to get your ipadress. This way you do not have to rely on any
spicific byte order.


thanks
Harry


/Peter
Jul 22 '05 #3
Harry wrote:

There is no type called CHAR or ULONG. It would be nice if you
stuck to real C++ types or told us the nature of any types
you introduce.
CHAR *ptr=NULL;
addr=new ULONG; ptr=(char *)addr;

printf("\n%X\n",*addr); //gives the IP address correctly. total 32
bits

//next line is the problem
printf("%X.%X.%X.%X\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3));
The above line should give the same value as *addr. But its reading
wrongly from the memory. First the byte position have interchanged(i
converted it to network order and saved this value in addr and *addr
gives the network order but the last line is printing in host order).
Secondly, the last byte read is garbage.


First, I would use (unsigned char*) rather than (char*). It is possible
that the char type is signed. When you pass a char to a vararg'd function
like printf, the char is widened to int. If the char value happens to be
one that is negative you might get unexpected answers.

You most certainly have a byte ordering issue that's unrelated to casting.
You haven't shown us your "converted to network order" but I suspect you've
made an error there. On an LSB-first machine (like Intel processors typically)
ptr[1] will be the low order byte. The question is also whether the addr
as ULONG was already in the proper byte order or not.

The way to do this without casting is to use shifts (which also potentially
gets around the byte ordering issues).

printf("%X.%X.%X.%X\n", (x >> 24) & 0xFF, (x >> 16) & 0xFF, (x>>8) & 0xFF, x&0xFF);
Jul 22 '05 #4
Harry wrote:
Hi ppl

Some problem regarding pointer:
ULONG *addr;
CHAR *ptr=NULL;
addr=new ULONG;
//calling a function and getting the addr value
//ULONG is 4 bytes. I am getting the Ip address which is naturally 4
bytes and
//storing the value in the location given by addr
ptr=(char *)addr;
Should probably be

ptr = (CHAR*)addr;

Why do you need all this dancing with 'new'? Just do (I will use your
notation for types, although here they require definition):

ULONG addr;
// retrieve the IP address _into_ 'addr', not into '*addr', no "new"
CHAR *ptr = &addr;

printf("\n%X\n",*addr); //gives the IP address correctly. total 32
bits
With my correction you would do

printf("\n%X\n", addr);

//next line is the problem
printf("%X.%X.%X.%X\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3));

The above line should give the same value as *addr. But its reading
wrongly from the memory. First the byte position have interchanged(i
converted it to network order and saved this value in addr and *addr
gives the network order but the last line is printing in host order).
Endianness is an implementation detail and if you get the wrong order,
just invert it.
Secondly, the last byte read is garbage.
I don't understand that statement.

any suggestions??...basically i want to convert the ULONG into
CHAR[4]. Any other way to do it i.e. without casting??


Definitely. Now I'll use normal type notation:

typedef unsigned long ulong;
typedef unsigne char uchar;
ulong addr = 0x12345678;
uchar ptr[4] = {(addr & 0xff000000UL) >> 24,
(addr & 0xff0000UL) >> 16,
(addr & 0xff00U) >> 8,
(addr & 0xff) };
printf("%X.%X.%X.%X\n", ptr[0], ptr[1], ptr[2], ptr[3]);

(assuming that your 'char' is 8 bits).

Should print
12.34.56.78

(although you should consider %u format because it's the decimal you
need in a dot notation, not hex)

Victor
Jul 22 '05 #5

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

Similar topics

5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
11
by: Vinod | last post by:
Hi, I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005) I got a problem when I cast a double value to unsigned int. Problem is I couldn’t get the proper value after...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.