tech help required..

N

nataraj

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
 
A

Allan Bruce

nataraj said:
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
 
E

Ed Morton

nataraj said:
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.
 
C

CBFalconer

Allan said:
nataraj said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top