Efficiency Question regarding RaiseEvent in a Loop

No worries Tim :slight_smile:

Input comes in the form of Decimal. Calculations are best done in binary. Then the results go back to the user as decimal.

Im trying hard to keep it all in decimal right now :slight_smile:

This is all subnet mask calculations also …

I’m not grok’ing the difference. And where does this string representation fit in?

That is where my xojo “trying to get stuff to work” without really knowing 100% the differences is evident.

I used the string representation because that was the only way (today) I thought to convert to binary representation. I am looking at Memory blocks to replace this as you have told me many of times to use it (and I have in other apps).

I have a bad habit of getting apps to work and then to refine as I go. I am at that point for sure. :slight_smile:

I am trying to replace my string binary conversion to something like this instead.

  Dim FirstOctetDec, SecondOctetDec, ThirdOctetDec, FourthOctetDec as UInt8
  
  Dim FirstOctetMB As New MemoryBlock(1)
  Dim SecondOctetMB As New MemoryBlock(1)
  Dim ThirdOctetMB as New MemoryBlock(1)
  Dim FourthOctetMB as New MemoryBlock(1)
  
  FirstOctetMB = Inbound32BitWord.Mid(1,8)
  SecondOctetMB = Inbound32BitWord.Mid(9,8)
  ThirdOctetMB = Inbound32BitWord.Mid(17,8)
  FourthOctetMB =Inbound32BitWord.Mid(25,8)
  
  FirstOctetDec = FirstOctetMB.UInt8Value(0)
  SecondOctetDec = SecondOctetMB.UInt8Value(0)
  ThirdOctetDec = ThirdOctetMB.UInt8Value(0)
  FourthOctetDec = FourthOctetMB.UInt8Value(0)

So far I am getting a FirstOctetDec value of 48 when it’s memory block has the correct 00000001 (1) value. :slight_smile: Getting closer.

I see what you are alluding to TIm… I just need to go with memory blocks 100% and get rid of all of the ‘Bin’ string conversions… Thats killing me :slight_smile:

Your code is literally the same as this:

FirstOctetDec = Inbound32BitWord.Left( 1 ).Val
SecondOctetDec = Inbound32BitWord.Mid( 9, 1 ).Val
ThirdOctetDec = Inbound32BitWord.Mid( 17, 1 ).Val
FourthOctetDec = Inbound32BitWord.Mid( 25, 1 ).Val

They will all either be 0 or 1.

There is no advantage that I can see for you to use a MemoryBlock over 4 integer variables. The question is (what neither I nor, I think, Tim understands), why you need the binary representation at all? What are you doing with the bits you count?

BTW, creating a 1-byte MemoryBlock that gets immediately replaced with an 8-byte MemoryBlock by the implicit conversion is waste of cycles.

Yeah, you can access each individual bit in the memoryblock with a little bitwise math. And all that integer to string conversion is terribly inefficient.

Oh, and lest I seem to be ragging on you, it’s awesome that you got it working, period! The rest is just refining a little.

Kem Thanks again buddy. I am going back to the beginning replacing all string conversions with MB so thanks as I realized after I posted it wasn’t going to come close :slight_smile:

Tim thanks for all of the help also :wink: Yes it works, but until it’s efficient i can’t really share the class :slight_smile:

https://github.com/mikecotrone/Xojo_SubnetCalculatorClass.git

its working but when you lean on anything /24+ it gets slow (for now;) )

Aaaand… Don’t use 1 byte memoryblocks. Use a 4-byte MB and integers for everything else.

dim octet1, octet2, octet3, octet4 as integer
dim mb as new memoryblock(4)
mb.Int32Value(0) = theAddress
octet1 = mb.byte(0)
octet2 = mb.byte(1)
octet3 = mb.byte(2)
octet3 =  mt.byte(3)
socket.write(mb.StringValue(0, 4)

I was going with 1 byte since I only needed 1 byte per octet. :slight_smile: Thanks Tim.

Ah thanks. I see what you are doing!

Project is missing files.

SubnetCalculatorClass.xojo_binary_project I just pushed it again.

Oh Sorry I have a few images I need to include.

ok added the two images.

OK, maybe I’m not understanding the results. If I enter 192.168.1.1/255.255.0.0 in the “every range” section, shouldn’t I get 192.168.0.0 through 192.168.255.255 (give or take)?

Thanks again Guys. This is Loading the Network Address and Subnet mask perfectly into the Memory blocks. Thanks again as I understand the use of MB much more clearly having to do anything with direct byte/bit manipulation.

  // Break Down Start IP into Separate Octets in Decimal Form
  StartIP_1Dec = CDbl(NetworkIP.NthField(".",1))
  StartIP_2Dec = CDbl(NetworkIP.NthField(".",2))
  StartIP_3Dec = CDbl(NetworkIP.NthField(".",3))
  StartIP_4Dec = CDbl(NetworkIP.NthField(".",4))
  
  dim NetworkIPAddress as new memoryblock(4)
  NetworkIPAddress.LittleEndian = False
  NetworkIPAddress.byte(0) = StartIP_1Dec
  NetworkIPAddress.byte(1) = StartIP_2Dec
  NetworkIPAddress.byte(2) = StartIP_3Dec
  NetworkIPAddress.byte(3) = StartIP_4Dec

  // Break Down Subnet Mask into Separate Octets in Decimal Form
  SubnetMask1Dec = CDbl(SubnetMask.NthField(".",1))
  SubnetMask2Dec = CDbl(SubnetMask.NthField(".",2))
  SubnetMask3Dec = CDbl(SubnetMask.NthField(".",3))
  SubnetMask4Dec = CDbl(SubnetMask.NthField(".",4))
  
  Dim SubnetMaskMB as new memoryblock(4)
  SubnetMaskMB.LittleEndian = False
  SubnetMaskMB.byte(0) = SubnetMask1Dec
  SubnetMaskMB.byte(1) = SubnetMask2Dec
  SubnetMaskMB.byte(2) = SubnetMask3Dec
  SubnetMaskMB.byte(3) = SubnetMask4Dec