PC SOFT

PROFESSIONAL NEWSGROUPS
WINDEVWEBDEV and WINDEV Mobile

Home → WINDEV 2024 → Decimal to Hex conversion
Decimal to Hex conversion
Started by Patrick Thijs, Apr., 11 2004 12:05 AM - 2 replies
Posted on April, 11 2004 - 12:05 AM
I'm writing a program that needs to communicate with a device by the serial port.
The communication protol requires me to place a checksum behind every command I send to the device.
For example : when I want to send the command :I
I have to calculate the checksum, wich is 0x00 - 0x49 - 0x3A
This is 0 minus the hexvalue of the ascii-code for the first byte (I) minus the hexvalue of the ascii-code for the second byte (:)
The result should be 7D (0x37 0x44)
Because it's not possible to do math operations on Hex numbers in Windev, i'm doing the calculation in Decimal values, wich is 0 - 73 - 58
the result is now -131, when I use a mathematical calculator to convert this to Hex, again I get 7D, wich is correct.
My problem now, how can I convert a negative decimal number into Hex in Windev ? The code that the wizzard generates, only works for positive numbers, but I want to be able to do this with Negative numbers also.
Any suggestions from the math-wizzards here ? Becaus math is not really my thing :-)
Greetz,
Patrick


Carmen
Posted on April, 11 2004 - 2:10 AM
The representation of the checksum in hex is only for the programmer. Assuming you are sending a 1 byte code to the device, you would just do your calculations and then send the result to the serial port. If you want to verify the result in hex you could try something like:
x1 is int on 1 byte
x2 is int on 1 byte
// using hex values for the math
x1 = 0x00 - 0x49 - 0x3a
// using Asc function to convert the characters
x2 = 0 - Asc("I") - Asc(":")
// display the results in hex
Info(NumToString(x1,"2x"))
Info(NumToString(x2,"2x"))
I hope this helps
Floyd
I'm writing a program that needs to communicate with a device by the serial port.
The communication protol requires me to place a checksum behind every command I send to the device.
For example : when I want to send the command :I
I have to calculate the checksum, wich is 0x00 - 0x49 - 0x3A
This is 0 minus the hexvalue of the ascii-code for the first byte (I) minus the hexvalue of the ascii-code for the second byte (:)
The result should be 7D (0x37 0x44)
Because it's not possible to do math operations on Hex numbers in Windev, i'm doing the calculation in Decimal values, wich is 0 - 73 - 58
the result is now -131, when I use a mathematical calculator to convert this to Hex, again I get 7D, wich is correct.
My problem now, how can I convert a negative decimal number into Hex in Windev ? The code that the wizzard generates, only works for positive numbers, but I want to be able to do this with Negative numbers also.
Any suggestions from the math-wizzards here ? Becaus math is not really my thing :-)
Greetz,
Patrick
Posted on April, 14 2004 - 3:10 AM
>From Michel Fages some time ago and available through forum search:
Hello,
Decimal to Hexa :
hex = NumToString(dec, "x")
Hexa to decimal
dec = Val(hex, 16)
Regards,
Michel Fages
Hi
How do I convert a Decimal(base10) to Hexidecimal(base16) and reverse?
Tried all the keywords I can think of in the help without success
Regards
DT

Easy is best
Gill