Convert a decimal value to hex in Python
hexValue = "%X" % 256 print hexValue #100
11304 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
hexValue = "%X" % 256 print hexValue #100
#include <stdio.h> #include <stdlib.h> /* * To convert 53 to the character 'S': * char returnVal = hexToString('5', '3'); */ char hexToAscii(char first, char second) { char hex[5], *stop; hex[0] = '0'; hex[1] = 'x'; hex[2] = first; hex[3] = second; hex[4] = 0; return strtol(hex, &stop, 16); } int main(int argc, char* argv[]) { printf("%c\n", hexToAscii('5', '3')); }
using System; namespace testApp { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { Class1 t = new Class1(); long i = 12; // Console.WriteLine(t.int32ToHexString(i)); } private string int32ToHexString(long i) { byte[] int32Bytes; int32Bytes = BitConverter.GetBytes(i); return String.Format("{0}{1}{2}{3}", padString(int32Bytes[0].ToString("X")), padString(int32Bytes[1].ToString("X")), padString(int32Bytes[2].ToString("X")), padString(int32Bytes[3].ToString("X"))); } private string int16ToHexString(short i) { byte[] int32Bytes; int32Bytes = BitConverter.GetBytes(i); return String.Format("{0}{1}", padString(int32Bytes[0].ToString("X")), padString(int32Bytes[1].ToString("X"))); } private string byteToHexString(byte b) { return padString(String.Format("{0}", b.ToString("X"))); } private string padString(string s) { while (s.Length < 2) s = "0" + s; while (s.Length < 3) s = " " + s; return s; } } }
function str_hex($string){ $hex=''; for ($i=0; $i < strlen($string); $i++){ $hex .= dechex(ord($string[$i])); } return $hex; } function hex_str($hex){ $string=''; for ($i=0; $i < strlen($hex)-1; $i+=2){ $string .= chr(hexdec($hex[$i].$hex[$i+1])); } return $string; } // example: $hex = str_hex("test sentence..."); // $hex contains 746573742073656e74656e63652e2e2e print hex_str($hex); // outputs: test sentence...
function hexToRGB ( hex:Number ){ var returnObj:Object = new Object(); var returnObj .r = hex >> 16; var temp = hex ^ r << 16; var returnObj .g = temp >> 8; var returnObj .b = temp ^ g << 8; return returnObj; }
function RGBToHex (r, g, b ){ var hex = r << 16 ^ g << 8 ^ b; return hex; }