Integer ASCII value to character in BASH using printf
One line printf “\x$(printf %x 65)” Two lines set $(printf %x 65) printf “\x$1” Here is one if you do not mind using awk awk ‘BEGIN{printf “%c”, 65}’
One line printf “\x$(printf %x 65)” Two lines set $(printf %x 65) printf “\x$1” Here is one if you do not mind using awk awk ‘BEGIN{printf “%c”, 65}’
If you want to break a string literal onto multiple lines, you can concatenate multiple strings together, one on each line, like so: printf(“name: %s\t” “args: %s\t” “value %d\t” “arraysize %d\n”, sp->name, sp->args, sp->value, sp->arraysize);
printf(“Hello World!”); is IMHO not vulnerable but consider this: const char *str; … printf(str); If str happens to point to a string containing %s format specifiers, your program will exhibit undefined behaviour (mostly a crash), whereas puts(str) will just display the string as is. Example: printf(“%s”); //undefined behaviour (mostly crash) puts(“%s”); // displays “%s\n”
The trick is to use type classes. In the case of printf, the key is the PrintfType type class. It does not expose any methods, but the important part is in the types anyway. class PrintfType r printf :: PrintfType r => String -> r So printf has an overloaded return type. In the trivial … Read more
The new .format() method is meant to replace the old % formatting syntax. The latter has been de-emphasised, (but not officially deprecated yet). The method documentation states as much: This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in … Read more
You can specify a width on string fields, e.g. printf(“%-20s”, “initialization…”); And then whatever’s printed with that field will be blank-padded to the width you indicate. The – left-justifies your text in that field.
Your result will vary depending on what kind of terminal or console program you’re on, but yes, on most \b is a nondestructive backspace. It moves the cursor backward, but doesn’t erase what’s there. So for the hello worl part, the code outputs hello worl ^ …(where ^ shows where the cursor is) Then it … Read more
You are seeing the ffffff because char is signed on your system. In C, vararg functions such as printf will promote all integers smaller than int to int. Since char is an integer (8-bit signed integer in your case), your chars are being promoted to int via sign-extension. Since c0 and 80 have a leading … Read more
Try #include <inttypes.h> … printf(“i [ %zu ] k [ %”PRIu32″ ]\n”, i, k); The z represents an integer of length same as size_t, and the PRIu32 macro, defined in the C99 header inttypes.h, represents an unsigned 32-bit integer.
Pure Bash, no external utilities This demonstration does full justification, but you can just omit subtracting the length of the second string if you want ragged-right lines. pad=$(printf ‘%0.1s’ “-“{1..60}) padlength=40 string2=’bbbbbbb’ for string1 in a aa aaaa aaaaaaaa do printf ‘%s’ “$string1” printf ‘%*.*s’ 0 $((padlength – ${#string1} – ${#string2} )) “$pad” printf ‘%s\n’ … Read more