Re: How to conver the 4 bytes of a string to u_long in host-byte o



The same, but for ipv6 you need to include wsipv6ok.h which redefine the
declaration ( that correct for all structures )
Arkady

"Bassam" <Bassam@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:A97792F2-B79C-4786-A613-7557D6FDCEE6@xxxxxxxxxxxxxxxx
inet_ntoa() for IPv4, so what is its analogus for IPv6??

Regards
Bassam


"Arkady Frenkel" wrote:

fill in_addr struct with four bytes of the Value from your NAS-IP-Address
and inet_ntoa() will return your needed string
Arkady

"Bassam" <Bassam@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:A07AF89E-0C08-4F35-BAEA-446821B64352@xxxxxxxxxxxxxxxx
Ok, I think I was not clear and I will elaborate in here:
I am receiving Radius attributes string on the form of
(Type-Length-Value)
where:

Type: 1 Octet
Length: 1 Octet (that represents the length of the attribute including
Type
and Length)
Value: 1 or more octets, depending on attribute Type.

However, I am trying to convert the bytes of the NAS-IP-Address
attribute
into readable format and show up on the consol. Note that the fields
are
transmitted from left to right and the fields? values of this attribute
are
as follows:

Type: NAS-IP-Address (4).
Length: 6.
Value: The Address field is four octets.

Upon receiving this attribute, I retain or store the Type in a char
variable, and the same thing for the Length field, but I store Value
field
in
a string variable. I have no problem with the first two fields, as I
can
convert them to a readable format, but my problem is how to show up the
?myValue? in a dotted format IP address (i, e: X.X.X.X).

void setAttribute(char *myAttribute)
{
int i;
char myType, myLength;
string myValue

myType = myAttribute[0];
myLength = myAttribute[0];
for (i = 2; i < myLength - 1; i++)
myValue.push_back(myAttribute[i]);

cout << ?The Type is: ? << static_cast<unsigned short>(myType) << endl;
cout << ?The Length is: ? << static_cast<unsigned short>(myLength) <<
endl;

//the following steps show the ASCII symbol, while I want the integer
value
itself
cout << ?The address: ?;
cout << static_cast<unsigned short>(myValue.at(0)) << ?:?;
cout << static_cast<unsigned short>(myValue.at(1)) << ?:?;
cout << static_cast<unsigned short>(myValue.at(2)) << ?:?;
cout << static_cast<unsigned short>(myValue.at(3));
}


Note: If you want to ask me why I don?t store the Value inside an char
array, my answer is that there is no a fixed limit to the Value field
since
it depends on the attribute type. Some of the attributes have a fixed
limit
of octets and others are indeterminist, so any size of data can be
carried
on
the attribute. The mentioned procedure is just a general one that can
deal
with any type of attributes.

Your help is much appreciated.

Regards
Bassam


"Alexander Nickolov" wrote:

Do you mean you have char pointer to binary data or a
real ANSI string? You seem to indicate the former, e.g.
it's not a string. In that case simply use a type cast:

realInteger = ntohl(*reinterpret_cast<u_long*>(myString.c_str()));

BTW, the above code also assumes the integer is in network
byte order, which I copied from your own example. I hope
that's correct, otherwise your example wouldn't work either.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Bassam" <Bassam@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:AD59A6C0-48B2-401D-BD7E-7A40242B8795@xxxxxxxxxxxxxxxx
Dear All,
I am using win32 C++ using Visual C++ 2005 express edition on
windows
xp
pr.
I got stuck on how to convert the first four bytes of a string to
u_long?

If the string is just a 4 bytes array, then it is easy to convert it
by
using the following statments:

char integerBuffer[4];
u_long realInteger;
//some code and assignment to integerBuffer
realInteger = ntohl(*reinterpret_cast<u_long *>(&integerBuffer));

Now suppose I have a string defined identifier instead of the four
bytes
array, so what can I do?

I have used the following indirect conversion and gave the expected
result,
however, I am still seeking for a more efficient solution:

char integerBuffer[4];
u_long realInteger;
string myString;

//some data received from a network and assigned to myString
integerBuffer[0] = myString.at(0);
integerBuffer[1] = myString.at(1);
integerBuffer[2] = myString.at(2);
integerBuffer[3] = myString.at(3);
realInteger = ntohl(*reinterpret_cast<u_long *>(&integerBuffer));

Any idea?

Regards
Bassam








.



Relevant Pages

  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: How to conver the 4 bytes of a string to u_long in host-byte o
    ... "Arkady Frenkel" wrote: ... and inet_ntoawill return your needed string ... 1 or more octets, ... I retain or store the Type in a char ...
    (microsoft.public.win32.programmer.networks)
  • Re: How to conver the 4 bytes of a string to u_long in host-byte o
    ... Beg you pardon Arkady, how it could accept ipv6 structur while it still ... and inet_ntoawill return your needed string ... 1 or more octets, ... I retain or store the Type in a char ...
    (microsoft.public.win32.programmer.networks)