1

I have a textbox that captures strings like 0XA5, 0X2E, 0X34 and so on.

I want to convert it into a hexadecimal value like &HA5 etc and send it to a serial port (which accepts only hexadecimal numbers in the form of '&H__') using VB.

I have looked online and I am very confused. Any sort of help would be appreciated.

Rahul
  • 903
  • 1
  • 10
  • 27
  • To convert the data to numbers, you can split it on ",", trim the resulting array elements, and use the [Convert.ToInt32 Method (String, Int32)](https://msdn.microsoft.com/en-us/library/1k20k614(v=vs.110).aspx): *If fromBase is 16, you can prefix the number specified by the value parameter with "0x" or "0X".* – Andrew Morton Jul 13 '17 at 14:20
  • @AndrewMorton The serial port accepts input in the form of hexadecimal numbers and not integers. – Rahul Jul 13 '17 at 14:28
  • Possible duplicate of [How do you convert a byte array to a hexadecimal string, and vice versa?](https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa) – A Friend Jul 13 '17 at 14:32
  • @Rahul The number base makes no difference to the value of the number - the only difference is how it is displayed. You might find Convert.ToByte more convenient. – Andrew Morton Jul 13 '17 at 14:36
  • This is confusing. If the data is sent by number then it shouldn't care about the format. Seems like you need to send the data as string, in that case just replace 0X to &H. – the_lotus Jul 13 '17 at 14:47
  • Could perhaps show us how you send values _without_ a text box? `The serial port accepts input in the form of hexadecimal numbers and not integers` is _**extremely vague**_ as we don't know _in what form_ it accepts the data. Hexadecimal numbers are just another way of representing normal integers in Visual Basic. – Visual Vincent Jul 13 '17 at 14:50
  • The tool to which the data is being sent, accepts the data in the form "&H__". That is why I cannot pass integers. – Rahul Jul 13 '17 at 15:15
  • So in other words you just need to send an ordinary string? Then djv's answer does what you require. – Visual Vincent Jul 13 '17 at 15:21
  • Yes, But there are some calculation related to the hex values, which ill have to carry out.So, I cannot simply use replace. – Rahul Jul 13 '17 at 15:26
  • @Rahul what calculation? you didn't specify that in your question. You only said you need to send the data from the textbox. – the_lotus Jul 13 '17 at 15:39
  • Convert to Integer (A Friend's answer) -> Perform calculations with Integer -> Convert Integer to HEX string -> Replace prefix (djv's answer) -> Send data -> Done. – Visual Vincent Jul 13 '17 at 15:57
  • @VisualVincent Yep that's my plan however I don't know whose answer to accept now :S – Rahul Jul 13 '17 at 18:51
  • I forgot to upvote them myself. :D – Visual Vincent Jul 13 '17 at 20:43
  • I think in this case, maybe use a combination of each answer and a little of your own magic to answer the question yourself! :) – A Friend Jul 14 '17 at 08:13

2 Answers2

1

Converts the TextBox value to an integer of base 16 (hex)

Dim hexValueAsInteger = Convert.ToInt32(YourTxtBox.Text, 16)
A Friend
  • 2,750
  • 2
  • 14
  • 23
  • Hey, I want to send the textbox value as a hexadecimal value, not an integer. So, 0XA5 would be &HA5. Is there any function for this? I am also performing some operations on decimals and then converting them into hexadecimal. So, 165 would be &HA5(just an example). – Rahul Jul 13 '17 at 14:23
  • You want to be using `Byte`s then – A Friend Jul 13 '17 at 14:24
  • Could you tell me how? – Rahul Jul 13 '17 at 14:25
  • I've marked as potential duplicate. If you convert the answer to VB, all you'll need to do is trim the leading "0x" – A Friend Jul 13 '17 at 14:33
  • @AFriend If you check the documentation I linked to, you will discover that is not necessary. – Andrew Morton Jul 13 '17 at 14:37
  • In the dupe target it is. But the documentation you linked is essentially the content of my answer, yes. – A Friend Jul 13 '17 at 14:48
1

Since the serial port accepts "only hexadecimal numbers in the form of '&H__'", it would be fairly simple to use String.Replace

Dim input = "0XA5" ' simulate TextBox input
Dim output = Input.Replace("0X", "&H")

and send output to the serial port.

To check the resulting value,

Console.WriteLine("Input: {0}, Output {1}", input, output)

yields

Input: 0XA5, Output &HA5

djv
  • 15,168
  • 7
  • 48
  • 72