Asked  7 Months ago    Answers:  5   Viewed   46 times

Say I have a string in php, that prints out to a text file like this:

nÖ§9q1Fª£

How do I get the byte codes of this to my text file rather than the funky ascii characters?

 Answers

63

Use the ord function

http://ca.php.net/ord

eg.

<?php
$var = "nÖ§9q1Fª£ˆæÓ§Œ_»—Ló]j";

for($i = 0; $i < strlen($var); $i++)
{
   echo ord($var[$i])."<br/>";
}
?>
Wednesday, March 31, 2021
 
Dev
answered 7 Months ago
Dev
23

Here is what you can do :

echo changeLetter("täp@tecmax.com", 'ä', 'ae'), PHP_EOL;
echo changeLetter("tüp@tecmax.com", 'ü', 'ue'), PHP_EOL;
echo changeLetter("töp@tecmax.com", 'ö', 'oe'), PHP_EOL;

Output

taep@tecmax.com
tuep@tecmax.com
toep@tecmax.com

Function Used

function changeLetter($str, $needle, $val, $pos = 0) {
    $next = function ($str, &$pos) {
        if (! isset($str[$pos]))
            return false;
        $char = ord($str[$pos]);
        if ($char < 128) {
            return $str[$pos ++];
        } else {
            if ($char < 224) {
                $bytes = 2;
            } elseif ($char < 240) {
                $bytes = 3;
            } elseif ($char < 248) {
                $bytes = 4;
            } elseif ($char = 252) {
                $bytes = 5;
            } else {
                $bytes = 6;
            }
            $str = substr($str, $pos, $bytes);
            $pos += $bytes;
            return $str;
        }
    };
    $mstr = "";
    while(($chr = $next($str, $pos)) !== false) {
        $mstr .= $chr == $needle ? $val : $chr;
    }
    return $mstr;
}
Saturday, May 29, 2021
 
e_i_pi
answered 5 Months ago
64

The code:

public class Test {
    public static void main(String args[]) {
        String string = args[0];
        System.out.println("last character: " +
                           string.substring(string.length() - 1)); 
    }
}

The output of java Test abcdef:

last character: f
Sunday, June 27, 2021
 
Akdeniz
answered 4 Months ago
47

You can write text to a bitmap in any font you want and scan the bitmap to produce the ASCII art. You can even use anti-aliasing for effect. ;)


The last time I did this was about tens years ago. ;)

