Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > tech help required..

Reply
Thread Tools

tech help required..

 
 
nataraj
Guest
Posts: n/a
 
      10-17-2003
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



 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      10-17-2003

"nataraj" <(E-Mail Removed)> wrote in message
news:bmo8ug$opbts$(E-Mail Removed)-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


 
Reply With Quote
 
 
 
 
Ed Morton
Guest
Posts: n/a
 
      10-17-2003


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
>
>
>


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-17-2003
Allan Bruce wrote:
> "nataraj" <(E-Mail Removed)> 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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hi tech gurus combo box help =?Utf-8?B?cmFt?= MCSE 2 12-20-2005 12:13 PM
PC TECH HELP Stephen Ireland Computer Support 2 11-12-2004 02:13 PM
tech help help Microsoft Certification 1 12-15-2003 02:57 PM
Looking for help/resources on Writing a nice detailed design / tech design for vb.net code SpamProof Java 3 12-01-2003 06:06 AM



Advertisments