![]() ![]() These correspond to C's unsigned char data type. You can convert a character like A to its corresponding ASCII value 65 by just storing it into a numeric data type like byte, int, or long as shown below. ![]() The ord function can return numbers from 0 to 255. Here's how to convert from HAL to IBM: $hal = unpack("C*", $hal) Here are pack and unpack in action: = unpack("C*", "sample") The pack, unpack, chr, and ord functions are all faster than sprintf. printf("Number %d is character %c\n", 101, 101) The %c format forces printf or sprintf to convert a number into a character it's not used for printing a character that's already in character format (that is, a string). If you already have a character, it's really represented as a string of length one, so just print it out directly using print or the %s format in printf and sprintf. Perl provides Pascal's chr and ord to convert between a character and its corresponding ordinal value: $ascii_value = ord("e") # now 101 That means you can't just assign characters and numbers back and forth. Unlike low-level, typeless languages like assembler, Perl doesn't treat characters and numbers interchangeably it treats strings and numbers interchangeably.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |