2 Replies - 1544 Views - Last Post: 03 May 2012 - 05:11 PM

#1 evolivid  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 24-July 08

Pack missing last character

Posted 03 May 2012 - 04:11 PM

Ok I need to turn a string into a number

and when I try to do this

$a2 = "why does this not work"; 

print $a2,"\n";
 @a3 = unpack "N*", $a2;
 print @a3,"\n";
for $t (@a3) {
 @a4= pack "N*", @a3;
 
 }
print @a4,"\n";



so when I do that I should get
why does this not work
instead I get

why does this not wo

can any one help with this ???

Is This A Good Question/Topic? 0
  • +

Replies To: Pack missing last character

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Pack missing last character

Posted 03 May 2012 - 04:25 PM

N* is a list of longs. Since one long consists of 4 bytes, the given string's size must be a multiple of 4. In this case it's not, so unpack simply ignore the 2 trailing characters at the end. If you pad it to a multiple of 4 by adding two spaces at the end (or null-characters if you don't want to affect the value of the last long), you'll get the same string back that you put in.

PS: You don't need to perform line 7 in a loop. It will have the same effect each time you execute it (the value of @a3 doesn't change in the loop).

PS: This has nothing to do with your problem, but in general it's considered good practice to use strict and declare all your variables (and also to enable warnings).

This post has been edited by sepp2k: 03 May 2012 - 04:27 PM

Was This Post Helpful? 2
  • +
  • -

#3 evolivid  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 24-July 08

Re: Pack missing last character

Posted 03 May 2012 - 05:11 PM

that does seem to give me a solution Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1