473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

tech help required..

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

Nov 13 '05 #1
3 1394

"nataraj" <na*****@globaledgesoft.com> wrote in message
news:bm************@ID-208444.news.uni-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
Nov 13 '05 #2


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


Nov 13 '05 #3
Allan Bruce wrote:
"nataraj" <na*****@globaledgesoft.com> 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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

385
by: Xah Lee | last post by:
Jargons of Info Tech industry (A Love of Jargons) Xah Lee, 2002 Feb People in the computing field like to spur the use of spurious jargons. The less educated they are, the more they like...
3
by: News VS.NET \( MS ILM \) | last post by:
What is the equivalence of ActiveX, COM, COM+ and DCOM in .NET I know there is no 100% equivalence Thank you.
0
by: Scott Nonnenberg [MSFT] | last post by:
As part of our standard every-other-thursday chat series, the C# Team is having a Post-TechEd wrapup chat this Thursday. We're trying to get our session content online before then, but only DEV370...
102
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality...
22
by: mattf | last post by:
I've discovered Python and have been trying it out lately as a possible replacement for computations that would ordinarily be done with a commercial package like Matlab or IDL. I'd like to mention...
28
by: Useful Info | last post by:
Like on 9/11, the Federal Government apparently WANTED people to die at the hands of Cho at VA Tech, because they told campus police not to pursue Cho after the double homicide occurred. Story...
13
by: General Fear | last post by:
Does anyone in this forum know of tech questions for an Access programmer? I have not been able to find a good web page with Access tech questions. I am interviewing Access programmers and want...
33
by: NicolasG | last post by:
Hi, I want to be a professional python programmer, unfortunately I'm working on technical support and don't have the time/patience to start making projects my self. I tried to apply to some...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.