Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Lua Online

function string.replace(data, oldSubString, newSubString)
    data = data or ''
    oldSubString = oldSubString or ' '
    newSubString = newSubString or ''

    return data:gsub(oldSubString, newSubString)
end

function string.blockDivide(data, sizeBlock)
    sizeBlock = sizeBlock or 1
    data = tostring(data) or string.rep('0', sizeBlock)

    countBlock = math.ceil(data:len() / sizeBlock)
    data = data ..string.rep('0', sizeBlock * countBlock - data:len())

    local output = {}
    output.length = countBlock
    
    for i = 1, countBlock do
	    output[i] = data:sub(1 + (i - 1) * sizeBlock, i * sizeBlock)
    end

    return output
end

function string.blockDelimiter(data, delimiter)
    local result = {}
    local counter = 0

    data = data or ''
    delimiter = delimiter or ' '
    
    repeat
        counter = counter + 1
        
        local a, b = data:find(delimiter, 1, true)
        
        if a and b then
            result[counter] = data:sub(1, a - 1)
            data = data:sub(b + 1, data:len())
        else
            result[counter] = data
            data = nil
        end
    until result[counter] == nil or data == nil
    
    result.length = counter
    return result    
end

function string.blockParse(data, start, finish)
    local result = {}
    local counter = 0
    
    if start and finish then
        data = data or ''
        
        repeat
            counter = counter + 1
            
            local a, b = data:find(start, 1, true)
            
            if a and b then
                local c, d = data:find(finish, b + 1, true)
                
                if c and d then
                    result[counter] = data:sub(b + 1, c - 1)
                    data = data:sub(1, a - 1) ..data:sub(d + 1, data:len())
                end
            end
        until result[counter] == nil
        
        counter = counter - 1
    end
    
    result.length = counter
    return result
end

function blockConcat(data, left, right, delimiter)
    local result = ''

    data = data or {length = 0}
    left = left or '<'
    right = right or '>'
    
    for i = 1, data.length do
        result = result ..left ..data[i] ..right
        
        if data[i + 1] ~= nil and delimiter then
            result = result ..delimiter
        end
    end
    return result
end


--============================================================================--

function string.UTF8_to_UTF16(utf8)
    utf8 = utf8:blockDivide(2)

    local utf16 = ''

    local i = 1
    repeat
        local num1 = tonumber(utf8[i] or '00', 16) or 0
        local num2 = tonumber(utf8[i + 1] or '00', 16) * math.floor(num1 / 128)
	
    	local NUM1 = num1
    	local NUM2 = num2
    	
    	local hex1 = '00'
    	local hex2 = '00'
    	
    	if num1 < 128 then
    	    hex2 = utf8[i]
    	elseif num1 >= 192 then
    		num1 = math.ceil(((NUM1 % 32) - (NUM1 % 4)) / 4)
    		hex1 = string.format('%x', num1)
    
    		num2 = (NUM1 % 4) * 64 + (NUM2 % 64)
    		hex2 = string.format('%x', num2)
    	end
    	
    	hex1 = string.rep('0', 2 - hex1:len()) ..hex1
    	hex2 = string.rep('0', 2 - hex2:len()) ..hex2
    	utf16 = utf16 ..hex1 ..hex2
    	
    	i = i + math.ceil(NUM1 / 128)
    until i > utf8.length
	
    return utf16
end

function string.UTF16_to_UTF8(utf16)
    utf16 = utf16:blockDivide(4)
    local utf8 = ''

    local i = 1
    repeat
    	local Bytes = utf16[i]:blockDivide(2)
    	local num1 = tonumber(Bytes[1] or '00', 16) or 0
	    local num2 = tonumber(Bytes[2] or '00', 16) or 0
	    
	    local hex1 = ''
	    local hex2 = ''

    	if num1 > 0 then
    		num1 = 192 + num1 * 4 + math.floor(num2 / 64)
    		hex1 = string.format('%x', num1)
    		hex1 = string.rep('0', 2 - hex1:len()) ..hex1
    		
    		num2 = 128 + num2 % 64
    		hex2 = string.format('%x', num2)
    		hex2 = string.rep('0', 2 - hex2:len()) ..hex2
    	else
    		hex2 = string.format('%x', num2)
    		hex2 = string.rep('0', 2 - hex2:len()) ..hex2
    	end
    	
    	utf8 = utf8 ..hex1 ..hex2
        	
    	i = i + 1
    until i > utf16.length
	
    return utf8
end

function string.toUTF8(data)
    data = data:blockDivide()
    
    local utf8 = ''

    for i = 1, data.length do
        local hex = string.format('%x', string.byte(data[i]))
        hex = string.rep('0', 2 - hex:len()) ..hex
       
        utf8 = utf8 ..hex
    end

    return utf8	    
end

function string.UTF8_to_Str(utf8)
    utf8 = utf8:blockDivide(2)
    local str = ''

    local i = 1
    repeat
        num1 = tonumber(utf8[i] or '00', 16) or 0
        num2 = tonumber(utf8[i + 1] or '00', 16) * math.floor(num1 / 128)

    	if num2 > 0 then
    		str = str ..(string.char(num1, num2) or '')
    	else
    		str = str ..(string.char(num1) or '')
    	end
    
        i = i + math.ceil(num1 / 128)
    until i > utf8.length   
    
    return str    
end

function string.toUTF16(data)
    data = data:toUTF8()
    data = data:UTF8_to_UTF16()
    return data
end

function string.UTF16_to_Str(utf16)
    local utf8 = utf16:UTF16_to_UTF8()
    return utf8:UTF8_to_Str()
end

function string.toURL(data)
    data = data:toUTF8()
    data = data:blockDivide(2)
    data = blockConcat(data, '%', '')
    return data
end

--============================================================================--

text = "\nA\nB"
print(text)
result = text:toUTF8()
print(result)

result = result:UTF8_to_UTF16()
print(result)

result = result:UTF16_to_UTF8()
print(result)

print(result:UTF8_to_Str())

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.