1

Yesterday I have been writing simple program in perl.

It is icq bot, you write a message as the math expression and it calculates the value.

The problem is that incoming message has not-single-byte encoding and when it is writing to file there are a lot of bad symbols and of course calc can't handle this file.

how can I convert incoming message to ASCII?

Here is the source:

#!/usr/bin/perl

use Net::OSCAR;
use Encode;
use strict;

my ($UIN, $PASSWORD, $oscar, $t, $msg);

$UIN='675349295';
$PASSWORD='passwd';

$oscar = Net::OSCAR->new();
$oscar->set_callback_im_in(\&send_answer);
$t = 0;

while (1)
{
    if (!$oscar->is_on && (time() - $t) > 120)
    {
        $oscar->signon($UIN, $PASSWORD);
        $t=time();
    }

    $oscar->do_one_loop();
}

sub send_answer()
{
    my($oscar, $sender, $msg) = @_;

    if ($msg eq "quit")
    {
        $oscar->signoff();
        exit();
    }

    open(my($fh), '>', '/tmp/msg');
    print $fh "$msg";
    close $fh;

    my($ans)=`calc -p -f /tmp/msg`;
    $oscar->send_im($sender, $ans);
}
oybek
  • 79
  • 4
  • What is an example of the input you're trying to parse? – Sobrique Nov 23 '14 at 19:43
  • it is message sent by mobile phone. I redirect incoming messages to file and this is file and print it content byte by byte: how file look in editor: AA what I got: 65 0 65 0 There is null byte after each byte – oybek Nov 24 '14 at 06:12
  • I think you'll need to do more research here. When you say 'not-single-byte encoding', that sounds like UTF-8 (or something similar), but the example you gave is not that. Figure out if you need to decode the string, or just remove null bytes from it. – Arnon Weinberg Nov 25 '14 at 02:25

0 Answers0