将char数组转换为整数 [英] converting char array to integer

查看:76
本文介绍了将char数组转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



你能帮我把一个char数组转换成一个整数。我正在尝试的东西

喜欢..


char carray [5];

int numb;


carray [0] = 1;

carray [1] = 5;

carray [2] = 1;

carray [3] = 5;


numb =(int)carray; //这就是我要转换的内容


我知道这不起作用。你有没有人建议转换

。这将在16位μc中实现。有没有使用sprintf或乘法之类的解决方案




提前谢谢

Densil

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

推荐答案

2004年4月15日星期四05:26:27 -0400,silentlights写道:
On Thu, 15 Apr 2004 05:26:27 -0400, silentlights wrote:

你能帮我把char数组转换成整数吗?我正在尝试一些
喜欢..

char carray [5];
int numb;

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

numb =(int)carray; //这就是我要转换的内容

我知道这不起作用。你有没有人建议进行转换。这将在16位μc中实现。是否有解决方案
没有使用sprintf或乘法之类的东西。
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.




转换后你想要什么''麻木'?


1515?

5151?

12?


其他东西
< br $>
-

NPV


大字印刷品,小字体带走

Tom Waits - 向上走吧



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


" silentlights" < SI ********** @ yahoo.co.uk>在消息中写道

news:89 ****************************** @ localhost.ta lkaboutprogramming。 com ...
"silentlights" <si**********@yahoo.co.uk> wrote in message
news:89******************************@localhost.ta lkaboutprogramming.com...
你能帮我把一个char数组转换成一个整数。我正在尝试一些
喜欢..

char carray [5];
int numb;

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

numb =(int)carray; //这就是我要转换的内容

我知道这不起作用。你有没有人建议进行转换。这将在16位c中实现。有没有使用sprintf或乘法之类的解决方案。
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终止字符串


numb = atoi(carray);

....

-

Stephen Sprunk愚蠢的人用智能环绕自己

CCIE#3723人。聪明的人围绕着他们自己与他们不同意的K5SSS聪明人。 --Aaron Sorkin



#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


silentlights写道:
silentlights wrote:
char carray [5];
int numb;

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

numb =(int)carray; //这就是我要转换的内容
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




您还可以做什么(但这只适用于32位微处理器)

carray [0] = 1;

carray [1] = 5;

carray [2] = 1;

carray [3 ] = 5;

numb =(int *)carray; // carray是一个char *已经把它变成了一个int

*


至于你提到的情况,如果int''是16位而char'是8

位这只会将数字设置为15或51,具体取决于你正在使用的机器的b / b
的字节顺序。


Ahmed



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


这篇关于将char数组转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