How to get the ASCII value of a character

From here: The function ord() gets the int value of the char. And in case you want to convert back after playing with the number, function chr() does the trick. >>> ord(‘a’) 97 >>> chr(97) ‘a’ >>> chr(ord(‘a’) + 3) ‘d’ >>> In Python 2, there was also the unichr function, returning the Unicode character … Read more

Convert binary to ASCII and vice versa

For ASCII characters in the range [ -~] on Python 2: >>> import binascii >>> bin(int(binascii.hexlify(‘hello’), 16)) ‘0b110100001100101011011000110110001101111’ In reverse: >>> n = int(‘0b110100001100101011011000110110001101111’, 2) >>> binascii.unhexlify(‘%x’ % n) ‘hello’ In Python 3.2+: >>> bin(int.from_bytes(‘hello’.encode(), ‘big’)) ‘0b110100001100101011011000110110001101111’ In reverse: >>> n = int(‘0b110100001100101011011000110110001101111’, 2) >>> n.to_bytes((n.bit_length() + 7) // 8, ‘big’).decode() ‘hello’ To support all … Read more

Reading a plain text file in Java

My favorite way to read a small file is to use a BufferedReader and a StringBuilder. It is very simple and to the point (though not particularly effective, but good enough for most cases): BufferedReader br = new BufferedReader(new FileReader(“file.txt”)); try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) … Read more

How to shift characters to ASCII values in a file based on user input c++ [closed]

A simple implementation of the Caesar Cipher is to use a string of valid characters and the remainder operator, %. char Encrypt_Via_Caesar_Cipher(char letter, unsigned int shift) { static const std::string vocabulary = “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”; const std::string::size_type position = vocabulary.find(letter); char c = letter; if (position != std::string::npos) { const std::string::size_type length = vocabulary.length(); c = vocabulary[(position … Read more