NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

String to Byte Array

Solved!
Go to solution

I convert an array of bytes in a character string with the following expression:

Locals.String += Chr( Locals.U8[RunState.LoopIndex])

How to convert a character string into an array of bytes in TS (similar to the LV function of String To Byte Array). TS function Asc () returns only the first character in the string.

0 Kudos
Message 1 of 3
(5,906 Views)
Solution
Accepted by topic author AlexKu

Hi Alex,

 

Looking at your bytetochar.seq file, I think you were very nearly there, but it looks like the Mid() wasn't indexed correctly; 0 was used instead of RunState.LoopIndex.

 

You could acheive the String To Byte Array all in a single Statement Step using the following expressions:

 

Loop While Expression

RunState.LoopIndex < Len(Locals.String)

Pre-Expression

SetNumElements(Locals.U8, Len(locals.String))

 

Post-Expression

Locals.U8[RunState.LoopIndex] = Asc(Mid(Locals.String, RunState.LoopIndex))

 

You could also do the String To Byte Array conversion in a sub sequence if you wanted to use this many times on different strings and just maintain one method to do it.

 

I've attached example sequences for both ways of implementing this.

 

Regards,

Charlie Rodway | Principal Software Engineer | Certified TestStand Architect (CTA)

Computer Controlled Solutions Ltd | NI Silver Alliance Partner | GDevCon#1 Sponsor

Message 2 of 3
(5,854 Views)

Thanks for reply, Charlie.

Your solution helped me. But I have another problem. Array Byte to String conversion function Chr() removes the numeric value 0x00, believing that this is the end of the string. As a result, the function Asc () does not return a numeric value 0x00. How can I avoid this? I've added an example.

 

 

0 Kudos
Message 3 of 3
(5,815 Views)