V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
samohyes
V2EX  ›  程序员

js 的 buffer 能转到 Python 的字符串吗

  •  
  •   samohyes · 2023-08-15 16:52:16 +08:00 · 626 次点击
    这是一个创建于 458 天前的主题,其中的信息可能已经有所发展或是发生改变。

    求教下,我这边有个数据看起来是 js 里面的 buffer ,

    AQAAAAMNAKfqpTYk//ZYmATGU2AssmiKsqN7Z3I1BokbGoxZRlFnCUb+2Txc0Qifw99/eQOIZcoQBGrxC2pA9sKVT22p0AYBAin/xrQd6wgqACW8QgiZ/eT9J8lMRcJO3Whvguz3zb4nCawJUVyAdGKRkGDK2pfqxYM8loZrJXyEWLW+SnnUPR8AA54itCstZzbd8fsldmQIYnlxV/r5uS3vj2mjwatpqYN3DPPCdmSuDbINUIQiRBKEbNM69Ilny3caA4MMvZDGbbYBBA8erF5hM0hvRVgas9ewtEtOjbSsPhCVm0ZojhU661e6YAYwq33UAibAW3tIDNKCz5okieop4DRGTPKUHI/m3QsBBoQirRJNBmpr95Tk0Kps5g2OOssv/+VmoiiF44e6xxCXIfAlbyUz7CG9GqnxbXxKSy1+vco9RRSDY+HawEV7hHwBCeZue0QQd3u1POAQ3t0BnXI13NMWz/dYb9Xer9Qd60ODA0JiXYf83Sp7NttqyOvhMbPMnEO9pXK3zQpeLXSN6VUACoAS++omDlAF76BfZk4Bk55SU3XVnAUKCs+6qZzHj0bmW6krh6zwZyb4o/hkJAyu8bvyMz62i90DJiZVLIlsL5EADI51wZ0GMCP/VH+UpoTFYMf1IhEHt9o2foQLtI2mEbIgE4jQH2nRlmk5aZrF4Be/faJRYZYoP8nSiPmWfLUlUJ4ADXG4ghVxSdRTe+MRLt6d2BLUgIAZM0jn2N+y1nHtHQFvDieM3Sk0z7vXxUr8g/hXWW4IuzKHnT/4+I32v8zWeQ4BDuswcfeFPiJe/+s4sa9Ea0hztOlKuhoH0ThitOJSvdLjGRwrYhg5D165huoVZ0EgcMA9VrEVZGzNuFZtECPpy3IAEA22nFW0UZRKYgU2DEdxF83+1wh9tHK38ZfmZPFkljDUPXxq6MZcAxIRCOXTneNVJQAFeMMbdzj0xRxZmgsfirMBEenWasmUwyYyYdjwYdCpjHcpbIdMNtxbstjC5pz7/73KPHDKC+ExtHmqsGLcoUtz3zf/Gu9KomdofkPkMXoLeCwBEqgYsGa/a15/cPp3m85kIj/Tyb0gBYgKFx9kzhZor/WvM3vBWIF2DioKJdhwWIXVnWa5a7l9k970IIR5OVmv7ggBZNrqj0ABAAAAAgAAAAAAAAAAAAAAAD7hiyIUr/lwANl0z2R+fDR+j6WFAAAAAAAB3h4BAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADedllsuAAAAAAAAAAAAAAAAdlnOFH0OcURUBzpd1wA1RCNLaqAAAgAAAAAAAAAAAAAAAGWo8HvZqFmOG1tsCoj0d528B3Z1AAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
    

    我看 js 里面对方用的解码方式是

    export const uint8ArrayToHex = (a: Uint8Array): string =>
      Buffer.from(a).toString("hex");
    

    我用了下结果提示

    secp256k1 unavailable, reverting to browser version
    4151414141414d4e414b66717054596b2f2f5a596d41544755324173736d694b73714e375a334931426f6b62476f785a526c466e4355622b32547863305169667739392f65514f495a636f51424772784332704139734b56543232703041594241696e2f7872516436776771414357385167695a2f6554394a386c4d52634a4f3357687667757a337a62346e4361774a5556794164474b526b47444b3270667178594d386c6f5a724a587945574c572b536e6e555052384141353469744373745a7a62643866736c646d5149596e6c78562f7235755333766a326d6a7761747071594e3344505043646d53754462494e5549516952424b45624e4d3639496c6e7933636141344d4d765a44476262594242413865724635684d30687652566761733965777445744f6a625373506843566d305a6f6a6855363631653659415977713333554169624157337449444e4b437a356f6b69656f703444524754504b5548492f6d33517342426f516972524a4e426d70723935546b304b70733567324f4f7373762f2b566d6f69694634346536787843584966416c6279557a3743473947716e786258784b5379312b76636f3952525344592b4861774556376848774243655a756530515164337531504f4151337430426e584931334e4d577a2f6459623958657239516436304f4441304a6958596638335370374e747471794f76684d62504d6e454f3970584b337a5170654c58534e36565541436f41532b2b6f6d446c4146373642665a6b34426b353553553358566e41554b43732b36715a7a486a30626d57366b7268367a775a7962346f2f686b4a417975386276794d7a3632693930444a695a564c496c734c35454144493531775a30474d43502f56482b55706f5446594d66314968454874396f32666f514c7449326d4562496745346a5148326e526c6d6b35615a72463442652f66614a52595a596f50386e5369506d57664c556c554a3441445847346768567853645254652b4d524c74366432424c556749415a4d306a6e324e2b79316e4874485146764469654d33536b307a37765878557238672f685857573449757a4b486e542f342b49333276387a5765513442447573776366654650694a652f2b7334736139456130687a744f6c4b75686f4830546869744f4a5376644c6a47527772596867354431363568756f565a304567634d4139567245565a477a4e75465a744543507079334941454132326e465730555a524b59675532444564784638332b3177683974484b33385a666d5a50466b6c6a445550587871364d5a6341784952434f58546e654e564a514146654d4d62647a6a307852785a6d67736669724d4245656e5761736d557779597959646a77596443706a4863706249644d4e74786273746a4335707a372f37334b5048444b432b457874486d7173474c636f55747a337a662f4775394b6f6d646f666b506b4d586f4c65437742457167597347612f6131352f635070336d38356b496a2f54796230674259674b4678396b7a685a6f722f57764d3376425749463244696f4b4a646877574958566e57613561376c396b393730494952354f566d76376767425a4e72716a30414241414141416741414141414141414141414141414144376869794955722f6c77414e6c307a32522b6644522b6a365746414141414141414233683442415141414141414141414141414141414141414141414141414141414141414141414141414465646c6c737541414141414141414141414141414141646c6e4f4648304f63555255427a70643177413152434e4c61714141416741414141414141414141414141414147576f3848765a71466d4f47317473436f6a306435323842335a31414151414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d3d
    

    而预期正确结果是

    0x01000000030d00a7eaa53624fff6589804c653602cb2688ab2a37b67723506891b1a8c594651670946fed93c5cd1089fc3df7f79038865ca10046af10b6a40f6c2954f6da9d006010229ffc6b41deb082a0025bc420899fde4fd27c94c45c24edd686f82ecf7cdbe2709ac09515c807462919060cada97eac5833c96866b257c8458b5be4a79d43d1f00039e22b42b2d6736ddf1fb2576640862797157faf9b92def8f69a3c1ab69a983770cf3c27664ae0db20d5084224412846cd33af48967cb771a03830cbd90c66db601040f1eac5e6133486f45581ab3d7b0b44b4e8db4ac3e10959b46688e153aeb57ba600630ab7dd40226c05b7b480cd282cf9a2489ea29e034464cf2941c8fe6dd0b01068422ad124d066a6bf794e4d0aa6ce60d8e3acb2fffe566a22885e387bac7109721f0256f2533ec21bd1aa9f16d7c4a4b2d7ebdca3d45148363e1dac0457b847c0109e66e7b4410777bb53ce010dedd019d7235dcd316cff7586fd5deafd41deb43830342625d87fcdd2a7b36db6ac8ebe131b3cc9c43bda572b7cd0a5e2d748de955000a8012fbea260e5005efa05f664e01939e525375d59c050a0acfbaa99cc78f46e65ba92b87acf06726f8a3f864240caef1bbf2333eb68bdd032626552c896c2f91000c8e75c19d063023ff547f94a684c560c7f5221107b7da367e840bb48da611b2201388d01f69d1966939699ac5e017bf7da2516196283fc9d288f9967cb525509e000d71b882157149d4537be3112ede9dd812d48080193348e7d8dfb2d671ed1d016f0e278cdd2934cfbbd7c54afc83f857596e08bb32879d3ff8f88df6bfccd6790e010eeb3071f7853e225effeb38b1af446b4873b4e94aba1a07d13862b4e252bdd2e3191c2b6218390f5eb986ea1567412070c03d56b115646ccdb8566d1023e9cb7200100db69c55b451944a6205360c477117cdfed7087db472b7f197e664f1649630d43d7c6ae8c65c03121108e5d39de35525000578c31b7738f4c51c599a0b1f8ab30111e9d66ac994c3263261d8f061d0a98c77296c874c36dc5bb2d8c2e69cfbffbdca3c70ca0be131b479aab062dca14b73df37ff1aef4aa267687e43e4317a0b782c0112a818b066bf6b5e7f70fa779bce64223fd3c9bd2005880a171f64ce1668aff5af337bc15881760e2a0a25d8705885d59d66b96bb97d93def42084793959afee080164daea8f4001000000020000000000000000000000003ee18b2214aff97000d974cf647e7c347e8fa585000000000001de1e0101000000000000000000000000000000000000000000000000000000379d965b2e0000000000000000000000007659ce147d0e714454073a5dd7003544234b6aa0000200000000000000000000000065a8f07bd9a8598e1b5b6c0a88f4779dbc07767500040000000000000000000000000000000000000000000000000000000000000000
    

    我想着怎么用 python 来把那个原数据转变成我想要的这个。。js 实在不会啊。。哎。。

    4 条回复
    ma6254
        1
    ma6254  
       2023-08-15 16:56:43 +08:00   ❤️ 1
    应该是 base64 解码
    samohyes
        2
    samohyes  
    OP
       2023-08-15 16:58:48 +08:00
    @ma6254 感谢老哥
    ```
    Buffer.from("AQAAAAMNAKfqpTYk//ZYmATGU2AssmiKsqN7Z3I1BokbGoxZRlFnCUb+2Txc0Qifw99/eQOIZcoQBGrxC2pA9sKVT22p0AYBAin/xrQd6wgqACW8QgiZ/eT9J8lMRcJO3Whvguz3zb4nCawJUVyAdGKRkGDK2pfqxYM8loZrJXyEWLW+SnnUPR8AA54itCstZzbd8fsldmQIYnlxV/r5uS3vj2mjwatpqYN3DPPCdmSuDbINUIQiRBKEbNM69Ilny3caA4MMvZDGbbYBBA8erF5hM0hvRVgas9ewtEtOjbSsPhCVm0ZojhU661e6YAYwq33UAibAW3tIDNKCz5okieop4DRGTPKUHI/m3QsBBoQirRJNBmpr95Tk0Kps5g2OOssv/+VmoiiF44e6xxCXIfAlbyUz7CG9GqnxbXxKSy1+vco9RRSDY+HawEV7hHwBCeZue0QQd3u1POAQ3t0BnXI13NMWz/dYb9Xer9Qd60ODA0JiXYf83Sp7NttqyOvhMbPMnEO9pXK3zQpeLXSN6VUACoAS++omDlAF76BfZk4Bk55SU3XVnAUKCs+6qZzHj0bmW6krh6zwZyb4o/hkJAyu8bvyMz62i90DJiZVLIlsL5EADI51wZ0GMCP/VH+UpoTFYMf1IhEHt9o2foQLtI2mEbIgE4jQH2nRlmk5aZrF4Be/faJRYZYoP8nSiPmWfLUlUJ4ADXG4ghVxSdRTe+MRLt6d2BLUgIAZM0jn2N+y1nHtHQFvDieM3Sk0z7vXxUr8g/hXWW4IuzKHnT/4+I32v8zWeQ4BDuswcfeFPiJe/+s4sa9Ea0hztOlKuhoH0ThitOJSvdLjGRwrYhg5D165huoVZ0EgcMA9VrEVZGzNuFZtECPpy3IAEA22nFW0UZRKYgU2DEdxF83+1wh9tHK38ZfmZPFkljDUPXxq6MZcAxIRCOXTneNVJQAFeMMbdzj0xRxZmgsfirMBEenWasmUwyYyYdjwYdCpjHcpbIdMNtxbstjC5pz7/73KPHDKC+ExtHmqsGLcoUtz3zf/Gu9KomdofkPkMXoLeCwBEqgYsGa/a15/cPp3m85kIj/Tyb0gBYgKFx9kzhZor/WvM3vBWIF2DioKJdhwWIXVnWa5a7l9k970IIR5OVmv7ggBZNrqj0ABAAAAAgAAAAAAAAAAAAAAAD7hiyIUr/lwANl0z2R+fDR+j6WFAAAAAAAB3h4BAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADedllsuAAAAAAAAAAAAAAAAdlnOFH0OcURUBzpd1wA1RCNLaqAAAgAAAAAAAAAAAAAAAGWo8HvZqFmOG1tsCoj0d528B3Z1AAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",'base64').toString("hex")
    ```
    这样可以获得正确结果
    Freakr
        3
    Freakr  
       2023-08-15 16:59:18 +08:00   ❤️ 1
    base64.b64decode(a).hex()
    samohyes
        4
    samohyes  
    OP
       2023-08-15 17:01:03 +08:00
    @Freakr 感谢,没意识到是 base64, 省了我一大笔时间!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1236 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 19ms · UTC 18:18 · PVG 02:18 · LAX 10:18 · JFK 13:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.