0
1. "20220924020521"
2. 20220924020521
3. 0x12640CFE4729
4.        00010010      01100100     00001100    11111110   01000111  00101001
5. 000100       1001100      1000000     1100111    1111001   0001110  0101001
6. 0x044C4067790E29
7. Binary string of (6)

I need to convert a string from (1) to (7) in PHP, let me explain.

(1) is converted to an long integer(2) and then to its hex equivalent(3) but it is encoded in 8 bits(4). I need to then encode it to 7 bits(5) and then back to a binary string(7).

Is there a built-in function to convert from 1 to 7 in PHP?

This isn't homework, I'm trying to convert a date for the FAST protocol of FIX.

My question is about whether there is a low code way to do this.

Thank you.

Grant Birchmeier
  • 17,809
  • 11
  • 63
  • 98
bilogic
  • 449
  • 3
  • 18
  • 3
    The point about being a homework question isn't really relevant. The main thing people expect is that you make some effort in solving the problem yourself before asking others to help. – Nigel Ren Sep 24 '22 at 07:14
  • Do you have difficulty in all six conversions? – shingo Sep 24 '22 at 07:21
  • No, I have no trouble writing all the code, but I want to know if there is some function in PHP that can jump from 1 to 7 or as far as I can without writing every step. – bilogic Sep 24 '22 at 07:23
  • I don't think PHP itself has such a function unless this kind of conversion ([base128?](https://github.com/seizu/base128/blob/master/base128.php)) is widely used. – shingo Sep 24 '22 at 07:32
  • My current way is to `decbin` and meddle with the binary string before reaching (7) – bilogic Sep 24 '22 at 07:36
  • Step 3 is completely unnecessary, and I’m suspect on 6 and 7. Hex is generally used for debugging/visualizing numbers, if you were to transport it it would always be as a string and now we’re back to encoding problems. But I know nothing about Fix. As noted, I don’t think PHP has anything to support this, but here’s a long-form version that matches your output just for fun: https://3v4l.org/mPOvM. – Chris Haas Sep 24 '22 at 14:36
  • Thanks, I like that `str_split`. I just need to convert (1) to (7), of course I was just trying to be explicit in case some have trouble following. – bilogic Sep 24 '22 at 17:57

0 Answers0