免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2099 | 回复: 6
打印 上一主题 下一主题

关于,对不足8位的在后面补二进制的0 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-08-25 12:39 |只看该作者 |倒序浏览
如字符:45045  如何在后面补二进制的0 并且成为8位数,
请问如何实现?

thx all!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-08-25 12:46 |只看该作者

关于,对不足8位的在后面补二进制的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');
复制代码

论坛徽章:
0
3 [报告]
发表于 2004-08-25 13:09 |只看该作者

关于,对不足8位的在后面补二进制的0

谢谢!原来直接在后面加0就可以了?

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
4 [报告]
发表于 2004-08-25 15:43 |只看该作者

关于,对不足8位的在后面补二进制的0

你的这个太简单了哦:
  1. <?php
  2. $strSource = "45045";
  3. $strDist = sprintf("%-08s",$strSource);//-表示在后面补,0表示补0,8表示程度为8,s表示字符串
  4. echo $strDist;
  5. ?>;
复制代码


真是受不了阿,居然不看帮助文档不官方手册:
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. 函数种类: 资料处理




  6. 内容说明


  7. 本函数用来将字符串格式化。参数 format 是转换的格式,以百分比符号 % 开始到转换字符为止。而在转换的格式间依序包括了


  8. 填空字符。0 的话表示空格填 0;空格是默认值,表示空格就放着。
  9. 对齐方式。默认值为向右对齐,负号表向左对齐。
  10. 字段宽度。为最小宽度。
  11. 精确度。指在小数点后的浮点数位数。
  12. 类型,见下表 % 印出百分比符号,不转换。
  13. b 整数转成二进位。
  14. c 整数转成对应的 ASCII 字符。
  15. d 整数转成十进位。
  16. f 倍精确度数字转成浮点数。
  17. o 整数转成八进位。
  18. s 整数转成字符串。
  19. x 整数转成小写十六进位。
  20. X 整数转成大写十六进位。




  21. 使用范例


  22. <?
  23. $money1 = 68.75;
  24. $money2 = 54.35;
  25. $money = $money1 + $money2;
  26. // 此时变量 $money 值为 "123.1";
  27. $formatted = sprintf ("%01.2f", $money);
  28. // 此时变量 $ formatted 值为 "123.10"
  29. ?>;
复制代码

论坛徽章:
0
5 [报告]
发表于 2004-08-25 16:05 |只看该作者

关于,对不足8位的在后面补二进制的0

45045 Or 11110000

论坛徽章:
0
6 [报告]
发表于 2004-08-25 17:23 |只看该作者

关于,对不足8位的在后面补二进制的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. ?>;  
复制代码



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

手册上都有

论坛徽章:
0
7 [报告]
发表于 2004-08-25 17:26 |只看该作者

关于,对不足8位的在后面补二进制的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. ?>;  
复制代码



注: The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP