473,404 Members | 2,187 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,404 software developers and data experts.

converting char array to integer

Hi,
Can you help me conver a char array into an integer. I am trying something
like..

char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert

I know this doesnt work. Does anyone of you have a suggestion for that
conversion. This is to be implemented in a 16bit µc. Is there a solution
without using anything like sprintf or multiplication.

Thanks in advance
Densil
Nov 14 '05 #1
15 42735
On Thu, 15 Apr 2004 05:26:27 -0400, silentlights wrote:
Hi,
Can you help me conver a char array into an integer. I am trying something
like..

char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert

I know this doesnt work. Does anyone of you have a suggestion for that
conversion. This is to be implemented in a 16bit µc. Is there a solution
without using anything like sprintf or multiplication.


What would you want 'numb' to be after the conversion?

1515 ?
5151 ?
12 ?

Something else

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 14 '05 #2
"silentlights" <si**********@yahoo.co.uk> wrote in message
news:89******************************@localhost.ta lkaboutprogramming.com...
Can you help me conver a char array into an integer. I am trying something
like..

char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert

I know this doesnt work. Does anyone of you have a suggestion for that
conversion. This is to be implemented in a 16bit c. Is there a solution
without using anything like sprintf or multiplication.


#include <stdlib.h>

char carray[5];
int numb;
....
carray[0] = '1'; // 1 != '1'
carray[1] = '5';
carray[2] = '1';
carray[3] = '5';
carray[4] = 0; // null termination of string

numb = atoi(carray);
....
--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #3
silentlights wrote:
char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert


What you could also do (but this will only work on 32-bit micro-processors)
carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;
numb = (int *) carray; // carray is a char* already so cast it into an int
*

As for the case you're mentioning, if int's are 16 bits and char's are 8
bits this will only set num to either 15 or 51 depending on the endianity of
the machine you're using.

Ahmed
Nov 14 '05 #4
"Ahmed S. Badran" <ah**********@spymac.com> wrote:
silentlights wrote:
char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert


What you could also do (but this will only work on 32-bit micro-processors)
carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;
numb = (int *) carray; // carray is a char* already so cast it into an int
*


Besides it being a brain-dead idea in the first place, you could've
well put any garbage in the array, since you only try to assign the
pointer value carray decays to in value context (sic) to an integer
variable, after performing a useless type cast, BTW.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #5
Irrwahn Grausewitz wrote:
"Ahmed S. Badran" <ah**********@spymac.com> wrote:
silentlights wrote:
char carray[5];
int numb;

carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;

numb = (int) carray; // this is what I want to convert


What you could also do (but this will only work on 32-bit
micro-processors) carray[0] = 1;
carray[1] = 5;
carray[2] = 1;
carray[3] = 5;
numb = (int *) carray; // carray is a char* already so cast it into
an int *


Besides it being a brain-dead idea in the first place, you could've
well put any garbage in the array, since you only try to assign the
pointer value carray decays to in value context (sic) to an integer
variable, after performing a useless type cast, BTW.

Regards


Sorry guys, the correction is

numb = *((int *)carray);

Ahmed
Nov 14 '05 #6
Hi Nils,

I need to have numb = 1515

by the way..

char str[5] = "5678";
int intvar = (int *)str;

doesnt work either.

I thank everyone here who is reading this messg and trying to post a
reply. Your help is highly appreciated.

Cheers
Densil

Nov 14 '05 #7
silentlights wrote:
Hi Nils,

I need to have numb = 1515

by the way..

char str[5] = "5678";
int intvar = (int *)str;

doesnt work either.

I thank everyone here who is reading this messg and trying to post a
reply. Your help is highly appreciated.

Cheers
Densil


Densil,

char str[5] = "5678";

/* is NOT */

char str[5];
str[0] = 5;
str[1] = 6;
str[2] = 7;
str[3] = 8;
str[4] = 0; /* or null or whatever.. */

to convert

char str[5] = "5678"; /* to int */

use

numb = atoi(str);

to covert the earlier case (of str[i] = 5 + i;) you can use

numb = * ((int *) str); /* in this case numb will be 5678 if you're on a
big endian system */

Ahmed

Ahmed
Nov 14 '05 #8
Hi Guys,
I thank everyone who took time to write me an idea.

(1) I cant use the function "atoi" because I cant use any standard
libraries as headers.

(2) It is a 16bit env, neither can I use the *((int *) str) way.

The only method I can use.. without any multiplication is...

unsigned int intvar = 0;
char str[4] = {'4','3','2','1'};
unsigned int tau[11] = {0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000,
9000};
unsigned int hun[11] = {0, 100, 200, 300, 400, 500, 600, 700, 800, 900};
unsigned int ten[11] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90};

intvar = tau[str[0] - 48] + hun[str[1] - 48] + ten[str[2] - 48] + str[3] -
48;
If anyone of you find there is something fishy.. pls tell me.. if there
could be any other way.. without using functions like "atoi" or any "str*"
functions.. wihtout using multiplication.. pls post me here.

Thank you all once again
Densil

Nov 14 '05 #9
"silentlights" <si**********@yahoo.co.uk> writes:
Hi Guys,
I thank everyone who took time to write me an idea.

(1) I cant use the function "atoi" because I cant use any standard
libraries as headers.

(2) It is a 16bit env, neither can I use the *((int *) str) way.

The only method I can use.. without any multiplication is...

unsigned int intvar = 0;
char str[4] = {'4','3','2','1'};
unsigned int tau[11] = {0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000,
9000};
unsigned int hun[11] = {0, 100, 200, 300, 400, 500, 600, 700, 800, 900};
unsigned int ten[11] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90};

