LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with hexadecimal string concatenation

I am trying to construct a hexadecimal string to be sent to a machine via serial port. The string consists of a constant command (+ a parameter that is calculated and converted  into U16 and then to hexadecimal string, as shown in the  following block diagram clip
String_Problem_Block.jpg


1. As far is the constant is concerned, it appears exactly as shown in the final string that is to be  send. 
2. When displayed on its own, the hew integer string shows exactly the value I want to send (compare Word_1 and hex integer string in the  following clip).

3. However, when concatenated into  a  final "String", the hex integer string is instead converted into the ascii equivalent of each hex character!!! 

String_Problem_Front.jpg
What am  I doing wrong??? 

0 Kudos
Message 1 of 13
(4,668 Views)

Your final String, and also the Constant, are in Hex Display mode, but the hex integer string is in Normal Display mode.

 

Message 2 of 13
(4,649 Views)

Thanks! That  + I  U16 needs to be typecasted into U8 array and converted to string, rather than using the number to hexadecimal converter,  fixed the thing. 

0 Kudos
Message 3 of 13
(4,614 Views)

@fsahmed wrote:

The string consists of a constant command (+ a parameter that is calculated and converted  into U16 and then to hexadecimal string, as shown in the  following block diagram clip


Let's take a step back.  What exactly do you mean by "hexadecimal string"?  A raw data stream?  A string with ASCII values that represent a hexadecimal number?

 

You're problem is that you are mixing these two concepts.  Your string constant and indicator are showing the raw data stream (Hex Display).  But the Number To Hexadecimal String creates the ASCII value.  If you want all to be the raw data, use Flatten To String instead of the Number To Hexadecimal String.


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
Message 4 of 13
(4,583 Views)

 


@crossrulz wrote:

You're problem is that you are mixing these two concepts.  Your string constant and indicator are showing the raw data stream (Hex Display).  But the Number To Hexadecimal String creates the ASCII value.  If you want all to be the raw data, use Flatten To String instead of the Number To Hexadecimal String.


Just for my own benefit, can someone comment on usage:

I usually use Type Cast. Crossrulz recommended to use Flatten to String. Other than the obvious difference (Prepend size, byte order, etc), is there any benefit to use one over the other? Both yield the same results.

 

Choose one over the other.png

0 Kudos
Message 5 of 13
(4,568 Views)

Hi Jamiva,

 

FlattenToString becomes handy once you convert something different than byte arrays as you can determine the byte order easily…

 

In your snippet I would even use ByteArrayToString instead. 🙂 (Didn't check but guess you're using an U8 array constant.)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 6 of 13
(4,555 Views)

The bytearray to string did my bidding, all works now, the world makes sense again. 

0 Kudos
Message 7 of 13
(4,546 Views)

@jamiva wrote:

I usually use Type Cast. Crossrulz recommended to use Flatten to String. Other than the obvious difference (Prepend size, byte order, etc), is there any benefit to use one over the other? Both yield the same results.

 

Choose one over the other.png


Once you deal with enough systems, you realize that the Endianness (byte order) matters A LOT.  If you are talking to a Little Endian system, then Type Cast will give you bad results where the Flatten To String allows you to fix that.  But this only really affects multi-byte values.  An I8, U8, array of I8 or U8, or strings will not matter.  But I32, DBL, etc do care.

 

And for the array of U8, use the Byte Array To String.  That is literally a no-op since a string and array of U8 actually have the exact same structure.


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 8 of 13
(4,533 Views)

The reason I asked, a long time ago I didn't know about the flatten & unflatten from string functions. So when I ran into byte order issues, I wrote my own conversion subVIs and stuck them in a library. Here is one of them and I STILL use all of them to date. It's hard to teach an old dog new tricks. On a scale of 1 to 10, how bad is the Rube Goldberg Code. Smiley Very Happy

 

my_code.png

0 Kudos
Message 9 of 13
(4,526 Views)

@jamiva wrote:

On a scale of 1 to 10, how bad is the Rube Goldberg Code.


 

Is 0 or 10 the best RG? Is the worst RG perfect code,5 being borderline RG?

 

Anyway, it's not bad at all. It's clear at a glance and that's what most important. The unflatten has benifits (probably speed, error in\out, endianness) so it's preferred.

0 Kudos
Message 10 of 13
(4,510 Views)