Jump to content
Sign in to follow this  
kpu

IP To HEX

Recommended Posts

I was wonder if someone could help me on this. I saw the Post here:

http://www.autoitscript.com/forum/index.ph...200entry55200

I'm trying to convert HEX back to standard IP and can't figure out how to round it back.

Here's what I have for code:

$ip1 = InputBox("IP Address","What is the first IP in HEX?","169101054",)

;CONVERT $IP1 from HEX back to standard IP.
$n4 = Mod($ip1, 2^8) 
$n3 = Mod($ip1/2^8, 2^8) 
$n2 = Mod($ip1/2^16, 2^8)
$n1 = Mod($ip1/2^24, 2^8)

MsgBox(0,"Answer","Your IP is: " & $n1 & "."  & $n2 & "."  & $n3 & "."  & $n4 & ".",)

_FileWriteLog( "IP.log", "Your IP is: " & $n1 & "."  & $n2 & "."  & $n3 & "."  & $n4 & ".",)

What I get in the MsgBox is :10.0791932344437.20.2734680175781.70.0078125.2.

This should be 10.20.70.2. I tried using the round() , but may have been doing it incorrectly.

Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites

Larry, integer gives an error for me - what is it?

But this seems to work

/code

$n4 = Mod($ip1, 2^8)

$n3 = Mod(int($ip1/2^8), 2^8)

$n2 = Mod(int($ip1/2^16), 2^8)

$n1 = Mod(int($ip1/2^24), 2^8)

code/


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Larry, I know why, its normal.

kpu, some issues. First, your code doesn't run! You have a trailing comma in some of your function calls. Second, the IP is 10.20.70.254, not what you said.

Now, the real issue is this: Mod() works with both floats and integers. When you pass it a float, it returns a floating point number. 3 out of 4 of those calls to Mod() have a divide in them, which produces a floating point number. The solution is to do this:

$n4 = Mod($ip1, 2^8)
$n3 = Mod(Int($ip1/2^8), 2^8)
$n2 = Mod(Int($ip1/2^16), 2^8)
$n1 = Mod(Int($ip1/2^24), 2^8)

Notice the casts to Int(). This ensures no floating point number is returned.

Share this post


Link to post
Share on other sites

Valik,

Since I have seen you be so offhand to people, I am interested to know why have you simply copied my previous reply?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Valik,

Since I have seen you be so offhand to people, I am interested to know why have you simply copied my previous reply?

<{POST_SNAPBACK}>

Perhaps I'm offhand to people because they speak without using their brain, kind of like you are doing right now. Quite obviously I started my post and while working on it, you came in and posted yours which contained only the solution, not the reason, thus, it didn't take as long to write.

Isn't it ironic? I so rarely help people, and then when I do, I get somebody implying that I copy their answer as my own. If I can't win either way, I'm going to be lazy and just not even try.

Share this post


Link to post
Share on other sites

HEY!... that's my gig... get your own M.O.

Lar.

<{POST_SNAPBACK}>

Errr... sure. I'm going to be lazy, not even try, and make fun of everybody while doing so. Better?

Share this post


Link to post
Share on other sites

Perhaps I'm offhand to people because they speak without using their brain, kind of like you are doing right now.  Quite obviously I started my post and while working on it, you came in and posted yours which contained only the solution, not the reason, thus, it didn't take as long to write.

Isn't it ironic?  I so rarely help people, and then when I do, I get somebody implying that I copy their answer as my own.  If I can't win either way, I'm going to be lazy and just not even try.

<{POST_SNAPBACK}>

That's ok Valik, apology accepted.

(I assume that's the way you apologise anyway.)


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

That's ok Valik, apology accepted.

(I assume that's the way you apologise anyway.)

<{POST_SNAPBACK}>

No, I apologise quite proper when I have cause to. In this case, I don't. My post was there 3 minutes after yours. I started it like 5 minutes before you even posted yours. I did a bit of testing and reading of the helpfile to figure out why Mod() was returning floats. Like Larry, I'd forgotten/didn't know that it had such a feature so I was looking for a potential bug. I checked a few things before I finally came up with the solution. Then I wrote the post (I'd already had the reply window open, however, and your post was not there when I opened it).

Multiple people posting the same answer at the same time (roughly) isn't an uncommon thing on this forum. I saw a post the other day where 3 people replied with pretty much the same answer in the same minute.

I don't have a clue what you're on about. In all those multiple posts like that, you are the first person to ever accuse one of the late posters of "stealing" their answer. Let me give you a clue, dingus, I don't need to steal your answers. I'm not looking for some lame glory because I can answer a question. Are you? Damn sure looks like it to me.

Share this post


Link to post
Share on other sites

Valik,

I don't want to get into a fight with you and although I admit I was trying to talk to you the way I've seen you talk to other people I was probably wrong to do it. Sorry.

I have noticed that you have given a lot of useful help to this forum. I am a newbie and I'm trying to learn by reading peoples problems and trying to join in; I thought this would be a good way to learn. Sure it means I make a fool of myself like not knowing about FileGetShortCut() but that doesn't worry me.

I just thing that a lot of people like me can be be helped by people like you who are obviously more able and experienced with programming. But, people learn much faster, and enjoy it more, when they get encouragement. You have a tendency to discourage people and since you obviously want to help I think you would be more effective, and that you would be much more appreciated, if you weren't so harsh on people who are only trying no matter how stupid they look to you.

As I say, I don't want a fight so if you feel you want to write something back to tell me how evil and stupid I am then fine but I won't reply because otherwise it will go on and on..

Love and kisses, ever yours etc.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Valik,

I don't want to get into a fight with you and although I admit I was trying to talk to you the way I've seen you talk to other people I was probably wrong to do it. Sorry.

"Wrong to do it?" Debateable. "Doing it wrong?" Absolutely.

I have noticed that you have given a lot of useful help to this forum. I am a newbie and I'm trying to learn by reading peoples problems and trying to join in; I thought this would be a good way to learn. Sure it means I make a fool of myself like not knowing about FileGetShortCut() but that doesn't worry me.

There's a difference between not knowing and (See below)...

I just thing that a lot of people like me can be be helped by people like you who are obviously more able and experienced with programming.

Then why did you imply and/or accuse me of taking your answer?

But, people learn much faster, and enjoy it more, when they get encouragement. You have a tendency to discourage people and since you obviously want to help I think you would be more effective, and that you would be much more appreciated, if you weren't so harsh on people who are only trying no matter how stupid they look to you.

There are two groups I hit the hardest. Those who really aren't trying, or those who are trying, but are too stupid to get it. If somebody can't sing, I point that out; if somebody can't drive, I point that out; if somebody can't comprehend simple tasks and would die if important body functions weren't automatic, I point that out.

As I say, I don't want a fight so if you feel you want to write something back to tell me how evil and stupid I am then fine but I won't reply because otherwise it will go on and on..

Love and kisses, ever yours etc.

Meh, "evil" is used out of context.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...