亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 關(guān)于,對(duì)不足8位的在后面補(bǔ)二進(jìn)制的0 [打印本頁]

作者: ralind    時(shí)間: 2004-08-25 12:39
標(biāo)題: 關(guān)于,對(duì)不足8位的在后面補(bǔ)二進(jìn)制的0
如字符:45045  如何在后面補(bǔ)二進(jìn)制的0 并且成為8位數(shù),
請(qǐng)問如何實(shí)現(xiàn)?

thx all!
作者: 夜貓子    時(shí)間: 2004-08-25 12:46
標(biāo)題: 關(guān)于,對(duì)不足8位的在后面補(bǔ)二進(jìn)制的0

  1. function FillBit($source, $length, $fill = '0', $lor = 'l')
  2. {
  3.     $source_len = strlen($source);

  4.     if ( ($length - $source_len) >; 0 ){
  5.         if ( $lor == 'l' ){
  6.             $result = str_repeat($fill, $length - $source_len) . $source;
  7.         }else{
  8.             $result = $source . str_repeat($fill, $length - $source_len);
  9.         }
  10.     }else{
  11.         $result = $source;
  12.     }

  13.     return $result;
  14. }

  15. $result = FillBit($source, 8, '0', 'r');
復(fù)制代碼

作者: ralind    時(shí)間: 2004-08-25 13:09
標(biāo)題: 關(guān)于,對(duì)不足8位的在后面補(bǔ)二進(jìn)制的0
謝謝!原來直接在后面加0就可以了?
作者: HonestQiao    時(shí)間: 2004-08-25 15:43
標(biāo)題: 關(guān)于,對(duì)不足8位的在后面補(bǔ)二進(jìn)制的0
你的這個(gè)太簡(jiǎn)單了哦:
  1. <?php
  2. $strSource = "45045";
  3. $strDist = sprintf("%-08s",$strSource);//-表示在后面補(bǔ),0表示補(bǔ)0,8表示程度為8,s表示字符串
  4. echo $strDist;
  5. ?>;
復(fù)制代碼


真是受不了阿,居然不看幫助文檔不官方手冊(cè):
http://w.yi.org/ftp/FAPM/PHP/zh-php4/function.php-sprintf.htm

  1. sprintf
  2. 將字符串格式化。

  3. 語法: string sprintf(string format, mixed [args]...);

  4. 返回值: 字符串

  5. 函數(shù)種類: 資料處理




  6. 內(nèi)容說明


  7. 本函數(shù)用來將字符串格式化。參數(shù) format 是轉(zhuǎn)換的格式,以百分比符號(hào) % 開始到轉(zhuǎn)換字符為止。而在轉(zhuǎn)換的格式間依序包括了


  8. 填空字符。0 的話表示空格填 0;空格是默認(rèn)值,表示空格就放著。
  9. 對(duì)齊方式。默認(rèn)值為向右對(duì)齊,負(fù)號(hào)表向左對(duì)齊。
  10. 字段寬度。為最小寬度。
  11. 精確度。指在小數(shù)點(diǎn)后的浮點(diǎn)數(shù)位數(shù)。
  12. 類型,見下表 % 印出百分比符號(hào),不轉(zhuǎn)換。
  13. b 整數(shù)轉(zhuǎn)成二進(jìn)位。
  14. c 整數(shù)轉(zhuǎn)成對(duì)應(yīng)的 ASCII 字符。
  15. d 整數(shù)轉(zhuǎn)成十進(jìn)位。
  16. f 倍精確度數(shù)字轉(zhuǎn)成浮點(diǎn)數(shù)。
  17. o 整數(shù)轉(zhuǎn)成八進(jìn)位。
  18. s 整數(shù)轉(zhuǎn)成字符串。
  19. x 整數(shù)轉(zhuǎn)成小寫十六進(jìn)位。
  20. X 整數(shù)轉(zhuǎn)成大寫十六進(jìn)位。




  21. 使用范例


  22. <?
  23. $money1 = 68.75;
  24. $money2 = 54.35;
  25. $money = $money1 + $money2;
  26. // 此時(shí)變量 $money 值為 "123.1";
  27. $formatted = sprintf ("%01.2f", $money);
  28. // 此時(shí)變量 $ formatted 值為 "123.10"
  29. ?>;
復(fù)制代碼

作者: lekj    時(shí)間: 2004-08-25 16:05
標(biāo)題: 關(guān)于,對(duì)不足8位的在后面補(bǔ)二進(jìn)制的0
45045 Or 11110000
作者: csona    時(shí)間: 2004-08-25 17:23
標(biāo)題: 關(guān)于,對(duì)不足8位的在后面補(bǔ)二進(jìn)制的0
我也加一個(gè)

str_pad
(PHP 4 >;= 4.0.1)

str_pad --  Pad a string to a certain length with another string
Description
string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])


This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.

Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.

If the value of pad_length is negative or less than the length of the input string, no padding takes place.

例子 1. str_pad() example

  1. <?php
  2. $input = "Alien";
  3. echo str_pad($input, 10);                      // produces "Alien     "
  4. echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"
  5. echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"
  6. echo str_pad($input, 6 , "___");               // produces "Alien_"
  7. ?>;  
復(fù)制代碼



注: The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length.

手冊(cè)上都有
作者: csona    時(shí)間: 2004-08-25 17:26
標(biāo)題: 關(guān)于,對(duì)不足8位的在后面補(bǔ)二進(jìn)制的0
str_pad
(PHP 4 >;= 4.0.1)

str_pad --  Pad a string to a certain length with another string
Description
string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])


This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.

Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.

If the value of pad_length is negative or less than the length of the input string, no padding takes place.

例子 1. str_pad() example

  1. <?php
  2. $input = "Alien";
  3. echo str_pad($input, 10);                      // produces "Alien     "
  4. echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"
  5. echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"
  6. echo str_pad($input, 6 , "___");               // produces "Alien_"
  7. ?>;  
復(fù)制代碼



注: The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length.




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2