Link to home
Start Free TrialLog in
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?
Avatar of rockiroads
rockiroads
Flag of United States of America image

Would CByte help?
There is also CType
Avatar of craigdev
craigdev

ASKER

not sure?
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
SOLUTION
Avatar of vadim63
vadim63
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dont know if u can cheat and use VAL to remove leading zeros
e,g.

VAL(CSTR(00110000))

or

CDBL(00110000)

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, it's good for numbers between 0 and 127
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, does it work?
thanks for all the help
got it working by sending Chr(iAddress + 128)