Hi,
I'm having a Perl Code and i want to convert it into a C Code.
I want to know is there any tool is available to do so?
If not available anybony can help me to convert??:?:
plz mail me at
Regards,
Jeganathan K.
Hi,
I'm having a Perl Code and i want to convert it into a C Code.
I want to know is there any tool is available to do so?
If not available anybony can help me to convert??:?:
plz mail me at
Regards,
Jeganathan K.
— following your request and ’s pointer to an automated route, a manual port will be simpler to understand and easier to debug for this particular binary parser. The input uses a tiny byte-level protocol with four kinds of tokens: (1) a pen up/down+color token (high bits match 0x80 with low bits holding state/color), (2) 0x88 followed by a timestamp byte, (3) 0x90 followed by a layer byte, and (4) coordinate packets where the MSB is clear and the parser consumes three more bytes to form two 14-bit coordinates (x = b1 + (b2<<7), y = b3 + (b4<<7)). Treat the stream as raw bytes and avoid Perl-style character splitting when you move to C.
Practical porting notes: open the file in binary mode and read into a single buffer (use fseek/ftell or a streaming fread loop). Use unsigned types (uint8_t, uint16_t) so bitwise ops are deterministic. Walk the buffer with a single index, checking bounds before consuming extra bytes. Replace die and print OUT with fprintf(stderr, ...) and fprintf(out, ...). See fread documentation and the fixed-width types at stdint reference for details.
A compact parsing skeleton (illustrates masks and bounds checks):
/* assume data and len are set; out is open FILE* */
size_t pos = 0;
uint8_t color = 0, lastcolor = 0, layer = 0;
const char *colors[] = {"0 0 0","1 0 0","0 1 0","0 0 1"};
const char *pointcmd = "S";
while (pos < len) {
uint8_t next = data[pos++];
if ((next & 0xF8) == 0x80) { /* pen up/down + color */
bool down = next & 0x01;
pointcmd = down ? "S" : "L\nE\n";
color = ((next >> 1) + layer * colorlayers) & 0x03;
if (color != lastcolor) {
lastcolor = color;
fprintf(out, "%%Color: %u\n%s RGB\n", color, colors[color]);
}
} else if (next == 0x88) { /* timestamp */
if (pos >= len) { /* error */ }
uint8_t ts = data[pos++];
if (ts < 0x7f) fprintf(out, "%%TimeStamp: %u\n", ts);
} else if (next == 0x90) { /* end of layer */
if (pos >= len) { /* error */ }
layer = data[pos++];
if (layer & 0x80) { /* error */ }
fprintf(out, "%%EndOfLayer: %u\n\n", layer);
} else if ((next & 0x80) == 0) { /* coordinates */
if (pos + 2 >= len) { /* error */ }
uint16_t b1 = next, b2 = data[pos++], b3 = data[pos++], b4 = data[pos++];
uint16_t x = b1 + (b2 << 7), y = b3 + (b4 << 7);
fprintf(out, "%u %u %s\n", x, y, pointcmd);
pointcmd = "L";
} else {
fprintf(stderr, "unknown byte 0x%02x at %zu\n", next, pos-1);
exit(1);
}
} Notes: define colorlayers, initialize pointcmd as in your Perl, and test with small known inputs first. Use tools like valgrind to catch overreads.
Jump to Post— masijade 1,351perlcc that comes with the perl distribution
perlcc that comes with the perl distribution
Can anyone convert this perl code into C Code???:!: :!:
open(IN, "$inname");
# read rest of input file in 64K junks
while(read(IN, $buffer, 64*1024))
{
$data .= $buffer;
}
close(IN);
my @data = split(//, $data);
my $color = 0;
my $lastcolor = 0;
my $layer = 0;
my $strokes = 0;
my $points = 0;
my $pointcmd = "S";
while($count < $#data) {
my $next = ord($data[$count++]);
if (($next & ~0x07) == 0x80)
{ # pen up/down
if ($next & 0x01)
{
$pointcmd = "S";
print "DEBUG pen down POSITION $count\n" if ($debug);
}
else
{
$pointcmd = "L\nE\n";
$strokes++;
print "DEBUG pen up POSITION $count\n" if ($debug);
}
$color = (($next >> 1) + ($layer * $colorlayers)) & 0x03;
print "DEBUG pen color $color\n" if ($debug);
if ($color != $lastcolor)
{
$lastcolor = $color;
print OUT "%%Color: $color\n";
print OUT ("0 0 0", "1 0 0", "0 1 0", "0 0 1")[$color] . " RGB\n";
}
}
elsif ($next == 0x88) { # time stamp
my $timestamp = ord($data[$count++]);
print "DEBUG TimeStamp $timestamp\n" if ($debug);
print OUT "%%TimeStamp: $timestamp\n" if ($timestamp < 0x7f);
}
elsif ($next == 0x90) { ## end of layer
$layer = ord($data[$count++]);
print "DEBUG End of LAYER $layer\n" if ($debug);
die("Layer # $layer > 127") if ($layer & 0x80);
print OUT "%%EndOfLayer: $layer\n\n";
}
elsif (!($next & ~0x7f)) { ## coordinates
my ($b1, $b2, $b3, $b4) = ($next, ord($data[$count+0]), ord($data[$count+1]), ord($data[$count+2]));
$count += 3;
printf "DEBUG CHECK position $count A %02x %02x %02x %02x\n", $b1, $b2, $b3, $b4 if ($debug);
# die("MSB set in coordinate bytes") if (($b1 | $$b2 | $b3 | $b4) & ~0x7f);
my $x = $b1 + ($b2 << 7);
my $y = $b3 + ($b4 << 7);
print "DEBUG $count: x = $x / y = $y\n" if ($debug);
print OUT "$x $y " . $pointcmd . "\n";
$points++;
$pointcmd = "L";
} else {
die("unknown byte " . sprintf("0x%02x", $next) . " at position $count");
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.