Range control dec to hex with spesific range - php

I'm working with a slider control. I'm trying to do a decimal to hex or not exactly, it is a regular number range without a decimal point. And for reference:
0 is zero
00 is 100
50 is 50
(meaning the numbers are always 1 or 2 digits only on the slider control.)
(and the hex also needs to be only 2 digits.)
I know about dechex. Is that what I would use for this? Or if its not a number with a decimal point is there another command other than dechex for this.
And how do I set the range to only manage the control from 0 to 00?
I am learning. I don't have a clue how to write the php so any and all comments might help. Thanks for the help! Any questions let me know too.

<?php
function convert($s) {
print "CONVERT: $s\n";
$s = "X".$s;
switch($s) {
case "X00":
return dechex(100);
break;
case "X0":
return null;
break;
default:
return dechex(substr($s,1));
break;
}
}
print convert('0')."\n";
print convert('1')."\n";
print convert('50')."\n";
print convert('99')."\n";
print convert('00')."\n";
output:
CONVERT: 0
CONVERT: 1
1
CONVERT: 50
32
CONVERT: 99
63
CONVERT: 00
64
PHP does do some automatich type conversion, and does not know the difference between "0" en "00", that's why I am adding an "X" in front of it to know the difference (and I am comparing strings).
P.S.: some changes might need to be done to meet your exact expectations...

Related

number management with 16 decimals

I need to use numbers with 16 decimal places in a project and do math operations with them, but I have problems with their handling.
Whatever it does in php, these are truncated if they are too long.
such as if:
$value = 123456789.1234567890;
Printing it with "echo" the result I have on the screen is this: 123456789.12346
Even if the number has few elements in the integer part, the decimal part is truncated
$value = 0.12345678901234567890;
In output I have:
0.12345678901235
What can I do to manage them easily?
Thanks for your help.
I tried using the instruction suggested by GuidoFaecke:
ini_set('precision', '16')
Or:
ini_set('precision', '-1');
But when I use -1 I don't see decimal number. Using 16 I don't see changing showing the number.

How to get int to dec and dec to hex using php?

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
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.

Why strlen doesn't work in a paticular digit

I made this function. It seemed it's working but when it comes to 20 digits number, the return value was 19. I'm wondering why this problem happen..
My function
function sumDigits($n) {
return strlen($n);
}
echo sumDigits(100); //3
echo sumDigits(1000); //4
echo sumDigits(12345); //5
echo sumDigits(1000000000); //10
echo sumDigits(145874589632); //12
echo sumDigits(0); //1
echo sumDigits(12345698745254856320); //19 <-- Why not 20?
Can you please somebody explain for me?
Thank you so much.
First, I would point out that the name of your function is misleading, as you are not really summing the values of the digits, but are counting the digits. So I would call your function countDigits instead of sumDigits.
The reason why it doesn't work for large numbers, is that the string representation will switch to scientific notation, so you're actually getting the length of "1.2345698745255E+19" not of "12345698745254856320"
If you are only interested in integers, you will get better results with the logarithm:
function countDigits($n) {
return ceil(log10($n));
}
For numbers that have decimals, there is no good solution, since the precision of 64-bit floating pointing point numbers is limited to about 16 significant digits, so even if you provide more digits, the trailing decimals will be dropped -- this has nothing to do with your function, but with the precision of the number itself. For instance, you'll find that these two literals are equal:
if (1.123456789123456789123456789 == 1.12345678912345678) echo "equal";
Because you function parameter is an integer, exceeding the limit.
If you dump it, it actually shows the following:
1.2345698745255E+19 - which is 19 letters.
If you would do the following, it will return 20 - mind the quotes, which declares the input as string.
echo sumDigits("12345698745254856320"); //19 <-- Why not 20? -> now will be 20
As per documentation, strlen() expects a string so a cast happens. With default settings you get 1.2345698745255E+19:
var_dump((string)12345698745254856320);
string(19) "1.2345698745255E+19"
The root issue is that PHP converts your integer literal to float because it exceeds PHP_INT_MAX so it cannot be represented as integer:
var_dump(12345698745254856320, PHP_INT_MAX);
In 64-bit PHP:
float(1.2345698745254857E+19)
int(9223372036854775807)
You could change display settings to avoid E notation but you've already lost precision at this point.
Computer languages that store integers as a fixed amount of bytes do not allow arbitrary precision. Your best chance is to switch to strings:
var_dump('12345698745254856320', strlen('12345698745254856320'));
string(20) "12345698745254856320"
int(20)
... and optionally use an arbitrary precision library such as BCMath or GMP if you need actual maths.
It's also important to consider that this kind of issues is sometimes a symptom that your input data is not really meant to be an integer but just a very long digit-only string.

PHP Find the first scientificly significant 'non zero' digits of an floating number

In this question, I am using the word significant in a scientific context
I want to retrieve the first n digits from the left of a floating number. The function should take in:
the $number which is the floating point number.
The $n which is the amount of numbers to retrieve.
Here are some example inputs and outputs that I would like the function to return:
When $number= 5678 and $n=2 then -> result should be: 56
When $number= -69869 and $n=4 then -> result should be: 6986
When $number= 0.00676 and $n=2 then -> result should be: 67
When $number= -0.0000048 and $n= 3 then ->result should be: 480
I tried some math functions to achieve this, and got pretty far, but the last one gives problems in
$result = (int)(abs($number)/pow(10,(int)(log10(abs($number)))-$n));
It returns 479 as answer. This is because I am working with floating points, and I don’t know a way around this. Anyone?
You can do it with strings. Is this an option?
$result = (int)str_pad(substr((int)str_replace('.','',preg_replace('/E‌​(.*)/','',abs($numbe‌​r))),0,$n),$n,'0',ST‌​R_PAD_RIGHT);

How to create string from index in php

I have to pass an index to a function and from that index return a string containing from 1 to 4 chars.
I have:
$string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
So if I say index(20) the function should return lower "k" because it is its index in the $string variable.
But...
If I type indexes
62 it should return 10
63 .. 11
That is because from index 62 it should start the string from 1, not 0, and loop until the next index, so they will be 2 length string
If I type indexes
3843 .. ZZ
That is because from index 3843 all the possibilities from [0-9a-zA-Z][0-9a-zA-Z] have ended and now the string starts with 3 length.
3844 .. 100
...
9999 .. 2Bh
All the possibilities and at [0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z] but I only need until index 9999
Your question is a bit hard to understand, not sure if this is what you want:
$string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function t($n)
{
global $string;
$ret = $string{$n%62};
if($n>=62)
$ret = t(floor($n/62)).$ret;
return $ret;
}
echo t(9999);
What you're asking for is basically a number base converter, going from base 10 (decimal) numbers to base 64 (using your string as the 64 digits in counting system).
PHP does provide a built-in base_convert() function, however while it works well for converting between say hexadecimal and decimal, it doesn't work for very high bases like this. This is partly because no-one generally has any need for them, and partly because the digits of such a base are not an agreed standard.
There are several examples in the comments of the base_convert() page linked above where people have written functions that do attempt to work for high bases. I can't vouch for any of them though.

Categories