免费注册 查看新帖 |

Chinaunix

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

perl中字节与字符串如何转换 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-06 23:27 |只看该作者 |倒序浏览
         本人是学java的 ,perl小菜鸟,但非常喜欢perl。
         有个问题一直困扰小弟,java中字节 与字符串的转换相当简单,但perl的让我看起来不知如何转换,貌似用pack和unpack可以做到
比如 我打算按字节读文件,然后打印到控制台,怎么实现呢,假设一次读取1024个字节, 文件是文本文件。java中是这样的
  1. String file = "C:/Users/ke/Desktop/a.pl";
  2.                 FileInputStream in = new FileInputStream(file);
  3.                 byte[]buf = new byte[1024];
  4.                 int len;
  5.                 while((len = in.read(buf))>0){
  6.                         String s = new String(buf,0,len);
  7.                         System.out.print(s);
  8.                 }
  9.                 in.close();
复制代码
相同的作法,perl如何写呢,望高人指点,务必写的详细点,谢谢

论坛徽章:
0
2 [报告]
发表于 2012-08-07 07:45 |只看该作者
本帖最后由 hrb_kid 于 2012-08-07 10:11 编辑

在*nix/bsd上可以这样。

  1. #!/usr/bin/perl -w
  2. use strict;

  3. my ( $file, $handle, $buffer ) = '/foo/bar/baz';

  4. open $handle, '<', $file or die "open: $!\n";
  5. print $buffer while read $handle, $buffer, 1024;
  6. close $handle;
复制代码
当然这只是个简单应用,假设的文件内容是ascii。如果有unicode,则要为已经打开的文件句柄开启binmode。(详见perldoc)
  1. binmode $handle, ':utf8';  
  2. binmode STDOUT, ':utf8';
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-08-07 09:19 |只看该作者
回复 2# hrb_kid
read函数貌似是读字符的,你上面的代码还是用字符来读的,没涉及到字节,是不是应该用sysread

   

论坛徽章:
0
7 [报告]
发表于 2012-08-07 17:33 |只看该作者
本帖最后由 zk1878 于 2012-08-07 17:35 编辑

         这个帖子可能提问得不太好,java中字节与字符串的概念比较清晰, 出于这两个语言的对比,所以想问下而已,并非不知道如何读取打印文本文件。
         另外我发这个帖子的目的是, 我想用socket构建http请求,来下载一个文件,比如图片,mp3之类的,这样基于字符串的方式就不行了,因为
服务器响应除了http头部信息外,内容是二进制的。 像这种情况,通过socket发送http请求怎么实现呢,我看过java的实现,是先获取请求头Content-Length,然后在读取http响应正文中Content-Length 长度的字节,这Content-Length 长度的字节就是要下载的文件,   所以才想问问perl中字节的问题

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
9 [报告]
发表于 2013-05-09 10:48 |只看该作者
@zk1878这个问题解决了么

论坛徽章:
0
10 [报告]
发表于 2013-05-14 09:58 |只看该作者
回复 9# 只是一个红薯


    用sysread来按字节读取了,先解析http的响应头部,解析出content-length的值,然后再进一步操作, 具体代码没写,应该是这个思路

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
4 [报告]
发表于 2012-08-07 09:27 |只看该作者
回复 1# zk1878

1. default use Line in 文本文件, easy and simple

2. what do you want to do
   How to read is not important in normal case
   

论坛徽章:
0
5 [报告]
发表于 2012-08-07 10:00 |只看该作者
本帖最后由 hrb_kid 于 2012-08-07 10:13 编辑

回复 3# zk1878

“貌似“的判断根据是什么?读过read的perldoc么?

       read FILEHANDLE,SCALAR,LENGTH,OFFSET
       read FILEHANDLE,SCALAR,LENGTH
               Attempts to read LENGTH characters of data into variable SCALAR
               from the specified FILEHANDLE.  Returns the number of charac-
               ters actually read, 0 at end of file, or undef if there was an
               error (in the latter case $! is also set).  SCALAR will be
               grown or shrunk so that the last character actually read is the
               last character of the scalar after the read.

               An OFFSET may be specified to place the read data at some place
               in the string other than the beginning.  A negative OFFSET
               specifies placement at that many characters counting backwards
               from the end of the string.  A positive OFFSET greater than the
               length of SCALAR results in the string being padded to the
               required size with "\0" bytes before the result of the read is
               appended.

               The call is actually implemented in terms of either Perl’s or
               system’s fread() call.  To get a true read(2) system call, see
               "sysread".

               Note the characters: depending on the status of the filehandle,
               either (8-bit) bytes or characters are read.  By default all
               filehandles operate on bytes
, but for example if the filehandle
               has been opened with the ":utf8" I/O layer (see "open", and the
               "open" pragma, open), the I/O will operate on UTF-8 encoded
               Unicode characters, not bytes.  Similarly for the ":encoding"
               pragma: in that case pretty much any characters can be read.

另外,sysread是系统指令。原则上讲如对同一文件句柄进行系统操作,则都要是系统操作,最好不要混用。
open,seek,read,print,close对应的系统操作是sysopen,sysseek,sysread,syswrite,sysclose。

论坛徽章:
2
CU大牛徽章
日期:2013-04-17 11:46:28CU大牛徽章
日期:2013-04-17 11:46:39
6 [报告]
发表于 2012-08-07 10:22 |只看该作者
回复 3# zk1878


    请教一下:你的代码中字节与字符区别?

论坛徽章:
2
CU大牛徽章
日期:2013-04-17 11:46:28CU大牛徽章
日期:2013-04-17 11:46:39
8 [报告]
发表于 2012-08-07 19:06 |只看该作者
html 貌似文本文件
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP