douhao7889 2019-03-03 12:36
浏览 385
已采纳

将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 12: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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度