BufferedImage image = new BufferedImage(144, 32, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setFont(new Font("Dialog", Font.PLAIN, 24));
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.drawString("Hello World!", 6, 24);
ImageIO.write(image, "png", new File("text.png"));

for (int y = 0; y < 32; y++) {
    StringBuilder sb = new StringBuilder();
    for (int x = 0; x < 144; x++)
        sb.append(image.getRGB(x, y) == -16777216 ? " " : image.getRGB(x, y) == -1 ? "#" : "*");
    if (sb.toString().trim().isEmpty()) continue;
    System.out.println(sb);
}

writes out an image

Hello World

and prints

    ##         ##                 ##    ##                      *#*      *##**      *#*                       ##            ##     ##       
    ##         ##                 ##    ##                      *#*      *###*      *#*                       ##            ##     ##       
    ##         ##                 ##    ##                      *#*      *#*#*      *#*                       ##            ##     ##       
    ##         ##                 ##    ##                      *#*     *******     *#*                       ##            ##     ##       
    ##         ##     *******     ##    ##     *******           *#*    *#* *#*    *#*    *******     ##****  ##     ****** ##     ##       
    ##         ##    **#####**    ##    ##    **#####**          *#*    *#* *#*    *#*   **#####**    ##*##*  ##    **#####*##     ##       
    ##         ##   **#*****#**   ##    ##   **#** **#**         *#*   **#* *#*    *#*  **#** **#**   ##****  ##   **#** ***##     ##       
    #############   *#*     *#*   ##    ##   *#*     *#*         *#*   *#*   *#*   *#*  *#*     *#*   ##*     ##   *#*     *##     ##       
    #############   *#*     *#*   ##    ##   *#*     *#*         ****  *#*   *#*  ****  *#*     *#*   ##*     ##   *#*     *##     ##       
    ##         ##   *#########*   ##    ##   *#*     *#*          *#*  *#*   *#*  *#*   *#*     *#*   ##      ##   *#*     *##     ##       
    ##         ##   *#########*   ##    ##   *#       #*          *#* *#*     *#* *#*   *#       #*   ##      ##   *#       ##     ##       
    ##         ##   *#*           ##    ##   *#*     *#*          *#* *#*     *#* *#*   *#*     *#*   ##      ##   *#*     *##     ##       
    ##         ##   *#*           ##    ##   *#*     *#*          *****#*     *#*****   *#*     *#*   ##      ##   *#*     *##     ##       
    ##         ##   *#*     *#*   ##    ##   *#*     *#*           *#*#*       *#*#*    *#*     *#*   ##      ##   *#*     *##              
    ##         ##   **#** *****   ##    ##   **#** **#**           *#*#*       *#*#*    **#** **#**   ##      ##   **#** ***##              
    ##         ##    **######*    ##    ##    **#####**            *###*       *###*     **#####**    ##      ##    **#####*##     ##       
    ##         ##     *******     ##    ##     *******             *##**       **##*      *******     ##      ##     ****** ##     ##    

change to BOLD

    ###       ###                 ###    ###                        *##*     *###*     *##*                          ###            ###    #
    ###       ###                 ###    ###                        *##*     *###*     *##*                          ###            ###    #
    ###       ###                 ###    ###                        *##*     *###*     *##*                          ###            ###    #
    ###       ###                 ###    ###                        **##*   **###**   *##**                          ###            ###    #
    ###       ###     *******     ###    ###      *******            *##*   *##*##*   *##*     *******      ###****  ###     ****** ###    *
    ###       ###    **#####**    ###    ###    **#######**          *##*   *##*##*   *##*   **#######**    ###*##*  ###    **#####*###    *
    ###       ###   **#######**   ###    ###    *#########*          *##**  *#* *#*  **#**   *#########*    #####**  ###   **##########    *
    #############   *##** **##*   ###    ###   *###** **###*          *##* *##* *##* *##*   *###** **###*   ###**    ###   *##******###    *
    #############   *##*   *##*   ###    ###   *##*     *##*          *##* *##* *##* *##*   *##*     *##*   ###*     ###   *##*    *###    *
    #############   *#########*   ###    ###   *##*     *##*          *##* *##* **#* *##*   *##*     *##*   ###*     ###   *##*    *###    *
    ###       ###   *#########*   ###    ###   ###       ##*           *##*##*   *#**##*    ###       ##*   ###*     ###   *##      ###    *
    ###       ###   *##*          ###    ###   *##*     *##*           *##*##*   *##*##*    *##*     *##*   ###      ###   *##*    *###    *
    ###       ###   *##*          ###    ###   *##*     *##*           *#####*   *#####*    *##*     *##*   ###      ###   *##*    *###    *
    ###       ###   *##*****##*   ###    ###   *###** **###*           **###**    *###**    *###** **###*   ###      ###   *##******###     
    ###       ###   **########*   ###    ###    *#########*             *###*     *###*      *#########*    ###      ###   **##########    #
    ###       ###    **######*    ###    ###    **#######**             *###*     *###*      **#######**    ###      ###    **#####*###    #
    ###       ###     *******     ###    ###      *******               *###*     *###*        *******      ###      ###     *******###    #

in ITALIC

      *#*        *#*                *#*   *#*                      *#*       *##*      **#*                      *#*          *#*  *#*      
      *#*        *#*                *#*   *#*                      *#*      *###*      *#*                       *#*          *#*  *#*      
      *#*        *#*                *#*   *#*                      *#*      *###*     **#*                       *#*          *#*  *#*      
      *#*        *#*                *#*   *#*                      *#*     *#**#*     *#*                        *#*          *#*  *#*      
     ****       *#*    *******     ****  ****   *******            *#*     *#**#*    ****   *******    *#* **#* ****   ********#*  *#*      
     *#*        *#*   **######*    *#*   *#*   **######*           *#*    *#***#*    *#*   **######*   *#**###* *#*   **####**#**  **       
     *#*        *#*  **#******#*   *#*   *#*  **#******#*          *#*   **#* *#*   ****  **#******#*  *#****   *#*  *##** **##*  *#*       
     *############* **#*     *#*   *#*   *#* **#*     *#*          *#*   *#*  *#*   *#*  **#*     *#*  *#**     *#* **#*    **#*  *#*       
    **###########** *#*       #*  ****  **** *#*      *#*          *#*  **#*  *#*  ****  *#*      *#* **#*     **** *#*      *#*  *#*       
    *#*        *#*  *##########*  *#*   *#*  *#*       #*          *#*  *#*   *#*  *#*   *#*       #* *#**     *#*  *#*      *#*  **        
    *#*        *#*  *##########*  *#*   *#*  *#*      *#*          *#* ****   *#* *#**   *#*      *#* *#*      *#*  *#*      *#*  **        
    *#*        *#*  *#            *#*   *#*  *#       *#*          *#* *#*    *#* *#*    *#       *#* *#*      *#*  *#      ***   **        
    *#*        *#*  *#*           *#*   *#*  *#*     **#*          *#**#**    *#**#**    *#*     **#* *#*      *#*  *#*     *#*   **        
   *#*        *#*   *#*     **** *#*   *#*   *#*     *#**          *#**#*     *#**#*     *#*     *#***#*      *#*   *#*    **#*             
   *#*        *#*   *#*******#*  *#*   *#*   *#*** **##*           *###**     *###**     *#*** **##* *#*      *#*   *#******##*             
   *#*        *#*    *######**   *#*   *#*    *######**            *###*      *###*       *######**  *#*      *#*   **#####**#*  *#*        
   *#*        *#*     *******    *#*   *#*     *******              ##*       *##*         *******   *#*      *#*    ********#*  *#*        

Change the Font to SERIF BOLD

   #######   #######             ####   ####                   *######* *######*   ####*                       ####         ####    ***     
    *###*     *###*              *###   *###                    **##**   **##**    ****                        *###         *###    *#*     
     ###       ###                ###    ###                     *##**    **##*     **                          ###          ###    *#*     
     ###       ###                ###    ###                      *##*     *##*    ***                          ###          ###    *#*     
     ###       ###                ###    ###                      *##*     **#**   **                           ###          ###    *#*     
     ###       ###      ******    ###    ###     ******           **##*   ***##*   **     ******    #### ****   ###     *****###    *#*     
     ###       ###     *#***#**   ###    ###    *#****#*           *##*   ***##*   **    *#****#*   *###*###*   ###    *#****###    *#*     
     #############    *##* *##*   ###    ###   *##*  *##*          *##*   ****#** **    *##*  *##*   ###*****   ###   *##*  *###    *#*     
     ###       ###    *##   ##*   ###    ###   *##*  *##*           *##* **  *##* **    *##*  *##*   ###*       ###   *##*   ###    *#*     
     ###       ###    *#######*   ###    ###   *##    ##*           *##* **  *##* **    *##    ##*   ###*       ###   *##    ###    *#*     
     ###       ###    *##*        ###    ###   *##    ##*           *##***   **##**     *##    ##*   ###        ###   *##    ###    *#      
     ###       ###    *##*        ###    ###   *##    ##*            *##**    *##**     *##    ##*   ###        ###   *##    ###            
     ###       ###    *##*   **   ###    ###   *##*  *##*            *##**    *###*     *##*  *##*   ###        ###   *##*   ###            
     ###       ###    *##** ***   ###    ###   *##*  *##*            **#*      *#*      *##*  *##*   ###        ###   *##*  *###*   ***     
    *###*     *###*    *#####*   *###*  *###*   *#****#*              *#*      *#*       *#****#*   *###*      *###*  **#****####   *#*     
   #######   #######   ******    #####  #####    ******               ***      *#*        ******    #####      #####   **********   ***     
Sunday, August 8, 2021
 
shin
answered 3 Months ago
61

No need to apologize for asking a question! Try using the RIGHT function. It returns the last n characters of a string.

=RIGHT(A1, 1)
Wednesday, October 6, 2021
 
bart.z33
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :