Avatar of craigdev
craigdev
 asked on

byte conversion question

I need to write a function to convert a one or two digit number into a single byte starting with 1. Can someone please explain what I need to do?

Im doing serial port communication with an external device. Basically I want to send the address command "12" to the device, or in byte form starting with a 1 it would be 10001100. In the notes for the protocol it says the address should be "1 byte, 0-127 must add 128 to indicate as address".

Just wondering how I should calculate the bytes to send?
Visual Basic.NET

Avatar of undefined
Last Comment
craigdev

8/22/2022 - Mon
rockiroads

Would CByte help?
There is also CType
craigdev

ASKER
not sure?
craigdev

ASKER
This is what I have so far, just for testing.
When I enter "12" in the text box it displays 00110000
But I need it to display 10001100


'code:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lblOutput As System.Windows.Forms.Label
    Friend WithEvents btnConvert As System.Windows.Forms.Button
    Friend WithEvents tbInput As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lblOutput = New System.Windows.Forms.Label
        Me.btnConvert = New System.Windows.Forms.Button
        Me.tbInput = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'lblOutput
        '
        Me.lblOutput.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblOutput.Location = New System.Drawing.Point(240, 40)
        Me.lblOutput.Name = "lblOutput"
        Me.lblOutput.TabIndex = 0
        Me.lblOutput.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'btnConvert
        '
        Me.btnConvert.Location = New System.Drawing.Point(152, 40)
        Me.btnConvert.Name = "btnConvert"
        Me.btnConvert.TabIndex = 1
        Me.btnConvert.Text = "Convert"
        '
        'tbInput
        '
        Me.tbInput.Location = New System.Drawing.Point(48, 40)
        Me.tbInput.Name = "tbInput"
        Me.tbInput.TabIndex = 2
        Me.tbInput.Text = "12"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(392, 118)
        Me.Controls.Add(Me.tbInput)
        Me.Controls.Add(Me.btnConvert)
        Me.Controls.Add(Me.lblOutput)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Dim bytes() As Byte = {Me.tbInput.Text}
        Dim ba As New BitArray(bytes)
        For Each bit As Boolean In ba
            Select Case bit
                Case True
                    Me.lblOutput.Text += "1"
                Case False
                    Me.lblOutput.Text += "0"
            End Select
        Next
    End Sub
End Class
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
vadim63

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
rockiroads

dont know if u can cheat and use VAL to remove leading zeros
e,g.

VAL(CSTR(00110000))

or

CDBL(00110000)

craigdev

ASKER
vadim63 thats awesome, thanks

So it looks like adding 128 simply shifts the bits to the right and places a new 1 infront :)

Is there a way to convert it back at the same time? so taking 12 --> 00110000 --> 10011000 --> ?

ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
vadim63

Actually, it's good for numbers between 0 and 127
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
vadim63

So, does it work?
craigdev

ASKER
thanks for all the help
got it working by sending Chr(iAddress + 128)