M mikimoto Member Licensed User Longtime User Oct 10, 2013 #1 Good morning, i am new to B4A and already with a Problem. I can not find out how Byte Arrays are working, may someone can help ? I have to convert the parity and find the checksum. in vb.net i use this function: Private Function AddParity(ByVal _in As String) As Byte() Dim item As Byte = &HFF Dim p_par As Byte = &HD Dim list As New List(Of Byte) Dim encoding As New ASCIIEncoding Dim num2 As Byte For Each num2 In encoding.GetBytes((_in & Chr(10) & Chr(10) & Chr(10))) item = CByte((item Xor num2)) list.Add(num2) Next item = CByte((item Xor p_par)) list.Add(p_par) list.Add(item) Return list.ToArray End Function wich is working fine. in B4A i am struggeling with the this: Sub AddParity(Inn As String) As Byte() Dim bconv As ByteConverter Dim item As Byte item =0xFF ' &H7F Dim p_par As Byte = 0x0d Dim List1() As Byte Dim encodeing() As Byte encodeing = (Inn & Chr(10) & Chr(10) & Chr(10)).getbytes("ASCII") 'Dim num2 As Byte For Each num2 As Byte In encodeing item = Bit.Xor(item, num2) List1.Add(num2) Next item = Bit.Xor( item , p_par) List1.Add(p_par) List1.Add(item) Return List1 End Sub the result from this function should send data to a Serial device by astreams.Write2(buffer,0,buffer.Length ) Thanks for help.
Good morning, i am new to B4A and already with a Problem. I can not find out how Byte Arrays are working, may someone can help ? I have to convert the parity and find the checksum. in vb.net i use this function: Private Function AddParity(ByVal _in As String) As Byte() Dim item As Byte = &HFF Dim p_par As Byte = &HD Dim list As New List(Of Byte) Dim encoding As New ASCIIEncoding Dim num2 As Byte For Each num2 In encoding.GetBytes((_in & Chr(10) & Chr(10) & Chr(10))) item = CByte((item Xor num2)) list.Add(num2) Next item = CByte((item Xor p_par)) list.Add(p_par) list.Add(item) Return list.ToArray End Function wich is working fine. in B4A i am struggeling with the this: Sub AddParity(Inn As String) As Byte() Dim bconv As ByteConverter Dim item As Byte item =0xFF ' &H7F Dim p_par As Byte = 0x0d Dim List1() As Byte Dim encodeing() As Byte encodeing = (Inn & Chr(10) & Chr(10) & Chr(10)).getbytes("ASCII") 'Dim num2 As Byte For Each num2 As Byte In encodeing item = Bit.Xor(item, num2) List1.Add(num2) Next item = Bit.Xor( item , p_par) List1.Add(p_par) List1.Add(item) Return List1 End Sub the result from this function should send data to a Serial device by astreams.Write2(buffer,0,buffer.Length ) Thanks for help.
M mikimoto Member Licensed User Longtime User Oct 10, 2013 #2 strange behavior ? when i read the buffer values have changed ?? Dim buffer() As Byte = Array As Byte (108,68,116,116,10,10,10,13,208) the last Byte 208 converts to -48 ? Why ?? Upvote 0
strange behavior ? when i read the buffer values have changed ?? Dim buffer() As Byte = Array As Byte (108,68,116,116,10,10,10,13,208) the last Byte 208 converts to -48 ? Why ??
KitCarlson Active Member Licensed User Longtime User Oct 10, 2013 #3 I think the byte type is signed, not unsigned. It is because that is what is used in java. I think an int, might be used since the positive range goes to 32,767, and the byte as positive only to 127. I have not tried myself. Upvote 0
I think the byte type is signed, not unsigned. It is because that is what is used in java. I think an int, might be used since the positive range goes to 32,767, and the byte as positive only to 127. I have not tried myself.
W wl Well-Known Member Licensed User Longtime User Oct 10, 2013 #4 Yes, bytes are signed which is often bit annoying. Please look at how to deal with this at: http://www.b4x.com/android/forum/threads/signed-bytes-and-usb.15220/#post-86475 Upvote 0
Yes, bytes are signed which is often bit annoying. Please look at how to deal with this at: http://www.b4x.com/android/forum/threads/signed-bytes-and-usb.15220/#post-86475