Converting string to byte array in C#

If you already have a byte array then you will need to know what type of encoding was used to make it into that byte array. For example, if the byte array was created like this: byte[] bytes = Encoding.ASCII.GetBytes(someString); You will need to turn it back into a string like this: string someString = … Read more

Identify if a string is a number

int n; bool isNumeric = int.TryParse(“123”, out n); Update As of C# 7: var isNumeric = int.TryParse(“123”, out int n); or if you don’t need the number you can discard the out parameter var isNumeric = int.TryParse(“123”, out _); The var s can be replaced by their respective types!

How to convert a char to a String?

You can use Character.toString(char). Note that this method simply returns a call to String.valueOf(char), which also works. As others have noted, string concatenation works as a shortcut as well: String s = “” + ‘s’; But this compiles down to: String s = new StringBuilder().append(“”).append(‘s’).toString(); which is less efficient because the StringBuilder is backed by … Read more

Convert hex string to integer in Python

Without the 0x prefix, you need to specify the base explicitly, otherwise there’s no way to tell: x = int(“deadbeef”, 16) With the 0x prefix, Python can distinguish hex and decimal automatically: >>> print(int(“0xdeadbeef”, 0)) 3735928559 >>> print(int(“10”, 0)) 10 (You must specify 0 as the base in order to invoke this prefix-guessing behavior; if … Read more

How to convert an instance of std::string to lower case

Adapted from Not So Frequently Asked Questions: #include <algorithm> #include <cctype> #include <string> std::string data = “Abc”; std::transform(data.begin(), data.end(), data.begin(), [](unsigned char c){ return std::tolower(c); }); You’re really not going to get away without iterating through each character. There’s no way to know whether the character is lowercase or uppercase otherwise. If you really hate … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)