Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Convert a decimal value to hex in Python

// Convert a dec value to hex

hexValue = "%X" % 256
print hexValue  #100

c C++ convert hex to ascii

// Presented with hex such as 0x12345abc perhaps there is a spot in there
// which represents an ascii char - such as 53 would be 'S'
// Common when dealing with hardware-related data structures and wire
// protocols

#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'));
}

produces this output:
S

Csharp format integer as hex string

// Using the built in ability to print a byte as a hex value
// this set of methods gives the ability to print shorts
// and longs as hex using the spacing that is customary.

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;
		}
	}
}



produces this output:
0C 00 00 00

str_hex and hex_str

// Convert hex to string and vice versa.
//
// (Source: http://codedump.jonasjohn.de/)

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...

HEX to RGB

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;
}

RGB to HEX

function RGBToHex (r, g, b ){
    var hex = r << 16 ^ g << 8 ^ b;
   return hex;
}
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS