十進位(decimal system)轉換函式說明,decimalsystem_PHP教程

來源:互聯網
上載者:User

十進位(decimal system)轉換函式說明,decimalsystem


一,十進位(decimal system)轉換函式說明
1,十進位轉二進位 decbin() 函數,如下執行個體

echo decbin(12); //輸出 1100
echo decbin(26); //輸出 11010
decbin
(PHP 3, PHP 4, PHP 5)
decbin -- 十進位轉換為二進位
說明
string decbin ( int number )
返回一字串,包含有給定 number 參數的二進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 32 個 1 的字串。

2,十進位轉八進位 decoct() 函數

echo decoct(15); //輸出 17
echo decoct(264); //輸出 410
decoct
(PHP 3, PHP 4, PHP 5)
decoct -- 十進位轉換為八進位
說明
string decoct ( int number )
返回一字串,包含有給定 number 參數的八進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 "37777777777"。

3,十進位轉十六進位 dechex() 函數

echo dechex(10); //輸出 a
echo dechex(47); //輸出 2f
dechex
(PHP 3, PHP 4, PHP 5)
dechex -- 十進位轉換為十六進位
說明
string dechex ( int number )
返回一字串,包含有給定 number 參數的十六進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 "ffffffff"。

二,二進位(binary system)轉換函式說明
1,二進位轉十六制進 bin2hex() 函數

$binary = "11111001";
$hex = dechex(bindec($binary));
echo $hex;//輸出f9
bin2hex
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
bin2hex -- 將位元據轉換成十六進位表示
說明
string bin2hex ( string str )
返回 ASCII 字串,為參數 str 的十六進位表示。轉換使用位元組方式,高四位位元組優先。

2,二進位轉十制進 bindec() 函數

echo bindec('110011'); //輸出 51
echo bindec('000110011'); //輸出 51
echo bindec('111'); //輸出 7
bindec
(PHP 3, PHP 4, PHP 5)
bindec -- 二進位轉換為十進位
說明
number bindec ( string binary_string )
返回 binary_string 參數所表示的位元的十進位等價值。
bindec() 將一個位元轉換成 integer。可轉換的最大的數為 31 位 1 或者說十進位的 2147483647。PHP 4.1.0 開始,該函數可以處理大數值,這種情況下,它會返回 float 類型。

三,八進位(octal system)轉換函式說明
八進位轉十進位 octdec() 函數

echo octdec('77'); //輸出 63
echo octdec(decoct(45)); //輸出 45
octdec
(PHP 3, PHP 4, PHP 5)
octdec -- 八進位轉換為十進位
說明
number octdec ( string octal_string )
返回 octal_string 參數所表示的八位元的十進位等值。可轉換的最大的數值為 17777777777 或十進位的 2147483647。PHP 4.1.0 開始,該函數可以處理大數字,這種情況下,它會返回 float 類型。

四,十六進位(hexadecimal)轉換函式說明
十六進位轉十進位 hexdec()函數

var_dump(hexdec("See"));
var_dump(hexdec("ee"));
// both print "int(238)"

var_dump(hexdec("that")); // print "int(10)"
var_dump(hexdec("a0")); // print "int(160)"
hexdec
(PHP 3, PHP 4, PHP 5)
hexdec -- 十六進位轉換為十進位
說明
number hexdec ( string hex_string )
返回與 hex_string 參數所表示的十六進位數等值的的十進位數。hexdec() 將一個十六進位字串轉換為十進位數。所能轉換的最大數值為 7fffffff,即十進位的 2147483647。PHP 4.1.0 開始,該函數可以處理大數字,這種情況下,它會返回 float 類型。
hexdec() 將遇到的所有非十六進位字元替換成 0。這樣,所有左邊的零都被忽略,但右邊的零會計入值中。

五,任意進位轉換 base_convert() 函數

$hexadecimal = 'A37334';
echo base_convert($hexadecimal, 16, 2);//輸出 101000110111001100110100
base_convert
(PHP 3 >= 3.0.6, PHP 4, PHP 5)

base_convert -- 在任意進位之間轉換數字
說明
string base_convert ( string number, int frombase, int tobase )
返回一字串,包含 number 以 tobase 進位的表示。number 本身的進位由 frombase 指定。frombase 和 tobase 都只能在 2 和 36 之間(包括 2 和 36)。高於十進位的數字用字母 a-z 表示,例如 a 表示 10,b 表示 11 以及 z 表示 35。

