perl pack function in objective-C

Discussion in 'Mac Programming' started by zed2, Jan 18, 2010.

  1. zed2 macrumors 6502a

    Joined:
    Jul 17, 2004
    Location:
    Bucks
    #1
    I'm trying to convert a perl script into objective-C

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $rsuser = 'tester';
    my $rspass = '12345';

    my $login = pack("N", length($rsuser));
    $login .= $rsuser;
    $login .= pack("N", length($rspass));
    $login .= $rspass;
    $login .= pack("N", 3);
    $login .= 'XXX';
    $login = pack("x7") . pack("N", length($login)) . $login;
    my $length = pack("N", length($login));

    print $stream $length.$login."\n";

    this makes heavy use of the perls Pack function. Can anyone provide insight into how I might be able to rewrite this code in Objective-C.

    I also need to use perls unpack too..

    Thanks
    ---Guy
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Don't say "I want perl's pack function in Objective-C", because you aren't going to get it. Perl's pack has a ton of specifiers for how to interpret the data, etc. If you want to archive binary data to a file on disk, or for network transmission, etc. say so. There are many options. If you have some other goal in mind, tell us what it is. No one is going to write you a full pack/unpack implementation that matches perl, because similar things already exist. Simply using fread/fwrite will handle straight binary reads/writes. If you want to get a textual encoding of binary data, there's always Base64.

    Again, more robust responses will be forthcoming if you are specific about your goal rather than trying to bend a perl-ism into Objective-C.

    -Lee
     
  3. zed2 thread starter macrumors 6502a

    Joined:
    Jul 17, 2004
    Location:
    Bucks
    #3
    Well basically I need to convert the script into objective-C.. so I need to "An unsigned long (32-bit) in "network" (big-endian) order."

    This is indeed to transmit over a network stream to a server which I didn't write.

    Thanks
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Either google "man htonl" or just type that at your terminal prompt. There may be code in cocoa to do this, too, but this is the C way, which you can always use in Objective-C.

    -Lee
     

Share This Page