tech help required..

Discussion in 'C Programming' started by nataraj, Oct 17, 2003.

  1. nataraj

    nataraj Guest

    Hello,

    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.


    is there any built in function ....?

    Rgds
    Nataraj
     
    nataraj, Oct 17, 2003
    #1
    1. Advertising

  2. nataraj

    Allan Bruce Guest

    "nataraj" <> wrote in message
    news:bmo8ug$opbts$-berlin.de...
    > Hello,
    >
    > 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.
    >
    >
    > is there any built in function ....?
    >
    > Rgds
    > Nataraj
    >
    >
    >


    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 short, then placing the values
    into the unions char array, then reading the short should give you the
    answer.
    You could set up similar unions for 4Bytes, and 8Bytes (if your system
    supports them).
    HTH
    Allan
     
    Allan Bruce, Oct 17, 2003
    #2
    1. Advertising

  3. nataraj

    Ed Morton Guest

    nataraj wrote:

    > Hello,
    >
    > 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.


    Here's a way for unsigned long:

    #include "stdio.h"
    #include "stdlib.h"

    int main(void)
    {
    unsigned char source[] = { 0x23, 0x34 };
    unsigned long dest = 0;
    unsigned int shift = 0;
    unsigned int i = sizeof source;
    int ret = EXIT_SUCCESS;

    printf("source[0]= %#x\n",source[0]);
    printf("source[1]= %#x\n",source[1]);

    if (sizeof dest >= sizeof source) {
    while (i > 0) {
    dest |= (unsigned long)source[--i] << shift;
    shift += 8;
    }
    printf("dest= %#lX\n",dest);
    } else {
    printf("ERROR: dest is smaller than source\n");
    ret = EXIT_FAILURE;
    }

    return ret;
    }

    >
    > is there any built in function ....?


    None spring to mind.

    Ed.

    > Rgds
    > Nataraj
    >
    >
    >
     
    Ed Morton, Oct 17, 2003
    #3
  4. nataraj

    CBFalconer Guest

    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;
    }
    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!
     
    CBFalconer, Oct 17, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry Bires
    Replies:
    2
    Views:
    653
    salman sheikh
    Jun 3, 2004
  2. Gandalf
    Replies:
    2
    Views:
    1,589
    Gandalf
    Mar 7, 2004
  3. Tom Carter
    Replies:
    0
    Views:
    512
    Tom Carter
    Apr 29, 2004
  4. Just D.
    Replies:
    0
    Views:
    447
    Just D.
    Mar 10, 2006
  5. SpamProof
    Replies:
    3
    Views:
    652
    SpamProof
    Dec 1, 2003
Loading...

Share This Page