2

How can I convert a binary number into a string character using Perl script?

4
  • 1
    A binary what into what form of string? Give an example of what you have and what you want. Commented May 19, 2009 at 6:12
  • 1
    Please specify the type of string representation you want. Hex digits? Decimal digits? Commented May 19, 2009 at 6:15
  • You are still not getting across what you have and what you want it turned into. Give a concrete example or we will spend all of our time guessing what you want. Commented May 19, 2009 at 6:29
  • Any sort of response would be great...
    – user36457
    Commented May 19, 2009 at 8:42

3 Answers 3

8

If you mean binary to ASCII like this webpage, this should do the trick:

#!/usr/bin/perl

$binarySample = "01010100011001010111001101110100"; # "Test" in binary
$chars = length($binarySample);
@packArray = pack("B$chars",$binarySample);
print "@packArray\n";

output:

Test
1

chr(0x41) or chr(65) turns the number 65 (41 in hex) into the letter "A", is this what you are looking for?

0

Strings can contain either binary data or text characters; nothing special is needed.

Tell us more about what you are trying to do, and that might shed some light on what you mean by "convert" or "binary".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.