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.

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

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面