2

I cannot find the correct solution or I get an error for something that is very simple in C: how can I convert/move data from a table to string?

The C equivalent of what I am trying to do is:

for (i=0; i <10; i++)
{
    string[i] = table[i]
}

I can convert from string to table with :byte(i) but I don't understand how to append a sign in string.

1
  • 1
    So you have a table like {"x", "y", "z"}, and you want "xyz"? Commented Aug 7, 2022 at 21:29

1 Answer 1

3

To create a string from all values in a table you can use table.concat, this will concatenate each character in a table.

local myTable = { "h", "e", "l", "l", "o" }
local combinedString = table.concat(myTable)
print(combinedString) -- Outputs hello

Run Code Demo

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Skully, is small problem. 1. Table have ASCI value, and the same result I get in print- value 49(dec) will print as 49, not ASCI "1". 2. Table start's from 0, not 1 so table.contact miss first character I not write it also, but table in real is 16bit where first 8bit is empty. my code code MD_BUF[0] = 48 MD_BUF[1] = "a" MD_BUF[2] = 50 MD_BUF[3] = 51 MD_BUF[4] = 52 MD_BUF[5] = 53 MD_BUF[6] = 54 MD_BUF[7] = 55 MD_BUF[8] = 56 MD_BUF[9] = 57 string_buf = table.concat(MD_BUF) print(string_buf) result: a5051525354555657
Sorry, I cannot win with text formating when at comment writing ;/ I try to use loop with MD_BUF[i] = string.char(MD_BUF[i]) before table.concat but still I error with bad argument.
I am having trouble understanding what you mean, is your array full of hexadecimal values that you want to convert to characters? Also, you can paste your code and update your existing question and explain what you mean there more clearly - if you can provide an input and expected output, it would be helpful to understand what you mean.
I want to send text to device and display it. Table who keep this data is 16bit but I use only ASCI. So, in my table I have: code BUF[0]= 0x48 BUF[1]= 0x65 BUF[2]= 0x6c BUF[3]= 0x6c BUF[4]= 0x6f And this table I need to change to string who give me "hello" when I print it. Sorry, I really try to past code here, but I don't know where I made mistake with formatting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.