Vb 2003 Framework 1.1

C

cmdolcet69

Public Value As Integer = 4096
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim bytes As Byte() = BitConverter.GetBytes(Value)
End Sub



This code will create a aan array of 4 bytes {lenght=4}
(0) = 0
(1) =16

How can i know convert this into a Hex value but still keep the (2)
seperate bytes
 
J

Jack Jackson

Public Value As Integer = 4096
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim bytes As Byte() = BitConverter.GetBytes(Value)
End Sub



This code will create a aan array of 4 bytes {lenght=4}
(0) = 0
(1) =16

How can i know convert this into a Hex value but still keep the (2)
seperate bytes

Convert what to hex? Hex has no meaning except when displaying a
value or typing in a value.

What do you want to do?
 
A

Armin Zingler

cmdolcet69 said:
Public Value As Integer = 4096
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim bytes As Byte() = BitConverter.GetBytes(Value)
End Sub



This code will create a aan array of 4 bytes {lenght=4}
(0) = 0
(1) =16

How can i know convert this into a Hex value but still keep the (2)
seperate bytes


This way?

Dim bytes As Byte()
Dim s As String

bytes = BitConverter.GetBytes(4096)

With New System.Text.StringBuilder
For i As Integer = bytes.Length - 1 To 0 Step -1
.Append(bytes(i).ToString("X2"))
Next
s = .ToString
End With

MsgBox(s)


Armin
 
C

cmdolcet69

This way?

Dim bytes As Byte()
Dim s As String

bytes = BitConverter.GetBytes(4096)

With New System.Text.StringBuilder
For i As Integer = bytes.Length - 1 To 0 Step -1
.Append(bytes(i).ToString("X2"))
Next
s = .ToString
End With

MsgBox(s)

Armin

I think i will use the following (2) function to help me out


Public Sub HiByte(ByVal wParam As Integer)
Dim Hibyte As Integer
Hibyte = wParam \ &H100 And &HFF&
End Sub

Public Function LoByte(ByVal wParam As Integer)
LoByte = wParam And &HFF&
End Function


How can i pass values into these function when i have an arraylist of
1271 values?
what process should i do?
 
A

Armin Zingler

cmdolcet69 said:
I think i will use the following (2) function to help me out


Public Sub HiByte(ByVal wParam As Integer)
Dim Hibyte As Integer
Hibyte = wParam \ &H100 And &HFF&
End Sub

Public Function LoByte(ByVal wParam As Integer)
LoByte = wParam And &HFF&
End Function


How can i pass values into these function when i have an arraylist
of 1271 values?
what process should i do?


I don't get what you are trying to do. You wrote you want to "convert this
into a Hex value". These procedures don't do this. Did my code help?
In addition, HiByte is a sub, not a function. Moreover, an Integer consists
of 4 Bytes, so the functions HiByte and LoByte don't make much sense.

If you want to process an arraylist containing integers, you can either use
a For Each loop or copy the arraylist into an array of integers:

for each i as integer in Mylist
'process i here
next

or:
dim values as integer()

values = directcast (mylist.toarray(gettype(integer)), integer())
for i as integer = 0 to values.length - 1
'process values(i) here
next


Armin
 
C

cmdolcet69

I don't get what you are trying to do. You wrote you want to "convert this
into a Hex value". These procedures don't do this. Did my code help?
In addition, HiByte is a sub, not a function. Moreover, an Integer consists
of 4 Bytes, so the functions HiByte and LoByte don't make much sense.

If you want to process an arraylist containing integers, you can either use
a For Each loop or copy the arraylist into an array of integers:

for each i as integer in Mylist
'process i here
next

or:
dim values as integer()

values = directcast (mylist.toarray(gettype(integer)), integer())
for i as integer = 0 to values.length - 1
'process values(i) here
next

Armin- Hide quoted text -

- Show quoted text -


Ok taking this code below when i look at my LoByte and HiByte value
the numbers dont make sense.
I pass the first arraylist(index) into this function which is 3595
integer
i should get a Lobyte as 149 and a hibyte as 53
however i get a lobyte as 11 and hibyte as 14

what im i doing wrong.


Public Function ByteDeg(ByVal _ReadingArrayList3 As ArrayList)
Dim intindex As Integer
For Each intarrayindex In _ReadingArrayList3
LoByte_Value = _ReadingArrayList3(intindex) And &HFF&
HiByte_value = _ReadingArrayList3(intindex) \ &H100 And
&HFF&
intindex = intindex + 1
'table_upload_req()
Next
End Function
 
J

Jack Jackson

