--- Prabhu Gurumurthy <[EMAIL PROTECTED]> wrote: > Hi All, > > I have a C program which i want to convert it into perl > In the C program it is given like this > > typedef unsigned short u_int16; > #define dataLen 4 > #define keyLen 52 > #define userkeyLen 8 > #define DataT(v) u_int16 v [dataLen] > #define keyT(v) u_int16 v [keyLen] > #define UserKeyT(v) u_int16 v [userKeyLen] > > void function(DataT(dataIn), DataT(dataOut), keyT(key)); > > how can i convert the above lines into perl scripts,
Prabhu, Without seeing more of the program, it would be difficult to tell you how to convert it. A good first test, when trying to convert C to Perl is to run the C through the C preprocessor. With gcc, you can do this: gcc -E someprog.c > someprog.txt Then, at the bottom of the someprog.txt file is your actual program, after the preprocessor has done its work. Your code above generates the following: typedef unsigned short u_int16; void function(u_int16 dataIn [4], u_int16 dataOut [4], u_int16 key [52]); My C is a bit (well, a lot) rusty, but it appears that you are defined u_int16 as a new type definition of unsigned short. That means that you are passing function() three arrays of unsigned shorts (hmmm... that sounded a bit odd). By specifying a length for the array, you're setting aside that amount of memory (I think). Since the function is void, it's not returning anything. I seem to recall that passing arrays like that in C will actually be passing a reference to said array, so any effect that function is going to have outside of itself is going to modify the passed arguments directly. Anyone with better knowledge of C than myself, feel free to speak up! To achieve a similar effect in Perl, you'd pass in arrays by reference: function( \@dataIn, \@dataOut, \@key ); sub function { my ($dataIn, $dataOut, $key) = @_; ... } The three scalars in function() are actually array references and changing them will change the called arrays. > i thought that getting > the datalen from the input would solve the problem but how to get the > datalen of that variable in PERL? To get the length of any scalar in Perl, use the length function. $ perl -e '$x="abcde";print length $x' 5 > Also is there a way that i can convert the datalen into binary format > is yes how? What do you mean "binary format"? See perldoc for 'pack' and 'unpack'. Also, go to http://www.perlmonks.org, type "Super Search" in the box in the upper left corner and start searching for examples people have posted. Chapter 3 in Programming Perl also has more information about how to do this. > This is second question: > > I know that if i want to XOR two data > then i have to use "^" > I have a PERL script which is like this > > print "Enter number: "; > chomp($number = <STDIN>); > print "Enter another number: "; > chomp($num = <STDIN>); > > $value = $number ^ $num; > print "the value is $value\n; > > is there any mistake in the above script Looks fine to me: $ perl -e "print 5^2" 7 $ perl -e "print 4^2" 6 $ perl -e "print 3^2" 1 Cheers, Curtis "Ovid" Poe ===== "Ovid" on http://www.perlmonks.org/ Someone asked me how to count to 10 in Perl: push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//; shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A __________________________________________________ Do You Yahoo!? Check out Yahoo! Shopping and Yahoo! Auctions for all of your unique holiday gifts! Buy at http://shopping.yahoo.com or bid at http://auctions.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]