這裡主要是把PHP進位轉換函式進行整理,便於開發尋找,相關具體函數說明請參考PHP手冊。請關注下一期中文字元編碼研究系列。


將十進位數轉為十六進位數的JS或PHP代碼

JS十進位轉其他進位代碼如下var m = 10;document.write(m.toString(2) + "
"); // 顯示為 1010 二進位document.write(m.toString(8) + "
"); // 顯示為 12 8進位document.write(m.toString(10) + "
"); // 顯示為 10 十進位document.write(m.toString(16) + "
"); // 顯示為 a, 十六進位 php轉換函式如下:bindec() — 二進位轉換為十進位
decbin() — 十進位轉換為二進位
dechex() — 十進位轉換為十六進位
decoct() — 十進位轉換為八進位
hexdec() — 十六進位轉換為十進位
octdec() — 八進位轉換為十進位
base_convert()– 在任意進位之間轉換數字使用說明如下: 一,十進位(decimal system)轉換函式說明
1,十進位轉二進位 decbin() 函數,如下執行個體echo decbin(12); //輸出 1100
echo decbin(26); //輸出 11010
decbin
(PHP 3, PHP 4, PHP 5)
decbin -- 十進位轉換為二進位
說明
string decbin ( int number )
返回一字串,包含有給定 number 參數的二進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 32 個 1 的字串。2,十進位轉八進位 decoct() 函數echo decoct(15); //輸出 17
echo decoct(264); //輸出 410
decoct
(PHP 3, PHP 4, PHP 5)
decoct -- 十進位轉換為八進位
說明
string decoct ( int number )
返回一字串,包含有給定 number 參數的八進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 "37777777777"。3,十進位轉十六進位 dechex() 函數echo dechex(10); //輸出 a
echo dechex(47); //輸出 2f
dechex
(PHP 3, PHP 4, PHP 5)
dechex -- 十進位轉換為十六進位
說明
string dechex ( int number )
返回一字串,包含有給定 number 參數的十六進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 "ffffffff"。二,二進位(binary system)轉換函式說明
1,二進位轉十六制進 bin2hex() 函數$binary = "11111001";
$hex = dechex(bindec($binary));
echo $hex;//輸出f9
bin2hex
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
bin2hex -- 將......餘下全文>>
 

進位轉換的函數的問題

package com.test.numsys;

import java.util.*;

/**
* 數值轉換 十進位 二進位 八進位 十六進位
*/
public class NumSys {

public static void main(String[] args) {

NumSys ns = new NumSys();
System.out.println("十進位100轉換為二進位為:" + ns.decimalToBinary(100));//1100100

System.out.println("二進位1100100轉換為八進位為: " + ns.binaryToOctal(1100100));// 144

System.out.println("二進位10110000011101轉換為十六進位為:" + ns.binaryToHex("10110000011101")); //2C1D

System.out.println("十進位100轉換為八進位:" + ns.DecimalToOctal(100));

System.out.println("十進位15400轉換為十六進位:" + ns.DecimalToHex(15400));
}

//沒有實現小數部分的轉化 十進位轉換為二進位
public long decimalToBinary(int a) {

long binary = 0L;
int[] binaryArr = new int[64];
int i = 0;
do {
binaryArr[i] = a%2;
a = a/2;
i ++;
} while(a != 0);

String s = "";
for(int j = binaryArr.length-1; j>=0; j--) {
s+=binaryArr[j] + "";
}

binary = Integer.parseInt(s);

return binary;
}

//二進位轉換為八進位
public String binaryToOctal(long a) {
String octal = "";
String s = a +"";
char [] c_temp = (a+"").toCharArray();

int temp = 3 - c_temp.length%3;
for(int i = 0; i s = "0" + s;
}

char[] c2 = s.toCharArray();
List list = new ArrayList();

for(int i = 0 ; i < c2.length ; i += 3 ) {
String stemp = c......餘下全文>>
 

http://www.bkjia.com/PHPjc/900986.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/900986.htmlTechArticle十進位(decimal system)轉換函式說明,decimalsystem 一,十進位(decimal system)轉換函式說明 1,十進位轉二進位 decbin() 函數,如下執行個體 echo...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.