11-03-2013 03:29 PM - edited 11-03-2013 03:30 PM
Hi, I am trying to convert decimal number to little endian hex string to write to serial port. I've tried the type cast method but it doesn't give me the right output. for example, I have a decimal 2350 which in hex is 09 2E (HEX) , but when I type cast the double 2350 to hex string, i get 40A2 5C00 0000 0000 which is not right. then I tried the flatten to string function to change the endian order, that didn't work for me either.can anybody help me please? Thanks very much.
1) FIRST 2 BITS STATTION NUMBER, NUMBER BIT TO SEND
STATION 1--> 12
STATION 2--> 22
STATION 3--> 32
STATION 4--> 42
2) SECTION SELCTION
70 --> LF
7A --> HF
3) POSITION
23.5 % --> 2350 IN DECIMAL --> 09 2E (HEX)
REVERSE THE BIT( HEX) --> 2E 09
4) CHECK SUM
12 XOR 70 XOR 2E XOR 09 --> 45
THE COMMAND TO MOVE STATION 1 LF TO 23.5%
12 70 2E 09 45
Solved! Go to Solution.
11-03-2013 03:59 PM - edited 11-03-2013 04:01 PM
For little endian, you can use Swap Bytes to swap the most significant byte with the least significant if you have a 2 byte piece of data.
Make sure you are working with a U16 datatype and not a double datatype like you said you were working with.
Try attaching your VI so we can see what you are doing now.
11-03-2013 03:59 PM
Can you attach your code? You can typecast "2350" to a string, but it doesn't mean anything to typecast it to a "hex" string. You either have text - ASCII characters - or you have a numeric value, which you can then display to the user in any radix (decimal, hex, binary) that you want. Or, if you're receiving the data, either it's text in a specific base, or it's a number. If you have the actual number value and you need to byte- or word-swap it before sending it as a string, then use the byte- and word-swap functions, followed by "Number to Hexadecimal String."
11-03-2013 04:27 PM - edited 11-03-2013 04:29 PM
@alex75 wrote:
for example, I have a decimal 2350 which in hex is 09 2E (HEX) , but when I type cast the double 2350 to hex string, i get 40A2 5C00 0000 0000 which is not right. then I tried the flatten to string function to change the endian order, that didn't work for me either.can anybody help me please? Thanks very much.
A DBL has 8 bytes. If the hex representation is 092E, you are dealing with a 2byte integer representation, probably U16. Convert your DBL to U16 before flattening to string and you should get the right result. See image.
(Of course for the general case, you need to check if the DBL does not contain a fractional part and that it first within the range if U16.)
11-03-2013 04:33 PM
here's a simple code i tried that didn't work. thanks for your help
11-03-2013 04:38 PM - edited 11-03-2013 04:59 PM
As has been said, if you want a 2byte output, you need to flatten from a datatype that contains exactly two bytes, e.g. U16. (DBL contains 8 bytes!).
It is highly recommended to always work with appropriate datatypes. Don't use DBL when dealing with integers. Can you tell us where the data comes from? WHere is the rest of your code? It would be silly to do the checksum calculation in DBL, for example.
Study this document for some insight.
11-03-2013 05:27 PM
the data comes either from user by typing a decimal number on front panel (which translates to movement of a stepper motor) or selecting from an enum(for different motor positions), or in an automatic mode, read from an array of decimal strings like: 100, 200, 300, 400, 500, 600, 700, .....2500 from a text file. i tried this for checksum but doesn't work well either. thanks
11-03-2013 05:45 PM
and here's another try
11-03-2013 05:55 PM - edited 11-03-2013 05:57 PM
Why are you still converting to U32 instead of U16?
Posting endless versions of broken code is not useful unless you tell us exactly what's wrong with it. What you get and what you expect to get.
Programming does not work by randomly changing things around until the result is correct. There needs to be some kind of reasoning.
Do you have the manual of the instrument? What does it expect?
@alex75 wrote:
@Earlier, you wrote:
1) FIRST 2 BITS STATTION NUMBER, NUMBER BIT TO SEND
STATION 1--> 12
STATION 2--> 22
STATION 3--> 32
STATION 4--> 42
2) SECTION SELCTION
70 --> LF
7A --> HF3) POSITION
23.5 % --> 2350 IN DECIMAL --> 09 2E (HEX)
REVERSE THE BIT( HEX) --> 2E 09
4) CHECK SUM12 XOR 70 XOR 2E XOR 09 --> 45
THE COMMAND TO MOVE STATION 1 LF TO 23.5%
12 70 2E 09 45
This makes no sense. For example there are only two bits (not bytes!) for the station number. 2 bits are not sufficient to represent 12, 22, 32, 42 as requested, unless there is some hidden lookup table.
11-03-2013 06:26 PM
@altenbach wrote:
This makes no sense. For example there are only two bits (not bytes!) for the station number. 2 bits are not sufficient to represent 12, 22, 32, 42 as requested, unless there is some hidden lookup table.
My interpretation is that the first 2 bits represent stations 1-4, followed by the number of bytes to send (thus the 2 for 2 bytes) although I admit it doesn't match the way the sample is written which looks like 4 bits for the station and 4 bits for the number of bytes since otherwise it would be 02,12,22,32.