module(...,package.seeall)--гɼ
--[[ݸʽתdemo
    ֵַ--]]
require "common"--common


--[[bittese
    ܣbitʹãӡ
    ֵ--]]
local function bittest()
	print("bittest:")      --пʼ
	print(bit.bit(2))--λ1ƶλӡ4
	
	print(bit.isset(5,0))--һǲ֣ڶǲλá071true򷵻false÷true
	print(bit.isset(5,1))--ӡfalse
	print(bit.isset(5,2))--ӡtrue
	print(bit.isset(5,3))--طfalse
	
	print(bit.isclear(5,0))--෴
	print(bit.isclear(5,1))
	print(bit.isclear(5,2))
	print(bit.isclear(5,3))
	
	print(bit.set(0,0,1,2,3))--Ӧλ1ӡ15
	
    print(bit.clear(5,0,2)) --Ӧλ0ӡ0
	
	print(bit.bnot(5))--λȡ
	
	print(bit.band(1,1))--,--1
	
	print(bit.bor(1,2))----3
	
	print(bit.bxor(1,2))--,ͬΪ0ͬΪ1
	
	print(bit.lshift(1,2))--߼ƣ100Ϊ4
	
	print(bit.rshift(4,2))--߼ƣ001Ϊ1 
	
	print(bit.arshift(2,2))--ƣӵйأΪ0
  
end

--[[
	packedtest
    ܣչpackĹʾ
    
    ֵ
    --]]
local function packedtest()
	--[[һЩոʽװַ.'z'ַ'p'ֽȣ'P'ַȣ
	'a'ȣ'A'ַͣ'f','d'˫,'n'Lua ,'c'ַ,'b'޷ַ,'h','H'޷Ŷ
	'i','I'޷,'l','L'޷ųͣ">"ʾˣ"<"ʾСˡ]]
	print("pcak.pack test")
	print(common.binstohexs(pack.pack(">H",0x3234)))
	print(common.binstohexs(pack.pack("<H",0x3234)))
	--ַ޷ŶֽͣͣɶַڶƲתΪʮ
	print(common.binstohexs(pack.pack(">AHb","LUAT",100,10)))
	
	print("pack.unpack test:")
	local stringtest = pack.pack(">AHb","luat",999,10)
	--"nextpos"ʼλãĵһֵval1ڶval2val3ݺĸʽ
	--ַҪȡȡַĶͺһֽڵᱻǡ
	nextpox1,val1,val2 = pack.unpack(string.sub(stringtest,5,-1),">Hb")
	--nextpox1ʾλãĳ3nextpox14ƥ999,10
	print(nextpox1,val1,val2) 
end

--[[
	  ռ2ֽ
     ռ4ֽڣ32λ
    double ռ4ֽ
    long double ռ8ֽ
 
    	ȡֵΧ
     [signed]int	-2147483648~+2147483648
    ޷unsigned[int]	0~4294967295
     short [int]	-32768~32768
    ޷Ŷunsigned short[int]	0~65535
     Long int	-2147483648~+2147483648
    ޷ųunsigned [int]	0~4294967295
    ַ[signed] char	-128~+127
    ޷ַ unsigned char	0~255 
	֧С --]]

--[[
	stringtest
    ܣsting⼸ӿڵʹʾ
    
    ֵ--]]
		
local function stringtest()
	print("stringtest:")
	--עstring.charstring.byteֻһֽڣֵɴ256
	print(string.char(97,98,99))--ӦֵתΪַ
	print(string.byte("abc"),2) --һַڶλáǣַλתΪֵ
	local i=100
	local string1="luat100great"
	print("string.format\r\n",string.format("%04d//%s",i,string1))--[[ָʾĿƸʽַΪʮ'd'ʮ'x'
	˽'o''f'ַ's',ƸʽĸĲһ¡ܣضʽ--]]
	--ӡ"luat great"
	print("string.gsub\r\n",string.gsub("luat is","is","great"))--һĿַڶǱ׼ַǴ滻ַ
	--ӡĿַڲַеβλ
	print("string.find\r\n",string.find(string1,"great"))
	--ƥַ,()ָǷָʽַ,ȡַе
	print("string.match\r\n",string.match(string1,"luat(%d+)great"))
	--ȡַڶǽȡʼλãֹλá
	print("string.sub\r\n",string.sub(string1,1,4))
