C++ convert string to hexadecimal and vice versa

A string like “Hello World” to hex format: 48656C6C6F20576F726C64. Ah, here you go: #include <string> std::string string_to_hex(const std::string& input) { static const char hex_digits[] = “0123456789ABCDEF”; std::string output; output.reserve(input.length() * 2); for (unsigned char c : input) { output.push_back(hex_digits[c >> 4]); output.push_back(hex_digits[c & 15]); } return output; } #include <stdexcept> int hex_value(unsigned char hex_digit) { … Read more

Convert integer to hex and hex to integer

Convert INT to hex: SELECT CONVERT(VARBINARY(8), 16777215) Convert hex to INT: SELECT CONVERT(INT, 0xFFFFFF) Update 2015-03-16 The above example has the limitation that it only works when the HEX value is given as an integer literal. For completeness, if the value to convert is a hexadecimal string (such as found in a varchar column) use: … Read more

Python Hexadecimal

Use the format() function with a ’02x’ format. >>> format(255, ’02x’) ‘ff’ >>> format(2, ’02x’) ’02’ The 02 part tells format() to use at least 2 digits and to use zeros to pad it to length, x means lower-case hexadecimal. The Format Specification Mini Language also gives you X for uppercase hex output, and you … Read more

Java converting int to hex and back again

int val = -32768; String hex = Integer.toHexString(val); int parsedResult = (int) Long.parseLong(hex, 16); System.out.println(parsedResult); That’s how you can do it. The reason why it doesn’t work your way: Integer.parseInt takes a signed int, while toHexString produces an unsigned result. So if you insert something higher than 0x7FFFFFF, an error will be thrown automatically. If … Read more

Javascript: Unicode string to hex

Remember that a JavaScript code unit is 16 bits wide. Therefore the hex string form will be 4 digits per code unit. usage: var str = “\u6f22\u5b57”; // “\u6f22\u5b57” === “漢字” alert(str.hexEncode().hexDecode()); String to hex form: String.prototype.hexEncode = function(){ var hex, i; var result = “”; for (i=0; i<this.length; i++) { hex = this.charCodeAt(i).toString(16); result … Read more

Convert string to hex-string in C#

First you’ll need to get it into a byte[], so do this: byte[] ba = Encoding.Default.GetBytes(“sample”); and then you can get the string: var hexString = BitConverter.ToString(ba); now, that’s going to return a string with dashes (-) in it so you can then simply use this: hexString = hexString.Replace(“-“, “”); to get rid of those … Read more

Generating a random hex color code with PHP

An RGB hex string is just a number from 0x0 through 0xFFFFFF, so simply generate a number in that range and convert it to hexadecimal: function rand_color() { return ‘#’ . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, ‘0’, STR_PAD_LEFT); } or: function rand_color() { return sprintf(‘#%06X’, mt_rand(0, 0xFFFFFF)); }

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