LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

5 byte string to two 20bit numbers

Hello,
I have a problem converting String consisting of 5 bytes to two 20bit signed integers. I tried using "split number" on 8 bit integer but that doesn't work since there is no 4bit type I guess. So I converted the middle byte into boolean array and split into two 4 bit boolean arrays but now I don't know how to join the two 4bit boolean arrays and the remaining 8bit numbers.

 

The code in the picture is more complicated because I want to convert a string consisting of 5*N bytes and each group of 5 bytes I would like to split into two 20bit signed integers.

FilipO28555_0-1721148161342.png

The problem in my code is that joining turns red which I suppose means that one part is ignored

FilipO28555_1-1721148333984.png

 

 


Thank you

0 Kudos
Message 1 of 9
(1,343 Views)

This would be an excellent place to put some example "data" so that we can understand what you mean.  You talk about a "5-byte string" -- what does this really represent?  Is the "string" really text?  Or is it really a series of "bytes", most of which do not represent ASCII characters?  Note that this has direct implications to the question "How do you read/understand the 5-byte string?"  Can you show us an example?  Does it look like "ab3z!"?

 

Once you know what a "string" means and how to interpret it as bits, the problem almost "solves itself".  Convert the string to a bit pattern -- you'll have 40 "meaningful" bits, which fit nicely into a U64 number.  You can now split off the low (or high, depending on circumstances, which is why you write some "trial code" and look at the intermediate values) 20 bits twice, which should leave you with 24 bits of 0.  A 20 bit number, of course, fits nicely into a U32 (or an I32, since if you've done this correctly, the high 12 bits should all be 0).

 

Give it a try.  A function you might find very useful is TypeCast (found on the Numeric Palette) -- feed it a 5-character string, tell it you want a U64 representation, and see what happens.

 

Bob Schor

 

P.S.-- Welcome to the LabVIEW Forums.

0 Kudos
Message 2 of 9
(1,318 Views)

I don't have LabVIEW available right now, so I can't code it up.  But what I would do is Type Cast or Unflatten From String to convert the 5 bytes into a U64.  I would then use AND and/or Logical Shift to extract the bits I care about for each value.  For instance, I would take the value and AND it with 0xFFFFF to get the second value.  I would take the value and Logical Shift it to the right 20 times to get the first.  You can then convert them to a I32 and perform additional shifting to get the needed values.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 9
(1,313 Views)

This is basically the equivalent of what you tried to do.

 

40 Bit to 2 x 20 Bit.png

 

However if this is what you really want is very much unclear. You have not told us at all, in which byte order the data is supposed to be. The byte order as shown here would be basically Big Endian or Most Significant Byte first. Also there are numerous possibilities how those two 4-bits might be placed in the byte stream. The one as you did it is a possibility but not the most logical one.

 

Also as is the values in the Array 1 will be wrong as the upper 16 bit will contain the first two bytes and the lower 16 bit will contain the 4 bits. It's all a big mess and there is no good solution without at least a 5 byte string of example data and what numbers that should result in.

40 Bit to 2 x 20 Bit 2ND.png

This would solve the problem with the totally wrong values in Array 1 but only if the bytes are really in Big Endian format and the middle byte contains the two 4 bit values.

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 9
(1,310 Views)

Hi Filip,

 

@rolfk already nailed it, but here is an alternative for a 5-byte string:

Reinterpret the string into a U64, then use bit shifting and masking to get the 2 parts in 2 U32s:

 

raphschru_0-1721151612548.png

 

Regards,

Raphaël.

0 Kudos
Message 5 of 9
(1,293 Views)

Do I hear an echo?  In the second Post on this Message, I mentioned that we had no idea what the "string" data represented.  I suggested he learn about TypeCast, mentioned a U64, also mentioned "looking at the data" and then figuring how to isolate the two 20-bit pieces (which I'm assuming are going to be a U32, but it might be the low 20 bits of an I32, where the MSB is the "sign bit" that needs to be propagated (I've run into this a time or two).

 

Bob Schor

0 Kudos
Message 6 of 9
(1,255 Views)

@Bob_Schor ah yeah, this is a bit redundant with your answer, that's what happens when several people write an answer at the same time... and sometimes my G developer eyes skip comments that do not show actual code...

0 Kudos
Message 7 of 9
(1,209 Views)

I tend to make things a bit more scalable:

 

altenbach_0-1721175511208.png

 

Message 8 of 9
(1,166 Views)

@altenbach wrote:

I tend to make things a bit more scalable:

 

altenbach_0-1721175511208.png

 


You also tend to make things the size of a postage stamp.  🙂

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 9 of 9
(1,100 Views)