Advertisement
markuszeller

String as Number

Nov 4th, 2020 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. // https://stackoverflow.com/questions/64676862/how-to-encode-decode-string-to-decimal-number-in-php
  2. // As suggested in the comments, you can put it into hex and that into its numeric representation.
  3.  
  4. $list = array(
  5.     "string0",
  6.     "string1",
  7.     "string2",
  8.     "string3",
  9.     "string4",
  10.     "string5",
  11. );
  12.  
  13. function numberEncode($value){
  14.     return hexdec(bin2hex($value));
  15. }
  16.  
  17. function numberDecode($value){
  18.     return hex2bin(dechex($value));
  19. }
  20.  
  21. $list2 = array();
  22. foreach($list as $value){
  23.     $list2[] = numberEncode($value);
  24. }
  25.  
  26. print_r($list2);
  27.  
  28. $list3 = array();
  29. foreach($list2 as $value){
  30.     $list3[] = numberDecode($value);
  31. }
  32.  
  33. print_r($list3);
  34.  
  35. /*
  36. Array
  37. (
  38.     [0] => 32497657065662256
  39.     [1] => 32497657065662257
  40.     [2] => 32497657065662258
  41.     [3] => 32497657065662259
  42.     [4] => 32497657065662260
  43.     [5] => 32497657065662261
  44. )
  45. Array
  46. (
  47.     [0] => string0
  48.     [1] => string1
  49.     [2] => string2
  50.     [3] => string3
  51.     [4] => string4
  52.     [5] => string5
  53. )
  54. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement