users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re[1] hexstring help


From: Renaud Mariana
Subject: Re[1] hexstring help
Date: Thu, 31 Jan 2002 09:59:41 GMT+1

Gregory,

my question is how to unify any hexstring with a list of
bytes efficiently and without too much allocation ?

ex:
hexstring('A0000000300002FFFFFFFF8900010001',
 [160,0,0,0,48,0,2,255,255,255,255,137,0,1,0,1]).
 returns true.

Regards
Renaud.

> -------Message d'origine-------
> De : Gregory Bourassa <address@hidden>
> Date : 30/01/2002 20:17:50
>
> Mariana,
>
> First, note that I am not familiar with 010A as a hex
representation of [1,10]. Do you
> intend to represent things like FFFF as [255,255]?
>
> That said, your conversions should be possible within
Prolog quite easily.
>
> What would be wrong with treating the atom as a character
list until the last moment,
> then converting it back to an atom (see
read_term_from_chars and it's writing dual).
> Then, if you have a list of chars from the atom and a list
of hex chars, the unification
> becomes trivial. A conceptual example:
>
> hexstring([`0,`1,`0,`A], [1,10]) :-
> hex_pair( [`0,`1], [1] ),
> hex_pair( [`0,`A], [10] ).
>
> where hex_pair is defined as:
> hex_pair( [`0,`0], [0] ).
> hex_pair( [`0,`1], [1] ).
> ...etc.
>
> The latter is where my lack of understanding of your
notation becomes an issue. If you
> really plan to go up to 255, then hex_pair needs to be
defined in terms of rule that
> converts each member of the pair to an integer, multiplies
the left one by 16 and adds
> the right one to it.
>
> Regards,
>
> Gregory Bourassa
>
> On Jan 30, Renaud Mariana wrote:
> >
> >
> > Hi all,
> >
> >
> >
> > does anybody has a predicate that unifies a list of bytes to
> >
> > a hexstring (atom) efficiently ?
> >
> >
> >
> > ex: hexstring('010A', [1,10]). returns true.
> >
> >
> >
> > the one I propose with the C interface is not elegant,
> >
> > requires a lot of allocations and also may crash if the list
> >
> > is too long .
> >
> > Thanks.
> >
> >
> >
> > Renaud Mariana
> >
> >
> >
> > //-------------------------------------------------------
> >
> >
> >
> > // fill buf with byte-elements of list
> >
> > // buf must be allocated
> >
> > int
> >
> > getCharsFromList(PlTerm list, unsigned char* buf)
> >
> > {
> >
> >     PlTerm* pterm = (PlTerm*)list;
> >
> >     int n = 0;
> >
> >
> >
> >
> >
> >     for(; pterm != (PlTerm*)NIL_WORD; pterm = (PlTerm*)pterm[1])
> >
> >             {
> >
> >                     pterm = Rd_List( (PlTerm)pterm);
> >
> >                     if(pterm == 0) break;
> >
> >                     buf[n++] = Rd_Byte( pterm[0])&0xff;
> >
> >     }
> >
> >
> >
> >     return n;
> >
> > }
> >
> >
> >
> > // test
> >
> > // hexstring('A0000000300002FFFFFFFF8900010001', L ).
> >
> > // hexstring( T,
> >
> > [160,0,0,0,48,0,2,255,255,255,255,137,0,1,0,1]).
> >
> >
> >
> >
> >
> >
> >
> > // conversion: atom list of bytes
> >
> > Bool
> >
> > hexstring (PlTerm atom, PlTerm list)
> >
> > {
> >
> >     int i = 0, t, length;
> >
> >     char *hexDigits = "0123456789ABCDEF";
> >
> >     unsigned char buf[1024];
> >
> >     char str[2048], *str2;
> >
> >     PlTerm term[1024];
> >
> >
> >
> >
> >
> >     if(Blt_Non_Var(list)) {
> >
> >             length = getCharsFromList(list, buf);
> >
> >             str2 = str;
> >
> >
> >
> >             for ( i = 0; i
> >                     t = buf[i++];
> >
> >                     *str2++ = hexDigits[(t >> 4) & 0x0F];
> >
> >                     *str2++ = hexDigits[ t & 0x0F];
> >
> >             }
> >
> >             *str2 = 0;
> >
> >             return Un_String_Check(str, atom);
> >
> >     }
> >
> >
> >
> >     if( Blt_Var(atom))
> >
> >             Pl_Err_Instantiation();
> >
> >
> >
> >     str2 = Rd_String_Check(atom);
> >
> >     length = strlen(str2);
> >
> >
> >
> >     if ((length ) == 1) {
> >
> >             sscanf(str2++, "df9f66ac", &t);
> >
> >             term[i++] = Mk_Byte(t);
> >
> >     }
> >
> >
> >
> >     for ( ; *str2 ; str2+=2 ) {
> >
> >             sscanf(str2, "8046584", &t);
> >
> >             term[i++] = Mk_Byte(t);
> >
> >     }
> >
> >
> >
> > return Un_Proper_List_Check(i, term, list);
> >
> > }
> >
> > //-------------------------------------------------------
> >
> >
> >
> >
> >
> >
> >
> > ______________________________________________________
> >
> > Boîte aux lettres - Caramail - http://www.caramail.com
> >
> >
> >
> >
> >
> >
>
>
______________________________________________________
Boîte aux lettres - Caramail - http://www.caramail.com


reply via email to

[Prev in Thread] Current Thread [Next in Thread]