繁体   English   中英

Haxe-haxe.io.Bytes的十六进制字符串

[英]Haxe - hexadecimal string to haxe.io.Bytes

我将二进制blob存储在SQLite中,因此通过SELECT查询,我得到十六进制字符串。 haxe中是否有一种简单的方法可以将十六进制数字字符串转换为haxe.io.Bytes Bytes toHex()函数的反向操作)

您可以使用BaseCode ,但我不确定std库中是否更简单/更简单。

import haxe.io.Bytes;
import haxe.crypto.BaseCode;

class Main {

    static function main() {
        var hex = "26fB0d";
        var bytes = decode(hex);
        trace(bytes.toHex());
    }

    static function decode(str:String) {
        var base = Bytes.ofString("0123456789abcdef");
        // not using `decode` or `decodeString` because the result may contain \0
        // and that is not accepted in strings on all targets
        return new BaseCode(base).decodeBytes(Bytes.ofString(str.toLowerCase()));
    }

}

我想出了这样的东西:

public static function toBytes(hex:String):BytesData{

    var output = new haxe.io.BytesOutput();
    var len = Std.int(hex.length/2);

    for (i in 0...len){
        var byte:UInt = Std.parseInt(("0x" + hex.substr(i*2, 2)));
        output.writeByte(byte);
    }

    return output.getBytes().getData();
}

@jonasmalacofilho的答案也有效,可能更好,我尚未检查,这更快并且我的解决方案是否可靠。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM