Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

C to Java string conversion script

by perlmoth (Hermit)
on Dec 01, 2001 at 21:03 UTC ( #128884=perlquestion: print w/replies, xml ) Need Help??

perlmoth has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a script to convert source code. I want to convert lines containing things like "anStr" into {(byte)'a',(byte)'n',(byte)'S',(byte)'t',(byte)'r',}, and I came up with the following:

#!/usr/bin/perl -w use strict; use diagnostics; sub jc { my ($c_str) = shift; my $j_str = ""; foreach (split //, $c_str) { $j_str = $j_str . "(byte)\'$_\',"; } return $j_str; } while (<>) { s/\"(.+)\"/"{".&jc($1)."}"/e; print; }

This works (except for an extra comma at the end). Is there a better way to do this?

Replies are listed 'Best First'.
Re: C to Java string conversion script
by VSarkiss (Monsignor) on Dec 01, 2001 at 21:55 UTC

    Hmm... Not sure why you'd want this, but anyway. Here's a one-liner that does it, but broken out for clarity:

    while (<>) { s|"([^"]+)"| '{' . join(',', map { "(byte)'$_'" } split(//, $1) ) . '}' |gex; print; }
    Main difference is that this will match many quoted strings on a line. Like your example, it won't cope with "escaped \" quotes".


Re: C to Java string conversion script
by Fastolfe (Vicar) on Dec 01, 2001 at 22:07 UTC
    This looks like the weirdest set of requirements I've seen in a while. Maybe there is an easier way of accomplishing your goals? Not all strings in Java have to be written like (byte)'t',(byte)'h',(byte)'i',(byte)'s'.
      It's for smart card code. As far as I know this is the only way to use strings on smart cards.
Re: C to Java string conversion script
by impossiblerobot (Deacon) on Dec 01, 2001 at 21:44 UTC
    This is the quickest thing I could come up with. (I hope it works!)
    #!/usr/bin/perl -w use strict; print jc('anStr'); sub jc { my ($c_str) = shift; return '{'.join(',',(map {"(byte)'$_'"} (split(//, $c_str)))).'}'; }

    Update: My last update didn't make much sense, so I un-updated.

    Impossible Robot
Re: C to Java string conversion script
by chipmunk (Parson) on Dec 02, 2001 at 02:22 UTC
    This does the same thing, but with map and join rather than foreach and .= s/"(.+)"/'{' . join(',', map "(byte)'$_'", split m!!, $1) . '}'/e;
Re: C to Java string conversion script
by converter (Priest) on Dec 02, 2001 at 08:12 UTC

    This ought to work:

    $_ = "anStr"; { local $"; $s = qq!{@{[map{"(byte)'$_',"}split//]}}!; }


Re: C to Java string conversion script
by atcroft (Abbot) on Dec 02, 2001 at 10:22 UTC
    I am still but an egg myself, but here was what I came up with (including my testing code). Did you want a comma after the last character, as in your posting, or was that a typo?
    #!/usr/bin/perl -- -w use strict; use diagnostics; my @test = ( '"this is a test"', '"anStr"' ); foreach my $item (@test) { print($item, "\n", &jc($item), "\n"); } sub jc { my ($line) = shift; my (@parts, $i); @parts = split('', $line); $parts[0] = '{'; for ($i = 1; $i < $#parts - 1; $i++) { $parts[$i] =~ s/(.)/(byte)'$1',/; } $parts[$#parts - 1] =~ s/(.)/(byte)'$1'/; $parts[$#parts] = '}'; return(join('', @parts)); }
    UPDATE: In the spirit of "there's more than one way to do it", a (hopefully) cleaner version of the jc() routine than above.
    sub jc { my ($line) = shift; my (@parts, $i); @parts= split('', $line); (undef) = shift(@parts); (undef) = pop(@parts); for ($i = 0; $i <= $#parts; $i++) { $parts[$i] =~ s/(.)/(byte)'$1'/; } return('{' . join(',', @parts) . '}'); }
    I considered using a two-pass loop performing a pop() then reverse() on the array to remove the quotes on either end, but I think that would have been more work on the computer.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://128884]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2020-02-26 16:11 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (117 votes). Check out past polls.