MySQL equivalent of PHP's base_convert? - php

Does anyone know of an equivalent function in MySQL of PHP's base_convert?
http://php.net/manual/en/function.base-convert.php
Thanks,
Mark

CONV()

you could use CONV(N,from_base,to_base)

CONV(N,from_base,to_base)
Converts numbers between different number bases. Returns a string representation of the number N, converted from base from_base to base to_base. Returns NULL if any argument is NULL. The argument N is interpreted as an integer, but may be specified as an integer or a string. The minimum base is 2 and the maximum base is 36. If from_base is a negative number, N is regarded as a signed number. Otherwise, N is treated as unsigned. CONV() works with 64-bit precision.
http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_conv

Related

What does actually mean by the statement "PHP does not support unsigned integers"?

I am using PHP 7.0.2
At one place in the manual on integers, I saw the below statement :
Integers can be specified in decimal (base 10), hexadecimal (base 16),
octal (base 8) or binary (base 2) notation, optionally preceded by a
sign (- or +).
and at one place in the manual I saw below contradictory statement :
PHP does not support unsigned integers.
Due to these two sentences I got confused. At one place it's saying that an integer can optionally be preceded by a sign which means whenever I use any integer, preceding it with a sign(- or +) or not will be my choice.
And at the same time it's saying that PHP does not support unsigned integers.
So, does it say that whenever I use any integer without preceding it with a sign(+ or -) won't it be considered as a legal integer in PHP?
To clarify this, just as an integer and a float are different data types, an unsigned integer is actually a different data type to an integer. A standard integer is usually 32 bits (this can vary between different programming languages) and can be from approx -2 billion to +2 billion, while an unsigned integer can be from 0 to approx +4 billion. You can also have smaller and larger integers, which have lower and higher max values, and can also be unsigned, keeping the same number of distinct options but shifting to only positive.
This is relevant for things like database key fields, where an unsigned integer is ideal for autoincrement identity type fields, but will not be properly supported in php. It gets complicated, so if you're just using php don't stress over it and if you're using php with a database provider it's probably best to avoid using unsigned integers in your data as php will either break or start returning negative values different to the positive value that's actually stored.
It's the sort of annoyance that makes my eye twitch a little.
That simply means, that each integer is signed and you can't create unsigned integers. So if you want to use something bigger than PHP_INT_MAX, you have to use strings (or some library like bcmath) to come around this.
The + prefix to state a positive integer is optional, positive integers can be indicated by no sign or a +. Negative integers can only be indicated by a -.
Extending the examples from the integer guide.
$a = 123; // decimal number (123)
$a = +123; // decimal number (123)
$a = -123; // a negative decimal number (-123)
$a = 0123; // octal number (equivalent to 83 decimal)
$a = +0123; // octal number (equivalent to 83 decimal)
$a = -0123; // octal number (equivalent to -83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = +0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = -0x1A; // hexadecimal number (equivalent to -26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = +0b11111111; // binary number (equivalent to 255 decimal)
$a = -0b11111111; // binary number (equivalent to -255 decimal)

How to find last 8 digit of number 0000548795846

I want to find last 8 digit of number 0000548795846 in php.
Its done fine when I use string but I have some problem with Integer starts with zero.
Try with -
substr(" 0000548795846", -1, 8);
In your example what is happening is that, PHP by default considers the number starting with 0 (zero) as octal number i.e base 8, and you are expecting it as decimal.
When PHP comes across any number starting with zero it converts it into decimal equivalent of the actual number for ex. 0000548795846 gets converted into 44 decimal, so there is no length >= 8 present.
So, Solution is to convert the number in string format. Then use substr() with appropriate arguments, as suggsted by "sgt"

php scientific notation significant digits

In the PHP manual for function sprintf I find:
e - the argument is treated as scientific notation (e.g. 1.2e+2). The
precision specifier stands for the number of digits after the decimal
point since PHP 5.2.1. In earlier versions, it was taken as number of
significant digits (one less).
Now I'm on PHP 5.4, but would like to format a number with scientific notation and indicate the number of significant digits instead of decimal places. Is there a function for this, or should I write it myself?
I looked over the (one less) part, that's all I needed.
thanks for the help!
This means that I just had to decrease the value of the precision by one to get my function to behave the way I wanted.
See here for the function I came up with tot support scientific notation using sprintf
function sn($number,$precision = 1,$case = 'e'){ //scientific notation
$precision--;
$string = str_replace('+','',sprintf('%.'.$precision.$case, $number))." ";
return $string;
}

Casting zero padded numeric strings to integers

I am padding integers for barcodes with leading zeros so they have same number of characters, for example:
1 -> 00000001
12 -> 00000012
1044 -> 00001044
00000001 is numeric and when casting to an integer it is 1.
Will this work as expected for all integers?
Careful, numbers starting with 0 are treated as base 8 in PHP
>> var_dump(011);
int(9)
however explicit casting string to int seems to be safe
>> var_dump((int)'011');
int(11)
You did not say anything where you're going to be storing these numbers, but in case it will be a database, here's some advice regarding datatype:
Barcode numbers are not (despite the name) numbers. Same goes for fax numbers, social security numbers, etc. You should not store these as numeric data (for example using MySQL's INT or DECIMAL) datatype. Instead use textual datypes (like CHAR or VARCHAR)
Unless you treat it as a string PHP will interpret 00000001 as simply the integer 1. If you want to treat your barcodes as a string, take care to cast them correctly because PHP might interpret them as integers due to type juggling.
Will this work as expected for all ints?
That depends on what you expect. Left-padding with zeros will never change the value. Regardless of the number, you can add any number of zeros to the left of the string and it will always be numeric, and it will always cast to the same integer.
(int)"01" == 1
(int)"0001" == 1
(int)"000000001" == 1
etc.

int variable with leading zero?

Why is it that following results in 34?
It doesn't seem to have anything to do with octal numbers.
intval(042);
but a leading 0 does indicate octal in many languages, as is the case here.
It does have to do with octal numbers, 042 is interpreted as the octal number 42 which is 4 * 8 + 2 = 34.
Please be aware that the octal interpretation happens when the number literal is parsed while loading the PHP script. It has nothing to do with intval(), which doesn't do anything here because the value is already integer.
Octal interpretation happens only with number literals, not when casting a string to integer:
intval(042) // == 34
intval('042') // == 42
(int)'042' // == 42
In PHP a number with leading 0 is read as an octal number.
So 042 is read as an octal number.
The intval() function converts it into decimal number, which is 34.
So the browser output is 34.
It's simply how the function is defined. The leading zero is an instruction parse it as an octal number, similarly as to how 0x as a prefix means hex. See the documentation for more information.
Be careful when passing this function a string value with a leading "0". If you give it "042" then, it will treat it as BASE 8 - 9 and convert it to decimal value, which is by default base.
Please go through this

Categories