Decorating Hex function to pad zeros

Starting with Python 3.6, you can: >>> value = 42 >>> padding = 6 >>> f”{value:#0{padding}x}” ‘0x002a’ for older python versions use the .format() string method: >>> “{0:#0{1}x}”.format(42,6) ‘0x002a’ Explanation: { # Format identifier 0: # first parameter # # use “0x” prefix 0 # fill with zeroes {1} # to a length of n … Read more

How to transform fields during deserialization using Serde?

The deserialize_with attribute The easiest solution is to use the Serde field attribute deserialize_with to set a custom serialization function for your field. You then can get the raw string and convert it as appropriate: use serde::{de::Error, Deserialize, Deserializer}; // 1.0.94 use serde_json; // 1.0.40 #[derive(Debug, Deserialize)] struct EtheriumTransaction { #[serde(deserialize_with = “from_hex”)] account: u64, … Read more

Converting from hex to string

Like so? static void Main() { byte[] data = FromHex(“47-61-74-65-77-61-79-53-65-72-76-65-72”); string s = Encoding.ASCII.GetString(data); // GatewayServer } public static byte[] FromHex(string hex) { hex = hex.Replace(“-“, “”); byte[] raw = new byte[hex.Length / 2]; for (int i = 0; i < raw.Length; i++) { raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); } return raw; }

String.Format for Hex

The number 0 in {0:X} refers to the position in the list or arguments. In this case 0 means use the first value, which is Blue. Use {1:X} for the second argument (Green), and so on. colorstring = String.Format(“#{0:X}{1:X}{2:X}{3:X}”, Blue, Green, Red, Space); The syntax for the format parameter is described in the documentation: Format … Read more

Bitwise XOR of hexadecimal numbers

Whoa. You’re really over-complicating it by a very long distance. Try: >>> print(hex(0x12ef ^ 0xabcd)) 0xb922 You seem to be ignoring these handy facts, at least: Python has native support for hexadecimal integer literals, with the 0x prefix. “Hexadecimal” is just a presentation detail; the arithmetic is done in binary, and then the result is … Read more

Convert bytes to bits in python

Another way to do this is by using the bitstring module: >>> from bitstring import BitArray >>> input_str=”0xff” >>> c = BitArray(hex=input_str) >>> c.bin ‘0b11111111’ And if you need to strip the leading 0b: >>> c.bin[2:] ‘11111111’ The bitstring module isn’t a requirement, as jcollado‘s answer shows, but it has lots of performant methods for … Read more

Given a background color, black or white text?

Luminosity Contrast algorithm I think the best way is the Luminosity Contrast algorithm: function getContrastColor($hexColor) { // hexColor RGB $R1 = hexdec(substr($hexColor, 1, 2)); $G1 = hexdec(substr($hexColor, 3, 2)); $B1 = hexdec(substr($hexColor, 5, 2)); // Black RGB $blackColor = “#000000”; $R2BlackColor = hexdec(substr($blackColor, 1, 2)); $G2BlackColor = hexdec(substr($blackColor, 3, 2)); $B2BlackColor = hexdec(substr($blackColor, 5, 2)); … Read more

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