Binary to String?

Hello all,

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 :slight_smile:

Try this…

  ' ----- 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.

…or

Dim RemotePeerIDString As String = Str(RemotePeerNodeIDMB.UInt8Value(0))

if you want it as a string value.

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))

Can you give us an example of the raw values vs what you want it to look like after the “conversion”?

I receive a 2 byte binary message of 0x09C4 for example and I want to turn it into preferably String(2500) for example. Thanks guys for the help!

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)

Ha :slight_smile: Sorry 4 bytes :slight_smile: Thanks guys Ill revisit this again with your help. Thanks!

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 :slight_smile: 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)

Again, take out New. You’re creating an initial 2 byte MemoryBlock for no reason.

Change this line

Dim RemotePeerNodeIDMB As New MemoryBlock(2)

to

Dim RemotePeerNodeIDMB As MemoryBlock

There’s no point creating a memoryblock, just to throw it away, which is what this line does.

RemotePeerNodeIDMB = InputQueue(i).MidB(5,2)

It disposes of the object, if any, pointed to by RemotePeerNodeIDMB, and creates a new one, thanks to the automatic string to memoryblock conversion.

Ah thanks :slight_smile: Sorry I missed that after you explained it :slight_smile: Thanks!