0

I have some Python 3 code that pulls info from an array of bytes using masking but I need to do it in a VB.NET program. This is the Python.

modulation_indicies = ['Unknown','Normal','High','Low']
phs_noise = int.from_bytes(block_data[0x1E:0x20],byteorder='little')  & 0x01FFF
mod_index = modulation_indicies[(int.from_bytes(block_data[0x1E:0x20],byteorder='little')  & 0x0C000) >> 14]

where block_data is an array of bytes. My system is little_endian. How would this be coded in VB.NET? I've tried several things from different forums but I can't get it.

IanOnHill
  • 1
  • 1
  • We need to see your attempt to write the appropriate VB code. For all we know, you're 99% of the way there so there's no point our rewriting everything. If you spend some time in the Help Center to learn how this site works, you'll see that a proper question contains a FULL and CLEAR explanation of the problem, which includes EXACTLY what you're trying to do, how you're trying to do it and what happens when you try. That means the code you have and any error messages or aberrant behaviour it produces. – jmcilhinney Jan 22 '20 at 22:43

3 Answers3

0
Enum Modulation_indicies
    Unknown
    Normal
    High
    Low
End Enum

Sub Main()

    phs_noise = BitConverter.ToInt32(you_write_this, 0) And &H1FFF
    mod_index = Modulation_indicies(phs_noise And &HC000 >> 14)

End Sub
0

Ok, let's add some words pertaining to slicing.

block_data[0x1E:0x20]

is a typical slicing of array. It returns a slice of block_data from element 0x1E (14dec) to element 0x20-1 (15dec). We dont' know the type of block_data, though I assume will be bytes. We'll make it in VB.net for any type of array.

I wrote an extension of Array type called slice. In Visual Studio create a new solution for a library. The following can be compilaed in .net framework and .net core 3.1. Add a new module as follows:

Imports System.Runtime.CompilerServices

Public Module SliceExtension

    <Extension()>
    Public Function Slice(Of T)(brr() As T, Optional inizio As Integer = 0, Optional fine As Integer = 0) As T()

        Dim Len As Integer = brr.Length
        Dim returnArray() As T = {}

        If inizio < 0 Then
            inizio = Len + inizio
            If fine = 0 Then
                fine = Len
            ElseIf fine < 0 Then
                fine = Len + fine
            End If
        Else
            inizio = idx(len:=Len, pos:=inizio)
            fine = idx(Len, fine, Len)
        End If


        Dim elemento As Integer = 0
        While (inizio < fine)
            Array.Resize(returnArray, returnArray.Length + 1)
            returnArray(elemento) = brr(inizio)
            elemento += 1
            inizio += 1
        End While

        Return returnArray

    End Function

    Public Function idx(ByVal len As Integer, pos As Integer, Optional termine As Integer = 0) As Integer

        If IsNothing(pos) Then
            pos = termine Or 0
        ElseIf (pos < 0) Then
            pos = Math.Max(len + pos, 0)
        ElseIf pos = 0 And termine < 0 Then
            pos = len
        Else
            pos = Math.Min(pos, len)
        End If
        Return pos

    End Function

End Module

Compile. It will create a dll in .bin folder of the project path. In the same solution create either a desktop or console application to test the extension. Add a reference to the newly created dll. Add a Module and paste as follows:

Imports SliceClass.SliceExtension
Module Program
    Sub Main()
        Dim arr() As String = {"10", "20", "30", "40", "50", "60", "70"}
        Dim retarr() As Object = arr.Slice(-3, -1)
        Console.WriteLine(retarr.ToString)
    End Sub
End Module

Usage: Array.Slice(start, stop) Please see here for more explanation. Please note the extension does not support step.

0

Working from the answers the code is now:

Dim phs_noise As Int16
Dim mod_index As String
Dim ModulationIndicies = New String() {"U", "N", "H", "L"}
Dim PhaseNoise(1) As Byte
PhaseNoise(0) = Block_Data(12)
PhaseNoise(1) = Block_Data(11)
phs_noise = (BitConverter.ToInt16(PhaseNoise, 0) And &H1FFF) / 100
mod_index = ModulationIndicies(phs_noise And &HC000 >> 14)

and it provides values that are OK.

IanOnHill
  • 1
  • 1