Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

how to convert DEC to HEX?

wat i mean here is that i wan the code being able to convert negative DEC value to negative HEX.. eg: if i keyed in -10, it will be converted to -A.. i know it has to do with the highest bit of something that determines whether it will be + or - but i jus couldnt think of wat it is.. can any1 help me out?? Thanks
[314 byte] By [duibye] at [2007-11-11 6:49:44]
# 1 Re: how to convert DEC to HEX?
Dim X As Integer
X = -10
If X < 0 Then
Debug.Print "-";
End If
Debug.Print Hex(Abs(X))
Phil Weber at 2007-11-11 17:27:56 >
# 2 Re: how to convert DEC to HEX?
erm as i m pretty new to Vb, hence i m not sure how to apply this to my program.. can u help me on this?? and also, wat does ur code means?? can u explain to me? btw, thanks for ur code :)
duibye at 2007-11-11 17:28:56 >
# 3 Re: how to convert DEC to HEX?
I suggest you begin by looking up the Hex and Abs functions in VB's online help. Then post a followup message and tell me what you think Hex(Abs(X)) does, and I'll tell you if you're right. ;-)
Phil Weber at 2007-11-11 17:29:54 >
# 4 Re: how to convert DEC to HEX?
erm.. ok, lets assume that X=-10 now, then (abs(X)) means that now X is 10 hence Hex(abs(X)) means that its gonna convert 10 to hexadecimal?? m i rite?
duibye at 2007-11-11 17:30:59 >
# 5 Re: how to convert DEC to HEX?
Private Sub Command5_Click()

Get_Distance1 = Text1.Text
If Get_Distance1 < -100 Or Get_Distance1 > 100 Then
MsgBox ("please input value between -100 to 100")
End If

Data_Distance1 = Hex(Get_Distance1)
If Len(Data_Distance1) = 1 Then
Data_Distance1 = "000" + Data_Distance1
ElseIf Len(Data_Distance1) = 2 Then
Data_Distance1 = "00" + Data_Distance1
ElseIf Len(Data_Distance1) = 3 Then
Data_Distance1 = "0" + Data_Distance1
ElseIf Len(Data_Distance1) = 4 Then
Data_Distance1 = Data_Distance1
End If

Higher_Bit1 = Mid(Data_Distance1, 3, 2)
Lower_Bit1 = Mid(Data_Distance1, 1, 2)
value1 = Higher_Bit1 + Lower_Bit1
MSRecText = ""
MSSendText = "%01#WDD0100801008" + value1 + "**" + Chr(13)
MSComm1.Output = MSSendText
Sleep (50)
MSRecText = MSComm1.Input

End Sub

this is part of my program.. currently the problem i m facing is that whenever i keyed in a negative value, it will give me a strange value..
for example: if i keyed in -10, the hex value will be like FFFFFFF7. i dun wan that.. wat i wan is that the hex value shld be -A. how to improve my program to get wat i wan? can u pls help me on this?
duibye at 2007-11-11 17:31:58 >
# 6 Re: how to convert DEC to HEX?
(abs(X)) means that now X is 10 hence Hex(abs(X)) means that its gonna convert 10 to hexadecimal?? m i rite?

Yes, that is correct. To integrate this in your sample code, you can do something like this:

' Convert positive GetDistance1 to hex
Data_Distance1 = Hex(Abs(GetDistance1))
' Pad with leading zeroes to four characters
Data_Distance1 = String(4 - Len(Data_Distance1), "0") & Data_Distance1
' If GetDistance1 is negative...
If GetDistance1 < 0 Then
' Add leading minus sign
Data_Distance1 = "-" & Data_Distance1
End If
Phil Weber at 2007-11-11 17:32:54 >
# 7 Re: how to convert DEC to HEX?
can we step back a little bit?

there us no such "negative hexadecimal". The hexadecimal rappresentation of a number is just a stream (string) of the bits as they are stored in the number, grouped by bytes (one character per byte). when you convert -10 you get FFF6 (integer) or FFFFFFF6 (long). The hexadecimal -A simply does not exist. No computer will be able to understand it.

Said that, no one will stop you to write down -A (and Phil's suggestion is perfect), but you have to keep that in mind. In your case you are converting the number into a string in hexadecimal, and sending it to a port. Are you sure that the listener is able to recognize "-A"

Marco
mstraf at 2007-11-11 17:33:57 >