"Sputnik" help  
Sputnik Help
Ascii(<value>)

Description

Casts a value as a string using ASCII encoding, where each character is represented by a single byte.

Parameters

<value> The value to be cast as a string.

Return Value

Success: Returns a new string containing only ASCII.
Failure: Returns an empty string.

Remarks

The Ascii function allows you to convert a value into a string using the ASCII encoding. In ASCII encoding, each character is represented by a single byte, allowing you to work with individual characters as numeric values.This will not preserve Unicode characters.

The Ascii function can be useful for various purposes, such as reprocessing a string and comparing it to the original to check if the original string is already in ASCII format or to verify if it can be converted properly. By converting a string to its ASCII representation, you can perform comparisons, validations, or transformations based on the ASCII values of individual characters.

For example, you can compare the ASCII representation of a string to the original string to determine if the original string contains any non-ASCII characters. This can be helpful when working with input validation or data processing scenarios where ASCII compatibility is required.

Related

Casting, String, IsAscii

Example

Example of usage:

// ASCII is fine
say Ascii("Hello World");

// Unicode is not handled
say Ascii("こんにちは");

// PRINTS
// Hello World
// ?????

Example that demonstrates using the Ascii function to compare the ASCII representation of a string with the original string:

CheckAscii("Hello, こんにちは!");
CheckAscii("Hello, World!");

Function CheckAscii($str)
{
    say "Checking $str";
    my $asciiString = Ascii($str);
    // Check if the original string is already in ASCII format
    if ($str eq $asciiString)
        say "The original string is in ASCII format.";
    else
        say "The original string is not in ASCII format.";
    // Perform further processing based on the ASCII representation
    if ($asciiString =~ m/^[A-Za-z0-9!.,\s]+$/)
        say "The ASCII representation can be properly converted.";
    else
        say "The ASCII representation cannot be properly converted.";
}

// PRINTS
// Checking Hello, こんにちは!
// The original string is not in ASCII format.
// The ASCII representation cannot be properly converted.
// Checking Hello, World!
// The original string is in ASCII format.
// The ASCII representation can be properly converted.

Casting:

say (Ascii)"Hello World";
// PRINTS
// Hello World

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org