Another OT: .Net=>WB

Started by spl, December 08, 2024, 06:50:50 AM

Previous topic - Next topic

spl

This is an aside from the previous WOL thread that Tony ended quit effectively. The focus is the code for creating a byte array from hex. Curiosity for how that could be easily accomplished in .NET lead me to an unadvertised object System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary

So I can get this far
hex = '1A:2B:3C:4D:5E:6F'
ObjectClrOption('useany','System')
cvt = ObjectClrNew('System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary')
hex = StrReplace(hex,':','')
result = cvt.Parse(hex)
Message("",result)

but there is a Value Property and in PS
$result = ([System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary]::Parse($hex).Value)

will return the byte array as text. I'm sure WB CLR can return the Value property but not sure if the return from .Parse() should be treated as a pointer or an object.



Stan - formerly stanl [ex-Pundit]

td

If you look at the documentation for SoapHexBinary, Parse returns an object of type SoapHexBinary. SoapHexBinary has a property named "Value." So proper usage would be

aBytes = Result.Value
Of course, if you used WinBatch Studio, the variable types would be self-evident in the variable watch window.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

I believe my original post specified that .Value was a property. So, if using your suggestion
hex = '1A:2B:3C:4D:5E:6F'
ObjectClrOption('useany','System')
cvt = ObjectClrNew('System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary')
hex = StrReplace(hex,':','')
result = cvt.Parse(hex)
aBytes = result.Value
Message("",aBytes)

you get the attached, ergo back to my original question as to how to decode the .Parse() object
Stan - formerly stanl [ex-Pundit]

td

The result from the Value property call is a safearray of UI1 variants. If you use WBS, you will notice that it has the variant type of "ARRAY|UI1". "ARRAY|UI1" is the COM equivalent of a byte array.

aBytes = Result.Value
Message('aBytes Variant Type', ObjectTypeGet(aBytes))

or if you think you need a string representation of the byte array for some reason
sBytes = ''
foreach byte in aBytes
   sBytes := byte
next
Message("Byte Array As String",sBytes)

Again, the Parse method returns a COM object in WinBatch, the COM automation equivalent of a .Net class object. This is true of all .Net class objects in WinBatch.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Thanks; string rep preferable and aligns with PS one-liner.
Stan - formerly stanl [ex-Pundit]

spl

Quote from: spl on December 10, 2024, 12:50:29 PMThanks; string rep preferable and aligns with PS one-liner.

And just so you know, been thinking about a generic type converter to post for 2025. Specifically:
binary to decimal      | binary to hexadecimal  | binary to ipv4
decimal to binary      | decimal to hexadecimal | decimal to ipv4
hexadecimal to binary  | hexadecimal to decimal | hexadecimal to ipv4
ipv4 to binary         | ipv4 to decimal        | ipv4 to hexadecimal

with parameters as

bd | bh | bi
db | dh | di
hb | hd | hi
ib | id | ih

so you might issue cvt = NumConvert("127.0.0.1","ib")

and have it return 01111111000000000000000000000001

There is PS that looks at a generic conversion but for WB would (in my opinion) involve CLR in addition to WB processing, and a few maps for lookup. Will give it my best shot before year end, just don't get too annoyed with my test posts.
Stan - formerly stanl [ex-Pundit]

td

If you succeed, it might become a prototype for a native WIL function that performs the same or similar tasks.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

[quote author=td link=msg=18432 date=1733953825]
If you succeed, it might become a prototype for a native WIL function that performs the same or similar tasks.
[/quote]

My first concern will be evaluating the parameter for conversion type, i.e. bd, db ...etc. Would prefer a switch or select rather than a series of if's....

select cvttype 
case "db"
   code
case cvttype
   Return no valid conversion type and exit

But according to the WB docs, switch must return an integer value. That is why I have always stayed away unless sending an integer value. Haven't tried

case (cvttype == "bd")

to return a boolean, which would make sense. An alternative would be a map for each conversion to an integer, process the map find from the parameter then switch on the value. Or maybe there is a simpler method.
 
Stan - formerly stanl [ex-Pundit]

spl

Jim contacted me and seems the generic conversion spiked his interest in yet another one of his excellent extenders. Would probably be more efficient to go that route, as I would probably kludge together an #include file mixing and matching WB with CLR.

Jim, if you pursue that I will be happy to share preliminary byte/hex array lookups I would use.
Stan - formerly stanl [ex-Pundit]