-1

This is hex c4 and dec is 196

When string length is 50 one of my project returning \xc4
If length is 51 then getting hex \xc5
length 55 = \xc9
length 56 = \xca
length 61 = \xcf
length 62 = \xd0
Continuously \xd9 then \xda to continuously \xdf then changing xd to xe again from \xe0 to \xe9 and \xea to \xef

How it possible to get length 50 to hex c4 or dec 196?

I can convert dec to bin and bin to hex using below code:

$binary = decbin(50); //dec to binary
echo dechex(bindec($binary)); //binary to hex

1 Answers1

0

I think you're possibly overthinking this: forget about the hexadecimal for a while, and note that you've already discovered that you need to convert 50 to 196. The simplest way to do that is simply to add 146. So dechex(50 + 146) gives you the value you need.

The rest of the sequence is, as I think you've already worked out, just hexadecimal values going upwards from there, so from what you've said, there's nothing more clever to do than add that offset to each value. Why that offset? I have no idea, because I have no idea what you're using this for.

Meanwhile, converting to binary and back isn't doing anything - it's like writing a word backwards, and then writing it back the right way again. You can just pass the number into dechex directly.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • plz show me example, what you telling me to do – Edward Dulhunty Jan 06 '22 at 15:43
  • I'm not telling you to do anything very complicated - add two numbers, and then pass the result to a function. I'm not going to write the code for you, because then you'll copy and paste it and still not understand, and I'm more interested in teaching you how to think like a programmer. – IMSoP Jan 06 '22 at 15:51
  • your `146` technic working from `109`. Getting good result. but from `110` its returning extra `1` – Edward Dulhunty Jan 06 '22 at 15:56
  • `xf4` placed by `$this->text` length. Now just 99 length string supporting. – Edward Dulhunty Jan 06 '22 at 16:01
  • `@A__ Interesting that 50 decimal in binary is 0011 0010 - reversing each nibble one gets 1100 0100 which is hex c4. Love a good puzzle.` How? – Edward Dulhunty Jan 06 '22 at 16:18
  • I've answered the question you posted. I don't have access to wherever you're seeing these numbers, so can't really help beyond that. However, remember that 255 is hexadecimal FF, so the highest number you can store in one byte; if you're viewing some data one byte at a time, you'll never see a value higher than that. – IMSoP Jan 06 '22 at 16:31