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?
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?
ASKER
not sure?
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.ICon tainer
'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.Butto n
Friend WithEvents tbInput As System.Windows.Forms.TextB ox
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.lblOutput = New System.Windows.Forms.Label
Me.btnConvert = New System.Windows.Forms.Butto n
Me.tbInput = New System.Windows.Forms.TextB ox
Me.SuspendLayout()
'
'lblOutput
'
Me.lblOutput.BorderStyle = System.Windows.Forms.Borde rStyle.Fix ed3D
Me.lblOutput.Location = New System.Drawing.Point(240, 40)
Me.lblOutput.Name = "lblOutput"
Me.lblOutput.TabIndex = 0
Me.lblOutput.TextAlign = System.Drawing.ContentAlig nment.Midd leLeft
'
'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.btnConv ert)
Me.Controls.Add(Me.lblOutp ut)
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
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.ICon
'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.Butto
Friend WithEvents tbInput As System.Windows.Forms.TextB
<System.Diagnostics.Debugg
Me.lblOutput = New System.Windows.Forms.Label
Me.btnConvert = New System.Windows.Forms.Butto
Me.tbInput = New System.Windows.Forms.TextB
Me.SuspendLayout()
'
'lblOutput
'
Me.lblOutput.BorderStyle = System.Windows.Forms.Borde
Me.lblOutput.Location = New System.Drawing.Point(240, 40)
Me.lblOutput.Name = "lblOutput"
Me.lblOutput.TabIndex = 0
Me.lblOutput.TextAlign = System.Drawing.ContentAlig
'
'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.btnConv
Me.Controls.Add(Me.lblOutp
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
dont know if u can cheat and use VAL to remove leading zeros
e,g.
VAL(CSTR(00110000))
or
CDBL(00110000)
e,g.
VAL(CSTR(00110000))
or
CDBL(00110000)
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 --> ?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually, it's good for numbers between 0 and 127
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So, does it work?
ASKER
thanks for all the help
got it working by sending Chr(iAddress + 128)
got it working by sending Chr(iAddress + 128)
There is also CType