代码之家  ›  专栏  ›  技术社区  ›  Lance Rushing

从字节数组创建变量

php
  •  0
  • Lance Rushing  · 技术社区  · 16 年前

    $bytes = array(0x12, 0x8D, 0x9D, 0x40, 0x09, 0x64, 0x5A, 0x6E);
    

    $string = chr(0x12).chr(0x8D)......;
    

    但这似乎有点骇人听闻。

    有什么建议吗?

    2 回复  |  直到 16 年前
        1
  •  1
  •   dnagirl    16 年前
    $string=implode('',array_map('chr',array(0x8D, 0x9D, 0x40, 0x09, 0x64, 0x5A, 0x6E)));
    
    echo $$string; //given that you also have a variable named whatever the bytecode translates to.
    
        2
  •  0
  •   Lance Rushing    16 年前

    我最后做了和dna女孩一样的事,但更详细。

    另外,由于数组来自ini文件,因此数组未被识别为十六进制值。因此,我添加了一个intval()。

    function getInitializationVector() {
        $ini = parse_ini_file('foo.ini');
        $stringVector = explode(',', $ini['initialization_vector'] );
        $iv = '';
        foreach($stringVector as $theByte) {
            $iv .= chr(intval($theByte, 16));
        }
        return $iv;
    }
    

    initialization_vector=0x8D,0x9D,0x40,0x09,0x64,0x5A,0x6E,0xD4
    

    p、 如果你的键盘正确的话,你不需要储存你的静脉。(但这是另一个故事……)

    推荐文章