[erlang-questions] Help! a log dump function from C.

feiman <>
Wed Apr 22 02:03:11 CEST 2009


hi,All
   I have a function from C,which print out detail hex information
about a buffer,who can help me "translate" it into Erlang?

when I run
	const char *teststr = "teststr1234567890abcd";

	dump_hex((const unsigned char *)teststr,strlen(teststr));

it print out

00000000: 74 65 73 74 73 74 72 31 32 33 34 35 36 37 38 39 -
teststr123456789
00000010: 30 61 62 63 64                                  - 0abcd

thanks all in advance

void dump_hex( const unsigned char *buf, int len)
{
	int i;
	int nlocal;
	const unsigned char *pc;
	char *out;
	const unsigned char *start;
	char c;
	char line[100];

	start = buf;

	while (len > 0)
	{
		sprintf(line, "%08x: ", buf - start);
		out = line + 10;

		for (i = 0, pc = buf, nlocal = len; i < 16; i++, pc++)
		{
			if (nlocal > 0)
			{
				c = *pc;

				*out++ = NIBBLE((c >> 4) & 0xF);
				*out++ = NIBBLE(c & 0xF);

				nlocal--;
			}
			else
			{
				*out++ = ' ';
				*out++ = ' ';
			}			/* end else */

			*out++ = ' ';
		}			/* end for */

		*out++ = '-';
		*out++ = ' ';

		for (i = 0, pc = buf, nlocal = len;
			(i < 16) && (nlocal > 0);
			i++, pc++, nlocal--)
		{
			c = *pc;

			if ((c < ' ') || (c >= 126))
			{
				c = '.';
			}

			*out++ = c;
		}			/* end for */

		*out++ = 0;

		llog(L_NOTICE,"%s", line);

		buf += 16;
		len -= 16;
	}				/* end while */
}				/* end dump */




More information about the erlang-questions mailing list