Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zed2

macrumors 6502a
Original poster
Jul 17, 2004
606
59
Bucks
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
 
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
 
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
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.