I have the following code that is extracting a NodeID from an inbound packet.
RemotePeerNodeID = InputQueue(i).MidB(5,2)
I am just trying to convert “RemotePeerNodeID” into either a String or Integer and I am failing. I tried a memory block but something isn’t right as I never get a string converted value. I either can get nothing or get the same binary represented in hex and not a string.
This is my latest failed attempt:
Dim RemotePeerNodeIDMB As New MemoryBlock(2)
RemotePeerNodeIDMB.LittleEndian = False
RemotePeerNodeIDMB = RemotePeerNodeID
Dim RemotePeerIDString As String = RemotePeerNodeIDMB.StringValue(0,2)
I just get the same Binary value in hex format when I look at the RemotePeerNodeID in thn IDE under Binary. I also tried DecodeHex against that string with no luck. I am missing something
' ----- test code start -----
Dim RemotePeerNodeID As String = "a1"
' ----- test code end -----
Dim RemotePeerNodeIDMB As MemoryBlock
RemotePeerNodeIDMB = DecodeHex(RemotePeerNodeID)
RemotePeerNodeIDMB.LittleEndian = False
Dim RemotePeerIDInt As Integer = RemotePeerNodeIDMB.UInt8Value(0)
I think you might have to DecodeHex the value before you store it in the MemoryBlock.
Actually, you don’t need a MemoryBlock… the following gives the same result:
' ----- test code start -----
Dim RemotePeerNodeID As String = "a1"
' ----- test code end -----
Dim RemotePeerIDInt As Integer = AscB(DecodeHex(RemotePeerNodeID))
you get two bytes (09 C4) ?
or a string (“09C4”) - which is more like 4 bytes
ANd IF all the data is relatively fixed frames for headers etc you could shove the header data into a structure then just read it from the structure instead of messing around with MidB all the time
OK, hex 09C4 is 2500 decimal. So you just want the Int16 value of those 2 bytes? I second Norman’s suggestion of using a Structure, but you can do it with a MemoryBlock as well:
dim mb as MemoryBlock
mb = InputQueue(i).MidB(5,2)
RemotePeerNodeID = mb.Int16Value(0)
Just so you can understand the under workings, your code:
Dim RemotePeerNodeIDMB As New MemoryBlock(2)
RemotePeerNodeIDMB.LittleEndian = False
RemotePeerNodeIDMB = RemotePeerNodeID
Dim RemotePeerIDString As String = RemotePeerNodeIDMB.StringValue(0,2)
is exactly the same in effect as:
Dim RemotePeerIDString As String = RemovePeerNodeID.DefineEncoding( nil )
What’s more, if you create a new MemoryBlock, then assign a string to it directly, you’ve gotten rid of the original one you created. So, for example:
dim mb as new MemoryBlock( 1 )
mb = "A string much longer than 1 byte"
// The original, 1-byte MemoryBlock was created with "New MemoryBlock( 1 )",
// then discarded in favor of a new one that was created by
// the assignment of the string.
[quote=61713:@Norman Palardy]you get two bytes (09 C4) ?
or a string (“09C4”) - which is more like 4 bytes
ANd IF all the data is relatively fixed frames for headers etc you could shove the header data into a structure then just read it from the structure instead of messing around with MidB all the time[/quote]
Sorry Norman I read your post wrong above… I am receiving 2 bytes 09 C4… I was just turning them into a string after I received the packets. I am using Structures on the frame creation outbound, but not on the inbound (my error). All I had been doing was just matching the binary hex and then responding without needing to convert the binary hex. However now I do and you guys pointed out what I was doing wrong Thank you again!
Phew thanks again guys for all of the help. Here is my final snippet.
// Convert SenderNodeID to a String from Hex notation
Dim RemotePeerNodeIDMB As New MemoryBlock(2)
RemotePeerNodeIDMB = InputQueue(i).MidB(5,2)
RemotePeerNodeIDMB.LittleEndian = False
Dim RemotePeerIDString as UInt16= RemotePeerNodeIDMB.UInt16Value(0)