.NET BitConverter Class

Started by spl, December 18, 2025, 05:51:52 AM

Previous topic - Next topic

spl

I know WB has built in Binary Buffer and other conversion functions. This is just some playing with the CLR. The script below accepts a wilarray of numbers, converts to a binary array and outputs as hex. It is confined to digits 0-255 as the conversion process bytearray = ObjectClrType("System.Byte", ObjectType("array|ui1",array)) insists on that. However, the BitConverter class can handle more than just a bit. This would involve adding an additional getbytes() method to the getstring(). Haven't been able to figure out if the conversion to bytearray would handle values greater than 255, or if the BitConverter class could just output the correct hex from an array of values without the conversion udf.
;Stan Littlefield 12/17/2025 (using WB2025C)
;Test with .NET Bit.Converter class
;===========================================================================
IntControl(73,1,0,0,0)
GoSub UDFS
nums="72, 101, 108, 108, 128, 255"
array=Arrayize(nums,",")
bytes = a2Byte( array )
Message("Byte Array Created for",nums)
ObjectClrOption("useany","System")
oBit = ObjectClrNew("System.BitConverter")
hex = StrReplace(oBit.ToString(bytes),"-"," ") ;default would be - separated
Message("Converted to Hex",hex)
Exit
;===========================================================================
:WBERRORHANDLER
geterror()
Terminate(@TRUE,"Error Encountered",errmsg)
;===========================================================================
:UDFS

#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine

#DefineFunction a2Byte(array)
    bytearray = ObjectClrType("System.Byte", ObjectType("array|ui1",array))
    Return bytearray
#EndFunction

Return
;===========================================================================
Stan - formerly stanl [ex-Pundit]

bottomleypotts

Pretty cool Stan. This is the stuff I look forward to here. Just learning for the sake of learning. If you have any ideas processing Protocol Buffers, I'm all ears!

spl

Quote from: bottomleypotts on December 18, 2025, 06:46:13 PMPretty cool Stan. This is the stuff I look forward to here. Just learning for the sake of learning. If you have any ideas processing Protocol Buffers, I'm all ears!

Really appreciate that. I'm sure Tony has more insight/tools regarding Protocol Buffers, but with more specifics I think I might help.
Stan - formerly stanl [ex-Pundit]

bottomleypotts

No specifics and no details. Just know I need to use protocol buffers in an upcoming project, and the solution I have is command line tools to decode the data. I can extract the data. But I'm happy to learn other ways.

spl

Quote from: bottomleypotts on December 19, 2025, 06:50:46 AMNo specifics and no details. Just know I need to use protocol buffers in an upcoming project, and the solution I have is command line tools to decode the data. I can extract the data. But I'm happy to learn other ways.

Maybe start a new thread, as interesting topic. I assume you are talking about Google's ProtoBuf, .proto files and serializing or deserializing. Command line tools you have probably a good bet. Powershell has a Google.ProtoBuf module maybe callable with my StdOut udf, or WB can compile C# code, and Tony and Jim are experts at that.
Stan - formerly stanl [ex-Pundit]

bottomleypotts

I don't have anything to share, so I won't start a new topic until I do. I am reluctant to request Tony or Jim to do any work on this because the use case would be so small. I will power through command line solutions. I know you have a huge toolkit at your disposal, so you were my first pick for a general query. You also have a lot of knowledge of .net which I enjoy learning and thought there may be a solution in there. All good!

spl

Quote from: bottomleypotts on December 21, 2025, 07:09:42 PMI don't have anything to share, so I won't start a new topic until I do.

Understood. I have limited knowledge of ProtoBuf, but something like below is a good start for either compiling C# in WB, or executing PS code from WB. Having been termed from Spectrum and 'too old' to be in tech, I do a lot of free coding for some old clients and users on local PS group. If you have a scenario you would share for what I may be able to suggest with sample code, we can do that off-line, no strings, just a 'keep skills sharp'.

https://protobuf.dev/getting-started/csharptutorial/
Stan - formerly stanl [ex-Pundit]

SMF spam blocked by CleanTalk