huanyingch01
V2EX  ›  Node.js

求助 nodejs buffer 输出到文件非二进制

  •  
  •   huanyingch01 · Jul 18, 2018 · 5019 views
    This topic created in 2856 days ago, the information mentioned may be changed or developed.
    let bufOut = new Buffer(pos);
    bufBin.copy(bufOut, 0, 0, pos);
    
    fs.writeFile('./magic.bin', bufOut, function(err){
        if(err){
            console.log("error");
        }else {
            console.log("ok");
        }
    })
    

    为什么文件显示的还是 utf8 编码的类容,还非二进制显示? 输出如下

    3_id8_nameSid3_py8_keyword9_costGems8_skillId1I1D1S1B1I2[I61110014soul111941000420004300044000
    
    4 replies    2018-07-18 13:58:49 +08:00
    kohos
        2
    kohos  
       Jul 18, 2018
    fs.writeFile('./magic.bin', bufOut, function(err){
    这句改成
    fs.writeFile('./magic.bin', bufOut, 'binary', function(err){
    或者 hex 等其他想要的编码试试
    yyfearth
        3
    yyfearth  
       Jul 18, 2018
    你要怎样二进制显示呢?
    如果你原本的内容就已经是 utf8 文本 就算你用二进制方式编码
    fs.writeFile('./magic.bin', bufOut, 'binary', function(err){
    存下来的文件还是文本啊 你里面又没有什么非文本的内容

    除非你用 HEX 或者 Base64 编码 那么就会保存成代表二进制你看不懂的文本了 不知道是不是你想要的
    yyfearth
        4
    yyfearth  
       Jul 18, 2018
    你里面又没有 unicode 或者 真正非文本二进制数据
    ascii 的文本内容 你怎么存 只要不是 hex 或者 base64 再编码 也还是文本啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2982 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 15:21 · PVG 23:21 · LAX 08:21 · JFK 11:21
    ♥ Do have faith in what you're doing.