Can anyone pls send me a link where the method of converting from Hexadecimal to Decimal is explained? I am not understanding why the "Short" type variable is showing a negative sign in the below example...the literal seems to be the same in both cases. I understand UShort will not take signed values but then where is the negative sign even coming from ...the literals are exactly same in both cases. Is it because I am not understanding the rules of conversion from Hexadecimal to Decimal or is it something else?
Module Module1
Sub Main()
Dim counter As Short = &H8000S 'Output: -32768
Dim flags As UShort = &H8000US 'Output: 32768
Console.WriteLine("O/P: {0} {1}", counter, flags)
Console.ReadLine()
End Sub
End Module
Related
Requesting your help to understand the concept of widening better!
I came across the following statement w.r.t 'Widening Conversion' in VB.Net. From the msdn documentation on the topic I found the following: Widening conversions preserve the source value but can change its representation. This occurs if you convert from an integral type to Decimal, or from Char to String. The link to the page is found below:
https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/widening-and-narrowing-conversions
My Question is as follows: I wrote the following code to understand the meaning of the statement "...preserve the source value but can change its representation". But I see no difference in the output when I print the integer or the decimal. Then what does the phrase mean....what is the meaning of "...can change its representation"?
Module Module1
Sub Main()
Dim i As Integer = 5
Dim d As Decimal = i 'widening
Console.WriteLine(i)
Console.WriteLine(d)
'Both prints "5"...no difference in representation
Console.ReadLine()
End Sub
End Module
Can someone also please give an example to demonstrate how the representation changes when we convert a char value to a string?
It means the internal presentation of the number (in your case). Try to convert, say, Single to Double:
Sub Main(args As String())
Dim sng As Single = 1.23E+32
Dim dbl As Double = sng
Console.WriteLine($"Single: {sng}")
Console.WriteLine($"Double: {dbl}")
End Sub
' Output:
' Single: 1,23E+32
' Double: 1,2300000079302825E+32
I need help in converting an engineering decimal value (floating point) into its hexadecimal representation in VBA... The code below takes the integer part only and not the decimal. I would like to have the floating point represented in hex.
Sub float2hex()
'Declaring variables
hexsimvalue As String, txdata As Double
txdata = 100.25
hexsimvalue = Hex(txdata)
End Sub
Thank you in advance.
It appears that any bitwise operator with a negative number won't happen. Even something as simple
Dim b As SByte = CSByte(-128 And &HFF)
But the computer returns "Constant expression not representable in type 'SByte'" Why would that not work? Just to be safe, I tried to surround it with a conversion. SByte as -128 is 1000 0000 and &HFF is 1111 1111. Line them up and
1000 0000
1111 1111 And
1000 0000
Which is still -128 and legal for SByte. What am I missing?
By default, VB has integer overflow checking enabled. The compiler can see that the result of (-128 and &HFF) would overflow the signed byte so it gives the compiler error: Constant expression not representable in type 'SByte'.
The code below will compile, but will generate a System.OverflowException at runtime since the result (128) is outside the range for a signed byte (with overflow checking turned on):
Public Shared Sub Main()
Dim i As Integer = -128 And &HFF
Dim b As SByte = CSByte(i)
Console.WriteLine(b)
End Sub
If you go into your project's properties, and look under Advanced Compile Options, you will see an option to "Remove integer overflow checks". If you check this value the code will compile and the result in b will be -128. Removing integer overflow checks can result in unexpected values in overflow conditions, so you will have to watch out for those.
You can also use a Byte variable and the result in b will be 128:
Public Shared Sub Main()
Dim b As Byte = CByte(-128 And &HFF)
Console.WriteLine(b)
End Sub
I'm working with VB.NET and I need to send and receive bytes of data over serial. This is all well and good until I need to send something like 173. I'm using a Subroutine to send a byte that takes an input as an integer and just converts it to a character to print it.
Private Sub PrintByte(ByVal input As Integer)
serialPort.Write(Chr(input))
End Sub
If i try
PrintByte(173)
Or really anything above 127 it sends 63. I thought that was a bit odd, so i looked up the ASCII table and it appears 63 corresponds to the ? character. So i think what is happening is VB.NET is trying to encode that number into a character that it does not recognize so it just prints a ?.
What encoding should I use, and how would I implement the encoding change?
The issue here is the SerialPort.Encoding property. Which defaults to Encoding.ASCII, an encoding that cannot handle a value over 127. You need to implement a method named PrintByte by actually sending a byte, not a character:
Private Sub PrintByte(ByVal value As Integer)
Dim bytes() As Byte = {CByte(value)}
serialPort.Write(bytes, 0, bytes.Length)
End Sub
Im having trouble working out making a converter for mutliple currencys using multiple subs. I keep receiving an error saying that number is a friend , and therefore cannot be used in the jap conversion . can anyone help ? thank you in advance
Option Explicit On
'Option Strict On
Imports System
Module Yahtzed
Sub CANtoUSD()
Dim Number , USDConversion as Decimal
Number = Console.Readline
USDConversion =( Number * 1.0141)
Console.Writeline(USDConversion)
End Sub
Sub CANtoJAP()
Dim Number, JAPConversion as Decimal
Number = Console.Readline
JAPConversion =( Number * 79.9392)
Console.Writeline(JAPConversion)
End Sub
Sub Main()
Console.Writeline("Enter the CAN amount: ")
CANtoUSD()
CANtoJAP()
End Sub
End Module
Not a direct answer, but this requires more space than would work in a comment.
You have a fundamental design error in your code. You really want to structure it more like this:
Function CANtoUSD(Number As Decimal) As Decimal
Dim USDConversion as Decimal = 1.0141
Return USDConversion * Number
End Function
Function CANtoJAP(Number As Decimal) As Decimal
Dim JAPConversion as Decimal = 79.9392
Return JAPConversion * Number
End Function
Sub Main()
Console.Writeline("Enter the CAN amount: ")
Dim input As Decimal = Console.ReadLine()
Console.WriteLine(CANtoUSD(input))
Console.WriteLine(CANtoJAP(input))
End Sub
You don't want to mix responsibilities for you methods. The input/output should be strictly separated from the code that manipulates the data. If nothing else, this makes it easier to test that your specific conversion methods work exactly like they are supposed to, and could not be the source of your bug.
Later on, you'll learn how to also ahave a single method that accepts a key value for both source and destination types, and does a table lookup to convert any currency to any other by knowing the conversion factor to a common currency.