end



--[[bitstohexs()
   ܣתΪʮƣתʮִÿֽ֮÷ָ
   ӡʮִ
   һ֣ڶǷָ
   ֵ          --]]

local function binstohexs(binstring,s)	
	print(common.binstohexs(binstring,s)) --˻еcommon⣬ʮִ
end 
	
--[[ hexstobits
    ܣʮתΪ,תĶ
	ʮ
	ֵ                           --]]
local function hexstobins(hexstring)--ʮתΪַ
	print(common.hexstobins(hexstring)) --עЩǿɴӡɼģЩ
end

--[[
ucs2togb2312
  unicodeС˱ תΪ gb2312,ӡgd2312
  
		ucs2sunicodeС˱,עֽ
ֵ
]]
local function ucs2togb2312(ucs2s)
	print("ucs2togb2312")	
	local gd2312num = common.ucs2togb2312(ucs2s)--õcommon.ucs2togb2312صǱӦַ
	print("gb2312  code",gd2312num)	
end

--[[
gb2312toucs2
  gb2312 תΪ unicodeʮС˱ݲӡ
  
		gb2312sgb2312ݣעֽ
ֵ
]]
local function gb2312toucs2(gd2312num)
	print("gb2312toucs2")
	local ucs2num=common.gb2312toucs2(gd2312num)
	print("unicode little-endian code:"..common.binstohexs(ucs2num))--ҪתΪʮƣ޷
end 

--[[
ucs2betogb2312
  unicode˱ תΪ gb2312룬ӡgb2312,
˱С˱λõ
  
		ucs2sunicode˱ݣעֽ
ֵ
]]
local function ucs2betogb2312(ucs2s)
	print("ucs2betogb2312")
	local gd2312num=common.ucs2betogb2312(ucs2s) --תֱӱֱַ 
	print("gd2312 code "..gd2312num)	
end

--[[
gb2312toucs2be
  gb2312 תΪ unicode˱룬ӡunicode˱
  
		gb2312sgb2312ݣעֽ
ֵunicode˱
]]
function gb2312toucs2be(gb2312s)
	print("gb2312toucs2be")
    local ucs2benum=common.gb2312toucs2be(gb2312s)
	print("unicode big-endian code :"..common.binstohexs(ucs2benum))
end
	
--[[
ucs2toutf8
  unicodeС˱ תΪ utf8,ӡutf8ʮƱ
  
		ucs2sunicodeС˱ݣעֽ
ֵ
]]
local function ucs2toutf8(usc2)
	print("ucs2toutf8")
	local utf8num=common.ucs2toutf8(usc2)
	print("utf8  code"..common.binstohexs(utf8num))
	
end

--[[
utf8togb2312
  utf8 תΪ gb2312,ӡgb2312
  
		utf8sutf8ݣעֽ
ֵ
]]
local function utf8togb2312(utf8s)
	print("utf8togb2312")
	local gb2312num=common.utf8togb2312(utf8s)
	print("gd2312 code"..gb2312num)
	
end

--[[ --]]

bittest()
packedtest()
stringtest()

--[[Գ򣬽ӿھģͿֱӲ,ԡҡΪ--]]

binstohexs("ab")
hexstobins("3132")

ucs2togb2312(common.hexstobins("1162"))  --"1162"""ֵucs2룬common.hexstobinsתΪƣҲֽڡ
gb2312toucs2(common.hexstobins("CED2")) --"CED2"""ֵgb22312  
ucs2betogb2312(common.hexstobins("6211"))--"6211"""ֵucs2be
gb2312toucs2be(common.hexstobins("CED2"))
ucs2toutf8(common.hexstobins("1162"))
utf8togb2312(common.hexstobins("E68891"))--"E68891"""ֵutf8