intvar = tau[str[0] - 48] + hun[str[1] - 48] + ten[str[2] - 48] + str[3] -
48;

If anyone of you find there is something fishy.. pls tell me.. if there
could be any other way.. without using functions like "atoi" or any "str*"
functions.. wihtout using multiplication.. pls post me here.


I don't understand /why/ you cannot use multiplication. However, you
can emulate it using shifts and additions. For example, the macro

#define MUL10(x) (((x) << 3) + ((x) << 1))

emulates multiplication by 10.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #10
On Thu, 15 Apr 2004 18:08:18 +0200, "Ahmed S. Badran"
<ah**********@spymac.com> wrote in comp.lang.c:
silentlights wrote:
Hi Nils,

I need to have numb = 1515

by the way..

char str[5] = "5678";
int intvar = (int *)str;

doesnt work either.

I thank everyone here who is reading this messg and trying to post a
reply. Your help is highly appreciated.

Cheers
Densil
Densil,

char str[5] = "5678";

/* is NOT */

char str[5];
str[0] = 5;
str[1] = 6;
str[2] = 7;
str[3] = 8;
str[4] = 0; /* or null or whatever.. */

to convert

char str[5] = "5678"; /* to int */

use

numb = atoi(str);


Don't use atoi(), or any of the ato... functions, unless you can
guarantee the converted result will not be out of range for the return
type. Undefined behavior results.

The strto... functions, on the other hand, have defined behavior no
matter what the input.
to covert the earlier case (of str[i] = 5 + i;) you can use

numb = * ((int *) str); /* in this case numb will be 5678 if you're on a
big endian system */
1. No, the result will not be 5678 on any conforming C
implementation. Not a chance. C requires a pure binary notation for
the integer types.

2. In the original post, the OP stated he was using a 16 bit uC. At
best, accessing an array of four bytes will result in only the first
two of them being accessed.

3. str might not meet the alignment requirements for the address of
an int on many 16-bit platforms. Results can range from accessing the
wrong bytes of memory to hardware traps. Undefined behavior is like
that.
Ahmed


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #11
Hi Martin,
I don't understand /why/ you cannot use multiplication.
it is expensive for the µC.
However, you can emulate it using shifts and additions.
For example, the macro
#define MUL10(x) (((x) << 3) + ((x) << 1))
emulates multiplication by 10.


Fast multiplication is a better solution. It saves a lot of opcodes.

Thanks
Densil

Nov 14 '05 #12
Martin Dickopp wrote:
.... snip ...
I don't understand /why/ you cannot use multiplication. However,
you can emulate it using shifts and additions. For example, the
macro

#define MUL10(x) (((x) << 3) + ((x) << 1))

emulates multiplication by 10.


Pseudo assembly, 10 * R1 -> R2. R2, flags disturbed. Fast.

mov r2,r1; operands dest,src
add r2,r2; 2*
add r2,r2; 4*
add r2,r1; 5*
add r2,r2; 10*

and the equivalent macro:

#define MUL10(x) (((x) + ((x) << 2)) << 1)

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #13
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

CBFalconer wrote:
| Martin Dickopp wrote:
|
| ... snip ...
|
|>I don't understand /why/ you cannot use multiplication. However,
|>you can emulate it using shifts and additions. For example, the
|>macro
|>
|> #define MUL10(x) (((x) << 3) + ((x) << 1))
|>
|>emulates multiplication by 10.
[snip]
|
| #define MUL10(x) (((x) + ((x) << 2)) << 1)
|

Sweet!
- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAf8wqagVFX4UWr64RAiwWAJ0TLwPUSAY6i5RxKmoyQV je+Ey4eACfdTte
wqIYy3T7DgKJMFw73IM6y7s=
=LAKV
-----END PGP SIGNATURE-----
Nov 14 '05 #14
Could you please explain this shift.. I didnt get you.

How can I emulate multiplication for 100 and 1000 ?
Can I do something similair for division of 10, 100 and 1000 too ?..
Is there an alternative for modulo of 10, 100 and 1000 ?
Thanks
Densil

Nov 14 '05 #15
silentlights wrote:

Could you please explain this shift.. I didnt get you.

How can I emulate multiplication for 100 and 1000 ?
Can I do something similair for division of 10, 100 and 1000 too ?..
Is there an alternative for modulo of 10, 100 and 1000 ?


You failed to quote anything, so I have no idea what you are
talking about. I have better things to do than search through
discarded newsgroup entries.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #16

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

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
18
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
13
by: ppateel | last post by:
Hi, I am new to c++ and I am converting a c program to c++. I changed malloc call to new and I am getting an exception violation. Here is the relevant piece of code. Compiler vc++ 7.0 (.Net...
156
by: Lame Duck | last post by:
Hi Group! I have a vector<floatvariable that I need to pass to a function, but the function takes a float * arguement. That's OK, I can convert by doing &MyVector.front(), but when I get back a...
12
by: cmdolcet69 | last post by:
Can anyone give me some ideas on how to convert the following program to vb 2003? I would like to jsut take this code and implement it in vb. def.h /* Global Type Definitions */ typedef ...
1
by: freeurmind | last post by:
hello, i'm writing a function that translate Bytes of type unsigned char to integer array of 1's and 0's, i'm taking a message of 33 Bytes and translating them in an int array of 264 integers, here's...
5
by: AZRebelCowgirl73 | last post by:
I am trying to accomodate for negative numbers, and as long as the negative is not being used, I have no problems with my input() ouput() and add() functions. I have tried this two different ways,...
15
by: itdevries | last post by:
Hi, I'm trying to read some binary data from a file, I've read a few bytes of the data into a char array with ifstream. Now I know that the first 4 bytes in the char array represent an integer....
9
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.