07-16-2024 11:45 AM - edited 07-16-2024 11:47 AM
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.
The problem in my code is that joining turns red which I suppose means that one part is ignored
Thank you
07-16-2024 12:17 PM - edited 07-16-2024 12:18 PM
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.
07-16-2024 12:23 PM
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.
07-16-2024 12:27 PM - edited 07-16-2024 12:36 PM
This is basically the equivalent of what you tried to do.
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.
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.
07-16-2024 12:46 PM - edited 07-16-2024 12:58 PM
07-16-2024 03:03 PM
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
07-16-2024 06:19 PM - edited 07-16-2024 06:21 PM
07-16-2024 07:19 PM
07-16-2024 08:46 PM
@altenbach wrote:
I tend to make things a bit more scalable:
You also tend to make things the size of a postage stamp. 🙂