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

Michael McDaniel erlangy@REDACTED
Wed Apr 22 05:18:52 CEST 2009



 lists:foldl( fun(X, Acc) -> io:fwrite("~.16B ", [X]) end, "", "teststr" ).



On Tue, Apr 21, 2009 at 05:03:11PM -0700, feiman wrote:
> 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 */
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions

-- 
Michael McDaniel
Portland, Oregon, USA
http://trip.autosys.us
http://autosys.us
http://mmcdaniel.com/erlview




More information about the erlang-questions mailing list