zoukankan      html  css  js  c++  java
  • lua 按拉分析与合成

    -- 将数值分解成bytes_table
    local function decompose_byte(data)
        if not data then
            return data
        end
    
        local tb = {}
    
        if data == 0 then
            table.insert(tb, 0)
            return tb
        end
    
        local idx = 1
        while data > 0 do
            table.insert(tb, math.mod(data, 2))
            data = math.floor(data/2)
        end
    
        return tb;
    end
    
    -- 按位合成一个数值
    local function synthesize_byte(bytes_table)
        local data = 0;
        for i,v in ipairs(bytes_table) do
            data = data + math.pow(2, i - 1) * bytes_table[i]
        end
    
        return data
    end
    
    
    -- 使用方法
    local function show_bytes(t)
        local str = ""
        for i,v in ipairs(t) do
            str = tostring(v) .. str;
        end
        print(str)
    end
    
    local tb = decompose_byte(11)
    show_bytes(tb);
    print(synthesize_byte(tb))
    
    local str = string.format("%c%c", 255, 98)
    print(str, #str)
    
    tb = decompose_byte(string.byte(str, 1))
    show_bytes(tb)
    print(synthesize_byte(tb))
    
    tb = decompose_byte(string.byte(str, 2))
    show_bytes(tb)
    print(synthesize_byte(tb))
  • 相关阅读:
    67家基金子公司背景脉络梳理
    港股奇葩术语知多少
    68家信托公司7大派系股东分食图谱
    港股术语
    流动性陷阱
    ORACLE归档模式和非归档模式的利与弊
    肥尾效应
    DVP
    金融衍生品如何定价
    绿鞋机制
  • 原文地址:https://www.cnblogs.com/emyueguang/p/3892222.html
Copyright © 2011-2022 走看看