473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Convert number to hex in byte array (&H?? format)

2
Hello!

I'm running Visual Basic 2012

I'm having a certain amount of sucess controlling my serial port but now I've hit a wall.

Expand|Select|Wrap|Line Numbers
  1.  Public com3 As New SerialPort("COM3", 9600, Parity.None, 8, StopBits.One)
  2.  
  3.     Public Hexe As Integer = Hex(100)
  4.  
  5.       Public Sub New()
  6.         InitializeComponent()
  7.         For Each sp As String In My.Computer.Ports.SerialPortNames
  8.             ListBox1.Items.Add(sp)
  9.         Next
  10.         If com3.IsOpen = False Then
  11.             com3.Open()
  12.         End If
  13.  
  14.             Dim Absolut() As Byte = {1, 4, 0, 0, 0, 0, 0, Int in Hex (&H format), Int in Hex + 5 (control bit)}
  15.             com3.Write(Absolut, 0, Absolut.Length)
  16.     End Sub
  17.  
As you can see I need either the whole byte in hex or just parts of it, problem is that Hex(Int) gives it as a string instead of the &H?? format I need. I can't find any solution on MSDN.

Am I just not seeing the obvious?

Best Regards
SciCat

EDIT: The code is incomplete, I didn't want to dump a 100+ line blob, the code works in its entirety.
Feb 25 '13 #1
1 3557
SciCat
2
I have managed to solve the conversion problem using Bitshift in the byte itself using an integer:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Public com3 As New SerialPort("COM3", 9600, Parity.None, 8, StopBits.One)
  3.  
  4.     Public Sub SendCMD(Adress As Byte, Command As Byte, Type As Byte, Motor As Byte, Value As Integer)
  5.         Dim TxBuffer() As Byte
  6.         Dim i As Integer
  7.  
  8.         TxBuffer = {Adress, Command, Type, Motor, Value >> 24, Value >> 16, Value >> 8, Value & &HFF, 0}
  9.         For i = 0 To 7
  10.             TxBuffer(8) = TxBuffer(8) + TxBuffer(i)
  11.         Next
  12.         com3.Write(TxBuffer, 0, 9)
  13.  
  14.  
  15.     End Sub
  16.  
  17.  
  18.     Public Sub New()
  19.         InitializeComponent()
  20.         For Each sp As String In My.Computer.Ports.SerialPortNames
  21.             ListBox1.Items.Add(sp)
  22.         Next
  23.         If com3.IsOpen = False Then
  24.             com3.Open()
  25.         End If
  26.         SendCMD(1, 1, 0, 0, 1)
  27.         Tid.Enabled = True
  28.  
  29.     End Sub
  30.  
  31.     Private Sub Tid_Tick(sender As Object, e As EventArgs) Handles Tid.Tick
  32.         SendCMD(1, 3, 0, 0, 1)
  33.     End Sub
But when I run the code I get an arithmetic overflow, did i do something wrong?


EDIT: Everything works! Keeping it here for future reference.
Mar 2 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dave | last post by:
We are writing some code to interface to a DLL. The DLL has a callback method that returns the address of a byte buffer and the buffer length. I have defined the byte pointer as an IntPtr. ...
5
by: kevinniu | last post by:
Hi everyone, In c#, what is the fastest way(include unsafe) to convert a array of bytes(which really contains the bytes of a double array) to a arry of double. thanks
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
2
by: antonio matos | last post by:
hi people. I have to acess a database, and there are some fields that are binary. when i acess that field is read like an array of bytes. but in reality it's an array of doubles!!! how can...
2
by: Jaime Stuardo | last post by:
Hi all... I'm trying to retrieve a SQLXML query using VB.NET. When I programmed in VB 6.0, I used Stream object to accomplish this which was trivial. I cannot do the same thing in VB.NET. Here...
0
by: Jim Rasmussen | last post by:
I need some help in converting a byte array to pdf. Could someone give me an example of how in C#? Here is what I have so far, using System.IO; string sFile = "c:\testpdf.pdf"; FileStream fs =...
5
by: Star | last post by:
Hi all, Let's suppose we have this: byte buffer = new byte; buffer = 0x04; buffer = 0xF1; buffer = 0xB4;
3
by: dpsairam | last post by:
Hi, How to convert a byte array to image....i mean i have to convert a Byte array into image format.....please hellp.........i need the java code... Thanking You.
1
by: phpuser123 | last post by:
I want to convert my object to byte array and then next to an object and run a method..The codes are below: public class test_serialisation implements Serializable{ /** * */ private...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.