03-03-2025 04:33 PM - edited 03-03-2025 04:47 PM
Hello Everyone,
Just looking to be pointed in the right direction, not looking for the specific answer as I am still trying to learn LV.
I'm still pretty new to using LV and was wondering if anyone could help me when it comes to converting a string into a number. What I want to do is ultimately "decode" a word and translate it into a number.
It would follow this code for the translation:
1=a, 2=b, 3=c, 4=d, 5=e, 6 =f, 7=g, 8=h, 9=i, 0=g and -1 otherwise.
Example would be the number 365 is written as cfe.
TIA!!
Solved! Go to Solution.
03-03-2025 04:37 PM
This smells like homework. What have you tried?
Once you describe what you tried and where you got stuck and show us you attempt, we'd be happy to comment.
03-03-2025 04:45 PM
Hi yes this is a homework problem! I am not looking for the answering just rather trying to figure out how to go about this. I used 2 array constants where I dragged the string and numbers into them. Then for the string input for the user I connected it to a string length then connected it quotient and remainder to see if length mod 2 ==0.
Im now planning on using a for loop with a string subset where I will connect the user string input to it. This is as far as I have gotten
03-03-2025 05:46 PM - edited 03-03-2025 06:03 PM
You have a string (word), not an array of strings as input.
You can convert your string to a byte array and use it to index into a lookup table, (or suitable map), iterating over all characters of the input.
Your problem is actually not clear, especially the "-1 otherwise". For example if the word input is zzzzz, would the result be -1-1-1-1 or just -1?
How long can the word be?
Can you explain why 7 and 0 are both "g"?
03-03-2025 07:43 PM
Assuming "0" is actually "j", here's a quick draft. See if you can figure it out.... 😄
03-03-2025 09:14 PM
My bad, you are right it would just be -1.
0 should be j, I mistyped by accident.
03-04-2025 09:03 AM
It would be interesting to know what to optimize for (memory, CPU, clarity) and under what conditions.
Altenbach's routine will be hard to beat when the string is large and all characters are in range.
If the string is large and an early character is (or are statistically often) invalid , a for loop could be faster.
Not sure if you're looking for extra credit (or to learn more than asked), but there's plenty opportunity for that... Big O notation, statistics, parallel execution, etc.
03-04-2025 10:03 AM
wiebe@CARYA wrote:
It would be interesting to know what to optimize for (memory, CPU, clarity) and under what conditions.
Besides that, how adaptable is the code. For example, what if characters change, what if the characters are no longer in order, etc. A map will be slower than the given solution, but it might be easier to change the map when the characters change.
03-04-2025 10:07 AM - edited 03-04-2025 10:10 AM
We really don't have the full problem specifications. One issue might be that the output is a numeric string, not a numeric datatype. Any scalar numeric output will immediately limit the allowed input length, even for U64. My code works for any length that fits in memory.
And yes, for large problems, further optimizations are possible. (For example using a 8bit LUT with 256 elements and a conditional FOR loop that stops on the first failure. 😄 )
03-04-2025 11:31 AM
I think we may need to take a step back and provide some simpler help to get the OP started. A good general way to convert inputs to outputs in a systematic way is a lookup table as Altenbach mentioned. In this case since your inputs are numbers and outputs are letters, you can just index an array of the alphabet to get results.
This small piece of code can then be used in a larger program (SubVI Time!). The next step might be to put this code in a case structure so it will only run when the inputs are valid (number between 1 and 26). After that, run the whole thing in a loop which takes one number at a time and pass it into the conversion code and outputs an array of letters which you can then reassemble into a word.
Sounds like your actual goal is to reverse that and create a coded string. this is called the A1Z26 substitution coding method, also known as the Caesar Cipher. Substitution cipher - Wikipedia
See how far you get with your coding/decoding program and post back to tell us how it's going...