Thread

sdk.aw_address conversion code (Sdk)

sdk.aw_address conversion code // Sdk

1  |  

ep0ch

Feb 25, 2004, 12:02am
Since the output from sdk.aw_address(session) is given in Network Byte Order
(ie, junk) I coded a conversion code snippit. It will take "389473245" and
make it into a pretty "65.65.65.65".

Public Declare Function inet_ntoa Lib "wsock32.dll" (ByVal addr As Long) As
Long

Public Declare Function lstrcpyA Lib "kernel32" (ByVal RetVal As String,
ByVal Ptr As Long) As Long

Public Declare Function lstrlenA Lib "kernel32" (lpString As Any) As Long

Public Function GetInetStrFromPtr(Address As Long) As String

GetInetStrFromPtr = GetStrFromPtrA(inet_ntoa(Address))

End Function

Public Function GetStrFromPtrA(ByVal lpszA As Long) As String

GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)

End Function

Usage:
sdk.aw_address(session)
var = GetInetStrFromPtr(sdk.aw_int(AW_AVATAR_ADDRESS))

Enjoy! Any bugs or anything just let me know. Also theres a function to
convert TO the junk if you really need it.

-Ep0ch

mrgrimm

Feb 25, 2004, 6:15am
Or you can just use:

ip = sdk.long2ip (sdk.aw_int (AW_AVATAR_ADDRESS))
addr = sdk.ip2long (ip)

There's also long2date and date2long to convert timestamps

-MrGrimm

[View Quote]

bowen

Feb 25, 2004, 7:05am
I'll give you brownie points if you can write similar code in C or C++
and not use a prebuilt library to do it for you (the actual conversion
code ;)).

strike rapier

Feb 25, 2004, 7:03pm
Of course they return invalid instance unless your logged in..

- MR

[View Quote]

strike rapier

Feb 25, 2004, 7:17pm
char* Long2IP(long wIP, char* lpTemp)
{
/* our variables we use for each byte */
unsigned char a1, a2, a3, a4 = 0;
/* use bitwise shifting */
a1 = (unsigned char)((wIP) >> 32);
a2 = (unsigned char)((wIP) >> 8);
a3 = (unsigned char)((wIP) >> 16);
a4 = (unsigned char)((wIP) >> 24);
/* display as the byte order */
sprintf( lpTemp, "%i.%i.%i.%i", a1, a2, a3, a4);
/* return the string */
return lpTemp;
}

The first byte confused me a bit, needed to do a bit of research but it
matches the SDK, and no lib needed (well... C Runtime... hehehe) Do I get
brownie points?

- Mark R

bowen

Feb 25, 2004, 7:18pm
[View Quote] You sure do.

strike rapier

Feb 25, 2004, 9:26pm
[View Quote] Im not so sure it would...
> i2hex.exe 389473245
> result: 221.227.54.23

ep0ch

Feb 25, 2004, 11:18pm
It was an example. I never calculated it...

-Ep0ch

[View Quote]

ep0ch

Feb 25, 2004, 11:22pm
As aw doesnt see fit to document all the functions/events/attributes
whatever thanks. If only they had documented it I would have saved half an
hour worth of coding...

-Ep0ch

[View Quote]

strike rapier

Feb 25, 2004, 11:31pm
Doh, you muppet.

- Mark R

[View Quote]

ep0ch

Feb 26, 2004, 12:43am
*Turns green and hobbles around the screen*

-Ep0ch

[View Quote]

andras

Feb 26, 2004, 8:13am
[View Quote] > char* Long2IP(long wIP, char* lpTemp)
> {
> /* our variables we use for each byte */
> unsigned char a1, a2, a3, a4 = 0;
> /* use bitwise shifting */
> a1 = (unsigned char)((wIP) >> 32);
> a2 = (unsigned char)((wIP) >> 8);
> a3 = (unsigned char)((wIP) >> 16);
> a4 = (unsigned char)((wIP) >> 24);
> /* display as the byte order */
> sprintf( lpTemp, "%i.%i.%i.%i", a1, a2, a3, a4);
> /* return the string */
> return lpTemp;
> }
>
> The first byte confused me a bit, needed to do a bit of research but it
> matches the SDK, and no lib needed (well... C Runtime... hehehe) Do I get
> brownie points?
>
> - Mark R
>
>

