Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

help neeed in unpack

by uva (Sexton)
on Mar 14, 2006 at 11:15 UTC ( [id://536518]=perlquestion: print w/replies, xml ) Need Help??

uva has asked for the wisdom of the Perl Monks concerning the following question:

dear monk,
i stored some 4 chinese characters in utf8 format file.i opened the file with the file handle INPUT,
$chinese=<INPUT>; print unpack("A4",$chinese);
even i used decoding like,
print decode("utf8",unpack("A4",$chinese));
i can retrive only one chinese characters why??

Replies are listed 'Best First'.
Re: help neeed in unpack
by thundergnat (Deacon) on Mar 14, 2006 at 15:08 UTC

    It isn't really clear what you are trying to accomplish with unpack so it is difficult to tell what your problem is.

    Under pack/unpack, 'A' only works with ASCII strings, so that is unlikely to be what you want. 'U' returns the ordinal of a utf-8 character. That may be useful to you, I can't tell from your question.

    It seems like you are just trying to get the first 4 characters from the line, in which case, unpack is probably not the best way to go. You may be better of using substr

    use warnings; use strict; my $infile = 'D:/try_unicode/input.txt'; # or whatever my $outfile = 'D:/try_unicode/output.doc'; open my $input, '<:utf8', $infile or die "Couldn't open file: $!"; open my $output, '>:utf8', $outfile or die "Couldn't open file: $!"; while (my $line = <$input>) { chomp $line; print $output 'The first four utf-8 chars from the line: ', pack( "U4", unpack( "U4", $line ) ),"\n"; print $output 'Or, an easier way to get the same thing: ', substr $line, 0, 4; print $output "\n", '-' x 79, "\n"; }
Re: help neeed in unpack
by wazoox (Prior) on Mar 14, 2006 at 11:28 UTC
    The line
    $chinese=<INPUT>;
    grabs only one line from the file. If you wrote 1 character per line, well, you'll just get 1 character.
      that file contains the sequence of four chinese characters in a line.it is getting the four characters , i have problem in unpack operation.
      i tried the following code.but still problem
      the input.txt contains 4 chinese characters in a line.
      use utf8; use Encode; open INPUT,"<:utf8","D:\\try_unicode\\input.txt" or die "Couldn't open + file: $!"; close STDOUT; open STDOUT ,">:utf8","D:\\try_unicode\\output.doc" or die "Couldn't o +pen file: $!"; $unpacking=<INPUT>; chomp($unpacking); print "\nunpacking1 :" ,decode("utf8",unpack("x4A4",$unpacking)),"\n +"; print "\nunpacking2 :" ,unpack("U4A4",$unpacking),"\n"; close INPUT; close STDOUT;
      i used U4 still its giving some sequence of numbers.
      i trien to x4 to null byte the four chinese characters its not working .
      i tried using A4 also its not working.
        I don't understand what you're trying to do with "unpack" here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://536518]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2025-01-13 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (29 votes). Check out past polls.