Ok taking this code below when i look at my LoByte and HiByte value
the numbers dont make sense.
I pass the first arraylist(index) into this function which is 3595
integer
i should get a Lobyte as 149 and a hibyte as 53
however i get a lobyte as 11 and hibyte as 14

what im i doing wrong.

They make perfect sense.

3595 base 10 is hex E0B. The high byte is hex E which is 14 base 10
and the low byte is hex B which is 11 base 10. Therefore the answer
you are getting is correct.

If 3595 was a hex number, then the high byte would be hex 35 which is
53 base 10, and the low byte hex 95 which is 149 base 10.
 
C

cmdolcet69

They make perfect sense.

3595 base 10 is hex E0B. The high byte is hex E which is 14 base 10
and the low byte is hex B which is 11 base 10. Therefore the answer
you are getting is correct.

If 3595 was a hex number, then the high byte would be hex 35 which is
53 base 10, and the low byte hex 95 which is 149 base 10.

Jack,
I know that my 3595 is a decimal number.....the call out says it needs
to send the 3595 in 2 bytes. and i need to send the LoByte First.
so i would need to convert the 3595 into this type of format:
0x0000-0x0FF7 how can i do that?
 
A

Armin Zingler

cmdolcet69 said:
Jack,
I know that my 3595 is a decimal number.....the call out says it
needs to send the 3595 in 2 bytes. and i need to send the LoByte
First. so i would need to convert the 3595 into this type of format:
0x0000-0x0FF7 how can i do that?

This is confusing. You first said that 3595 is your integer value. If "3595"
is a hex number, then it makes sense that, as you write, the lo byte is 149
(= 0x95) and the high byte is 53 (= 0x35). Now you write that 3595 is a
decimal number.

What do you mean with "send"? Is there a method you need to call? Please
show us it's signature.

You write, you want to convert to "this type of format: 0x0000-0x0FF7".
What does this mean? This is the textual representation of a value in hex
format. It is a string. Do you want to convert to a string?

And again, an integer has 4 bytes, not 2.

To make this clear, please tell us, which varibles you use as the source and
destination and how you write the source values into the source variables.
Give us also an example of a value. Please use the "0x" prefix if it's a hex
number. Thx.



So:

- An Integer (System.Int32) is made of 4 Bytes.

- You get the single bytes into an array by using BitConverter.GetBytes

- Bytes are stored as binary digits. They don't have a Decimal nor a
Hexadecimal format.

- You can convert an Integer value into a string in different formats,
usually Decimal or Hexadecimal. Use the ToString function to do this. The
default is Decimal, otherwise use "X8" as the format string.

- You can convert a string, containing a number in hex format, into an
Integer value by using "Convert.ToInt32(TheString, 16)"



Armin
 
C

cmdolcet69

This is confusing. You first said that 3595 is your integer value. If "3595"
is a hex number, then it makes sense that, as you write, the lo byte is 149
(= 0x95) and the high byte is 53 (= 0x35). Now you write that 3595 is a
decimal number.

What do you mean with "send"? Is there a method you need to call? Please
show us it's signature.

You write, you want to convert to "this type of format: 0x0000-0x0FF7".
What does this mean? This is the textual representation of a value in hex
format. It is a string. Do you want to convert to a string?

And again, an integer has 4 bytes, not 2.

To make this clear, please tell us, which varibles you use as the source and
destination and how you write the source values into the source variables.
Give us also an example of a value. Please use the "0x" prefix if it's a hex
number. Thx.

So:

- An Integer (System.Int32) is made of 4 Bytes.

- You get the single bytes into an array by using BitConverter.GetBytes

- Bytes are stored as binary digits. They don't have a Decimal nor a
Hexadecimal format.

- You can convert an Integer value into a string in different formats,
usually Decimal or Hexadecimal. Use the ToString function to do this. The
default is Decimal, otherwise use "X8" as the format string.

- You can convert a string, containing a number in hex format, into an
Integer value by using "Convert.ToInt32(TheString, 16)"

Armin- Hide quoted text -

- Show quoted text -

Armin is feel so bad for all the confusion....heres a recap.
I store value in an arraylist these values are integers 3595 is a
decimal number.
I want to convert this so that it can be uploaded to a calibration
table on a microcontroller.

The micro has the following format.
location of the table calibration needs to be uploaded as hi_byte and
lo_byte in the following value range: 0x0000-0x0FF7 this is the
location address range of the micro.
I need to send the 3595 as lo_byte and hi_byte.

I hope this helps.
 
A

Armin Zingler

cmdolcet69 said:
- Show quoted text -

Armin is feel so bad for all the confusion...


Maybe it's just me who doesn't get it. :)

.heres a recap.
I store value in an arraylist these values are integers 3595 is a
decimal number.
I want to convert this so that it can be uploaded to a calibration
table on a microcontroller.

The micro has the following format.
location of the table calibration needs to be uploaded as hi_byte
and lo_byte in the following value range: 0x0000-0x0FF7 this is the
location address range of the micro.
I need to send the 3595 as lo_byte and hi_byte.

I hope this helps.

Yes. I would have been interested in the signature of the method, but if it
expects two single bytes, this should work:

for each value as integer in mylist
dim b as byte()
b = bitconverter.getbytes(value)

SendData(b(0), b(1)) 'lobyte, hibyte
next

Is it guaranteed that the Integer values in the list are not greater than
0xFFFF? Using the type Short (Int16) would be more appropriate.


Armin
 
C

cmdolcet69

Maybe it's just me who doesn't get it. :)




Yes. I would have been interested in the signature of the method, but if it
expects two single bytes, this should work:

for each value as integer in mylist
dim b as byte()
b = bitconverter.getbytes(value)

SendData(b(0), b(1)) 'lobyte, hibyte
next

Is it guaranteed that the Integer values in the list are not greater than
0xFFFF? Using the type Short (Int16) would be more appropriate.

Armin

following the above function i get a build error of
Publis Shared function Getbytes(value as and it list every datatype)
as byte; Value of type System.Collection.Arraylist cannot be convert
to any datatype.
Every value in the arraylist is type integer.

Why wont this work???
 
A

Armin Zingler

cmdolcet69 said:
following the above function i get a build error of
Publis Shared function Getbytes(value as and it list every datatype)
as byte; Value of type System.Collection.Arraylist cannot be convert
to any datatype.
Every value in the arraylist is type integer.


You're passing the arraylist to GetBytes? Post the code.

Why wont this work???


I don't know. Here, it works. Working example:

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

Dim mylist As New ArrayList

mylist.Add(1734)
mylist.Add(3595)

For Each value As Integer In mylist
Dim b As Byte()
b = BitConverter.GetBytes(value)

SendData(b(0), b(1)) 'lobyte, hibyte
Next
End Sub

Private Sub SendData(ByVal lobyte As Byte, ByVal hibyte As Byte)

Debug.WriteLine( _
String.Format( _
"lobyte: {0:X2}, hibyte: {1:X2}", _
lobyte, hibyte _
) _
)

End Sub


Armin
 
C

cmdolcet69

You're passing the arraylist to GetBytes? Post the code.


I don't know. Here, it works. Working example:

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

Dim mylist As New ArrayList

mylist.Add(1734)
mylist.Add(3595)

For Each value As Integer In mylist
Dim b As Byte()
b = BitConverter.GetBytes(value)

SendData(b(0), b(1)) 'lobyte, hibyte
Next
End Sub

Private Sub SendData(ByVal lobyte As Byte, ByVal hibyte As Byte)

Debug.WriteLine( _
String.Format( _
"lobyte: {0:X2}, hibyte: {1:X2}", _
lobyte, hibyte _
) _
)

End Sub

Armin- Hide quoted text -

- Show quoted text -

Armin that works, how can i show them in 2 seperate message boxes?

MessageBox.Show(String.Format("lobyte: {0:X2}", lobyte))
MessageBox.Show(String.Format("hibyte: {1:X2}", hibyte))

When i do this it returns an error
 
C

cmdolcet69

Armin that works, how can i show them in 2 seperate message boxes?

MessageBox.Show(String.Format("lobyte: {0:X2}", lobyte))
MessageBox.Show(String.Format("hibyte: {1:X2}", hibyte))

When i do this it returns an error- Hide quoted text -

- Show quoted text -

Sorry i should ahve included the error mesage. It says "Index[zero
based] must be greater than or equal then zero and less then the size
of the argument list.
It breaks on the second step messagebox
 
A

Armin Zingler

cmdolcet69 said:
Armin that works, how can i show them in 2 seperate message boxes?

MessageBox.Show(String.Format("lobyte: {0:X2}", lobyte))
MessageBox.Show(String.Format("hibyte: {1:X2}", hibyte))

When i do this it returns an error- Hide quoted text -

- Show quoted text -

Sorry i should ahve included the error mesage. It says "Index[zero
based] must be greater than or equal then zero and less then the
size of the argument list.
It breaks on the second step messagebox

Sorry for answering late...
You should understand what you are writing. ;-) What does String.Format do?
What does the "1" in the curly braces mean? It is the index of the value
passed. Press [F1], it's all explained there. The index is 0-based. So, "1"
is the 2nd value, but you only have one value with index 0, which is called
"hibyte". So, change it to "hibyte: {0:X2}"


Armin
 
A

Andrew Morton

cmdolcet69 said:
Armin that works, how can i show them in 2 seperate message boxes?

MessageBox.Show(String.Format("lobyte: {0:X2}", lobyte))
MessageBox.Show(String.Format("hibyte: {1:X2}", hibyte))

When i do this it returns an error- Hide quoted text -

- Show quoted text -

Sorry i should ahve included the error mesage. It says "Index[zero
based] must be greater than or equal then zero and less then the size
of the argument list.
It breaks on the second step messagebox

MessageBox.Show(String.Format("lobyte: {0:X2}", lobyte))
MessageBox.Show(String.Format("hibyte: {0:X2}", hibyte))

You are supplying only one item to the Format function (apart from the
string to format), and its index is zero.

You could do it with one MessageBox:
MessageBox.Show(String.Format("lobyte: {0:X2}, hibyte: {1:X2}", lobyte,
hibyte))

Andrew
 
C

cmdolcet69

Sorry i should ahve included the error mesage. It says "Index[zero
based] must be greater than or equal then zero and less then the
size of the argument list.
It breaks on the second step messagebox

Sorry for answering late...
You should understand what you are writing. ;-) What does String.Format do?
What does the "1" in the curly braces mean? It is the index of the value
passed. Press [F1], it's all explained there. The index is 0-based. So, "1"
is the 2nd value, but you only have one value with index 0, which is called
"hibyte". So, change it to "hibyte: {0:X2}"

Armin- Hide quoted text -

- Show quoted text -

Armin that work great......thanks for all your help. I have 1 more
question i need to do the same thing for my intindex values in the
arraylist
those are the value that refer to (0) 150
(1) 150
(2) 150
(3) 150
(4) 150

how can i get the (0), (1), (2), (3),..... to show the same way as in
the previous example.

MessageBox.Show(String.Format("lobyte: {0:X2}", lobyteintindex))
MessageBox.Show(String.Format("hibyte: {1:X2}", hibyteintindex))
 
A

Armin Zingler

cmdolcet69 said:
Armin that work great......thanks for all your help. I have 1 more
question i need to do the same thing for my intindex values in the
arraylist
those are the value that refer to (0) 150
(1) 150
(2) 150
(3) 150
(4) 150

how can i get the (0), (1), (2), (3),..... to show the same way as
in the previous example.

MessageBox.Show(String.Format("lobyte: {0:X2}", lobyteintindex))
MessageBox.Show(String.Format("hibyte: {1:X2}", hibyteintindex))


Sorry, I don't understand what you're trying. Could you please rephrase?


Armin
 
C

cmdolcet69

Sorry, I don't understand what you're trying. Could you please rephrase?

Armin

armin my appologies.... I have figure out that last problem i was
posting, however a funny issue poped up
in my code below how can i show me Len as 08 and my Cmd as 02. when it
displays in the messagebox it shows
up as a 2 and a single 8 i need it to show as a 02 and 08 also when i
display my pmsg it will only display 1 0 and not both zero

example of the comm1.output should be: * 08 02 97 00 00 00
* is a defined constant
08 is the len
02 is the cmd
97 is the lobyte value
00 is the hi byte value
00 is the lobyte location
00 is the hibyte location

Public Sub table_upload_req(ByVal lobyte As Byte, ByVal hibyte As
Byte, ByVal locationlobyte As Byte, ByVal locationhibyte As Byte)
Dim Len As Byte = &H8
Dim Cmd As Byte = &H2
'adc_value = &H0 ' read ADC value and put into storage var
'location_value = &H0
crc_value = 0

Dim msg_size As Byte = Len - 2
pmsg(0) = Len
pmsg(1) = Cmd

'Dim i As Integer = 0
'Do While i < intarrayindex
' single table upload request packet
'location_value += 1
'adc_value = Array_AVG(i) ' asign value array to adc_value

pmsg(2) = (String.Format("{0:X2}", lobyte))
pmsg(3) = (String.Format("{0:X2}", hibyte))
pmsg(4) = (String.Format("{0:X2}", locationlobyte))
pmsg(5) = (String.Format("{0:X2}", locationhibyte))

'crc_value = Make_Bitwise_CRC16(msg_size)
MessageBox.Show(BMS & pmsg(0) & pmsg(1) & pmsg(2) & pmsg(3) &
pmsg(4) & pmsg(5)) '& AscW(crc_value) & AscW(crc_value))
' i += 1
'Loop
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top