douhao7889 2019-03-03 04:36
浏览 386
已采纳

将64个字符的十六进制字符串转换为BINARY(32)

I have an input in the form of "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"(64 hexadecimal characters obtained from SHA-256) which I want to store in a database with MySqli. The column the value should be inserted to is of type BINARY(32). I think that the value is interpreted as a string, rather than a hex number, so MySQL returns "Data too long for column x at row y". How can I convert this string to 32 bytes in PHP? I already tried adding "0x" as a prefix, MySql's CONVERT function and I read about hex2bin and bin2hex, which don't seem to do what I want, and i tried both string and int types in bind_params (int results in 0x3000... regardless of the input)

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongye8110 2019-03-03 04:55
    关注

    Okay, Joni's comment is the answer, i thought about hex2bin just as a way to make hex-encoded string readable, and i didn't realize it compresses the hex value to a string which is short enough to be stored as binary. Thanks to joni and tkausl for enduring my incompetence in php and in managing stackoverflow questions :)

    EDIT: i can't close the question since the answer was posted as a comment, sorry for that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部