[ create a new paste ] login | about

Link: http://codepad.org/dGmnTULk    [ raw code | output | fork ]

C++, pasted on Dec 14:
void to_hex(char *buffer, size_t size, unsigned n)
{
	// Print digits in the reverse order
	size_t i = 0;
	for (; i < size - 1; ++i)
	{
		unsigned digit = n & 0xf;
		buffer[i] = digit < 10 ? digit + '0' : digit - 10 + 'A';
		n >>= 4;

		if (n == 0)
		{
			break;
		}
	}

	// Append NUL
	buffer[i + 1] = 0;

	// Reverse the string
	for (size_t j = 0; j < i / 2; ++j)
	{
		char c = buffer[j];
		buffer[j] = buffer[i - j];
		buffer[i - j] = c;
	}
}

int main()
{
	char s[20];
	to_hex(s, 20, 0x1c345);
	printf("%s\n", s);
	
	return 0;
}


Output:
1
1C345


Create a new paste based on this one


Comments: