lua-users home
lua-l archive

Re: Convert bytes array to string

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


--- SATO Seichi <seichi@air.ne.jp> wrote:
> How can I convert bytes array to string efficiently in Lua?
> or must I extend with C?
> 
> a sample I wrote:
> 
> function bytestostring (bytes)
> s = ""
> for i=1,getn(bytes) do
> s = s..strchar(bytes[i]) -- '..' create new string. Expensive!!
> end
> return s
> end
You can do something like this:
 function bytestostring (bytes)
 local buf = strrep(' ', getn(bytes))
 local i = 0
 return (gsub (buf, '(.)', 
 function(c)
 i = i + 1
 return strchar(bytes[i])
 end))
 end
 
Not exactly pretty, but it's a lot faster than string concatenation.
Cheers,
Eric
__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/

AltStyle によって変換されたページ (->オリジナル) /