Converting C++ to Perl

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Converting C++ to Perl

    I am trying to convert this snippet of c++ code to perl and am having
    some problems getting the results to match up. I was hoping someone
    might be able to help me out. Here is the snippet of C++ code:

    DWORD dwVolSerial;
    BOOL bIsRetrieved;
    char *tmp;
    char Data[6];
    // Get the serial number
    bIsRetrieved =
    GetVolumeInformation("C:\\",NULL,NULL,&dwVolSerial ,NULL,NULL,NULL,NULL);

    // Display the serial number
    printf("Serial number of drive C is %X\n",dwVolSerial);

    tmp = (char*)&dwVolSerial;

    Data[0] = tmp[0];
    Data[1] = tmp[1];
    Data[2] = tmp[2];

    printf("Data is %d-%d-%d\n",Data[0],Data[1],Data[2]);

    And here is what I tried in perl:

    #just hard coded volume serial number here
    $str = '303C-6B38';
    @pieces = split(/-/,$str);
    @data = split(//,$pieces[0]);
    printf("Data: %d-%d-%d\n",ord($data[0]),ord($data[1]),ord($data[2]));

    The C++ is run on Windows XP and the perl is run on Free-BSD. For the
    perl part I want the user to input the volume serial number of their C
    drive and I want to create a strign using this data. I think that the
    conversions are different between windows and unix and that is where
    my problem is but I am not sure. In windows the "3" becomes a "056"
    but in unix it becomes a "53". Any help would be apprecieated.
    Thanks.
    Kenny McCarty Guest

  2. Similar Questions and Discussions

    1. Off Topic: Active Perl Native Windows / cygwin perl
      I have both activestate windows native perl installed and the default cygwin perl. How can I have the cygwin shell use the windows perl rather...
    2. Control a non-perl image viewer from perl script
      Below is a (non-finished) script that trys to run a linux viewer called eog (eye of gnome) in a script that will eventually allow me to power thru...
    3. Re : Installing CPAN Perl Modules with Activestate Perl 5. v5.8
      Hi, In the process of trying to get perl modules installed, I downloaded over 300 Activestate specific perl modules and they work fine (of the ones...
    4. Effeciency question - perl scripts v's perl exe's
      Max Adams wrote: The only existing Perl compiler is part of perl. Anything else is just a packager that puts a perl, required modules, and the...
    5. ENV variables in perl scripts called from perl.
      The subject says it all. I am doing a: I have a perl script in which I am using a ReadParse routine that parses the $ENV{'QUERY_STRING'} or...
  3. #2

    Default Re: Converting C++ to Perl

    Kenny McCarty wrote:
    >
    > The C++ is run on Windows XP and the perl is run on Free-BSD. For the
    > perl part I want the user to input the volume serial number of their C
    > drive and I want to create a strign using this data. I think that the
    > conversions are different between windows and unix and that is where
    > my problem is but I am not sure. In windows the "3" becomes a "056"
    > but in unix it becomes a "53". Any help would be apprecieated.
    > Thanks.
    Are you sure about this ?
    Both 'C' and 'perl' report an ASCII value of 51 for '3' (on both Linux
    and Windows) for me - which is as I would expect. (Your perl script
    outputs '51-48-51'.)

    Hth.

    Cheers,
    Rob

    Sisyphus Guest

  4. #3

    Default Re: Converting C++ to Perl

    Kenny McCarty wrote:
    >
    > I am trying to convert this snippet of c++ code to perl and am having
    > some problems getting the results to match up. I was hoping someone
    > might be able to help me out. Here is the snippet of C++ code:
    >
    > DWORD dwVolSerial;
    DWORD is I believe an unsigned long (32 bit) integer.

    > BOOL bIsRetrieved;
    > char *tmp;
    > char Data[6];
    > // Get the serial number
    > bIsRetrieved =
    > GetVolumeInformation("C:\\",NULL,NULL,&dwVolSerial ,NULL,NULL,NULL,NULL);
    >
    > // Display the serial number
    > printf("Serial number of drive C is %X\n",dwVolSerial);
    >
    > tmp = (char*)&dwVolSerial;
    >
    > Data[0] = tmp[0];
    > Data[1] = tmp[1];
    > Data[2] = tmp[2];
    >
    > printf("Data is %d-%d-%d\n",Data[0],Data[1],Data[2]);
    >
    > And here is what I tried in perl:
    >
    > #just hard coded volume serial number here
    > $str = '303C-6B38';
    > @pieces = split(/-/,$str);
    > @data = split(//,$pieces[0]);
    > printf("Data: %d-%d-%d\n",ord($data[0]),ord($data[1]),ord($data[2]));
    The equivalent perl code is:

    my $str = '303C-6B38';
    my @pieces = map hex, $str =~ /[[:xdigit:]]{2}/g;
    printf "Data: %d-%d-%d\n", @pieces[ 0 .. 2 ];

    > The C++ is run on Windows XP and the perl is run on Free-BSD. For the
    > perl part I want the user to input the volume serial number of their C
    > drive and I want to create a strign using this data. I think that the
    > conversions are different between windows and unix and that is where
    > my problem is but I am not sure. In windows the "3" becomes a "056"
    > but in unix it becomes a "53". Any help would be apprecieated.
    Are both OSs running on Intel compatible CPUs? Intel's little-endian
    format means that an integer like 0x12345678 converted to a char array
    will be { 0x78, 0x56, 0x34, 0x12 } so your volume serial number
    '303C-6B38' is really the integer 0x386B3C30.



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  5. #4

    Default Re: Converting C++ to Perl

    Sisyphus <kalinabears@iinet.net.au> wrote in message news:<3f5ec426$0$23610$5a62ac22@freenews.iinet.net .au>...
    > Kenny McCarty wrote:
    >
    > >
    > > The C++ is run on Windows XP and the perl is run on Free-BSD. For the
    > > perl part I want the user to input the volume serial number of their C
    > > drive and I want to create a strign using this data. I think that the
    > > conversions are different between windows and unix and that is where
    > > my problem is but I am not sure. In windows the "3" becomes a "056"
    > > but in unix it becomes a "53". Any help would be apprecieated.
    > > Thanks.
    >
    > Are you sure about this ?
    > Both 'C' and 'perl' report an ASCII value of 51 for '3' (on both Linux
    > and Windows) for me - which is as I would expect. (Your perl script
    > outputs '51-48-51'.)
    >
    > Hth.
    >
    > Cheers,
    > Rob
    Yes I am sure....however I am not sure if the C code is actually
    outputting the ascii values or something else...therein lies my
    dilema. I am VERY rusty with C coding and I am trying to convert this
    to perl from someone else's C code. So maybe someone who is familiar
    with C and perl can figure this out :-)
    Kenny McCarty Guest

  6. #5

    Default Re: Converting C++ to Perl

    Kenny McCarty wrote:
    >>
    >>Are you sure about this ?
    >>Both 'C' and 'perl' report an ASCII value of 51 for '3' (on both Linux
    >>and Windows) for me - which is as I would expect. (Your perl script
    >>outputs '51-48-51'.)
    >>
    >>Hth.
    >>
    >>Cheers,
    >>Rob
    >
    >
    > Yes I am sure....however I am not sure if the C code is actually
    > outputting the ascii values or something else...therein lies my
    > dilema. I am VERY rusty with C coding and I am trying to convert this
    > to perl from someone else's C code. So maybe someone who is familiar
    > with C and perl can figure this out :-)
    Not sure where to go from here. Did John Krahn's post shed any light on
    the problem for you ?

    Does it help to know that the serial number can be parsed from the
    output of the 'dir' windows system command (so long as it's run in the
    appropriate drive, of course) ?

    eg the following (transcribed) code works for me on Win2k, though ymmv
    on different windows boxes.

    my $d = `dir`;
    my @stuff = split /\n/, $d;
    $stuff[1] =~ s/ Volume Serial Number is //i;
    print $stuff[1]; # prints serial number of current drive.

    Cheers,
    Rob

    Sisyphus Guest

  7. #6

    Default Re: Converting C++ to Perl

    "John W. Krahn" <krahnj@acm.org> wrote in message news:<3F5EF329.F46A314F@acm.org>...
    > Kenny McCarty wrote:
    > >
    > > I am trying to convert this snippet of c++ code to perl and am having
    > > some problems getting the results to match up. I was hoping someone
    > > might be able to help me out. Here is the snippet of C++ code:
    > >
    > > DWORD dwVolSerial;
    >
    > DWORD is I believe an unsigned long (32 bit) integer.
    >
    >
    > > BOOL bIsRetrieved;
    > > char *tmp;
    > > char Data[6];
    > > // Get the serial number
    > > bIsRetrieved =
    > > GetVolumeInformation("C:\\",NULL,NULL,&dwVolSerial ,NULL,NULL,NULL,NULL);
    > >
    > > // Display the serial number
    > > printf("Serial number of drive C is %X\n",dwVolSerial);
    > >
    > > tmp = (char*)&dwVolSerial;
    > >
    > > Data[0] = tmp[0];
    > > Data[1] = tmp[1];
    > > Data[2] = tmp[2];
    > >
    > > printf("Data is %d-%d-%d\n",Data[0],Data[1],Data[2]);
    > >
    > > And here is what I tried in perl:
    > >
    > > #just hard coded volume serial number here
    > > $str = '303C-6B38';
    > > @pieces = split(/-/,$str);
    > > @data = split(//,$pieces[0]);
    > > printf("Data: %d-%d-%d\n",ord($data[0]),ord($data[1]),ord($data[2]));
    >
    > The equivalent perl code is:
    >
    > my $str = '303C-6B38';
    > my @pieces = map hex, $str =~ /[[:xdigit:]]{2}/g;
    > printf "Data: %d-%d-%d\n", @pieces[ 0 .. 2 ];
    >
    >
    > > The C++ is run on Windows XP and the perl is run on Free-BSD. For the
    > > perl part I want the user to input the volume serial number of their C
    > > drive and I want to create a strign using this data. I think that the
    > > conversions are different between windows and unix and that is where
    > > my problem is but I am not sure. In windows the "3" becomes a "056"
    > > but in unix it becomes a "53". Any help would be apprecieated.
    >
    > Are both OSs running on Intel compatible CPUs? Intel's little-endian
    > format means that an integer like 0x12345678 converted to a char array
    > will be { 0x78, 0x56, 0x34, 0x12 } so your volume serial number
    > '303C-6B38' is really the integer 0x386B3C30.
    >
    >
    >
    > John
    John,

    Thanks so much...you code did the trick. It just prints out the
    numbers in reverse but I can deal with that. Guess it has to do with
    the little-endian/big-endian stuff you said. I vaguely remember
    learning about that in college hehe. Thanks again for your help and
    everyone else who tried to help too.
    Kenny McCarty Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139