No - you don't get ANY brownie points for this dirty and unacceptable solution!
It is a side-effect of the compilers which do (some!) rotate instead of shifting with the >> operator. Not to mention if the long is interpreted as 64 bits (some compilers do) when your routine fails .

Proper way is:
a1 = (unsigned char) wIP;
or be more precise:
a1=wIP&0xff;

--
Andras
"It's MY computer" (tm Steve Gibson)

bowen

Feb 26, 2004, 3:42pm
[View Quote] [View Quote] Didn't have to be perfect. :) But I like yours better (the person with
the shorter code always gets more brownie points).

strike rapier

Feb 26, 2004, 5:02pm
Well in that case their operators are screwed up aint they :) Its the coders
job to check that any code that they will use from other people is
compatable with the data that they are putting into it. If people are just
going to copy the function without understanding what the hell it does then
its entirely their own fault.

PS Bowen: The code is the same length... he only put one bit.

- Mark
You just HAD to have a go didnt you?

[View Quote]

andras

Feb 26, 2004, 5:37pm
[View Quote] > Well in that case their operators are screwed up aint they :) Its the coders
> job to check that any code that they will use from other people is
> compatable with the data that they are putting into it. If people are just
> going to copy the function without understanding what the hell it does then
> its entirely their own fault.
>

Actually not - the code you provided uses 8 bits beyond the 32 bits boundary (the >>32 means it is shifted 32 times to the right).
It is not logical and you are lucky it worked.
You wanted to change the sequence of the bytes within the dword, so be consistent please:) Don't shift the first byte if it is not necessary .

The actual translation in Borland is SAR (Shift Arithmetic right) while on a gnu compiler you'll get SHR shift logical right which will effectively turn the lowest byte to 0.

KISS:)

--
Andras
"It's MY computer" (tm Steve Gibson)

strike rapier

Feb 26, 2004, 6:56pm
Well considering I knew squat about the actual makeup till about 5 minutes
before I wrote that code (had to whip out the CCNA book) I happen to thing
it was rather good :O Even if it had a single flaw which worked for me
anyhow =P

And KISS isnt my style :)

- MR

[View Quote]

binarybud

Feb 26, 2004, 7:15pm
I don't think Andras was telling you to Keep It Simple Stupid....but rather was giving you a friendly "peck on the cheek" LOL

But, then again, I might have a warped mind too....:) LOL

Leo :)



[View Quote]

binarybud

Feb 26, 2004, 7:16pm
but then again.....I should not answer for Andras.....LOL heheheheh


[View Quote]

strike rapier

Feb 26, 2004, 7:50pm
I know what he meant, my business studies teacher / school headteacher uses
the KISS acronym with me a lot because I tend to look too deep into things
and make em overly complicated [in coding, so I can play about more :) ]

But while I would love a kiss off of Andras [haha... no] Appart from that
one line my code was perfecto, so everyone can just get over it and combine
my code and Andras' code!

- Mark R


[View Quote]

ep0ch

Feb 26, 2004, 11:49pm
Hmm...maybe people with SARS got compiled by a GNU compiler? ;)

-Ep0ch

[View Quote]

bowen

Feb 27, 2004, 1:05am
[View Quote] Nah, it's definately a Borland problem -- make sure to wear your mask.

1  |  
Awportals.com is a privately held community resource website dedicated to Active Worlds.
Copyright (c) Mark Randall 2006 - 2022. All Rights Reserved.
Awportals.com   ·   ProLibraries Live   ·   Twitter   ·   LinkedIn