Allan Bruce wrote:
> "nataraj" <> wrote in message
> >
> > Can any one know how to convert a multibyte char to unsigned
> > long or any type.
> >
> > sscanf(char* , format , &destination); will not work... for
> > multibyte..... even atol(char); will not work... if you have
> > other suggestions please do mail me.
> >
> > ex: char[2]; char[0] = 0x23; char[1] = 0x34 , the result
> > should be destination = 0x2334
> > should be generic one, the destination need not be always of
> > the same type.
>
> I would use a union but this will only work if you know how many
> bytes are used by your system, e.g. on mine a short uses 2Bytes,
> so if you have a union with a char array of length 2, and a
Not a good idea. The result is sensitive to such things as byte
size, endianess, etc. The OP has specified the relationship, and
implied that the char * array will be unsigned (if not, it should
be). He also implies that the chars are restricted to 0..255.
You can accumulate the results very simply with:
unsigned long valueof(char *s, unsigned int length)
{
unsigned long val;
unsigned int i;
for (val = 0, i = 0; i < length; i++) {
val = 256 * val + s[i];
}
return val;
}
Note that length has to be supplied, because the char array may
contain zeroes, and thus is NOT a string. The result is
independant of the endianess and the byte size, and can be cast to
signed or unsigned int, short, etc. as desired (provided it fits).
--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
|