将char数组转换为整数

将char数组转换为整数

本文介绍了将char数组转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你能帮我把一个char数组转换成一个整数。我正在尝试的东西 喜欢.. char carray [5]; int numb; carray [0] = 1; carray [1] = 5; carray [2] = 1; carray [3] = 5; numb =(int)carray; //这就是我要转换的内容 我知道这不起作用。你有没有人建议转换 。这将在16位μc中实现。有没有使用sprintf或乘法之类的解决方案 。 提前谢谢 DensilHi,Can you help me conver a char array into an integer. I am trying somethinglike..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 convertI know this doesnt work. Does anyone of you have a suggestion for thatconversion. This is to be implemented in a 16bit μc. Is there a solutionwithout using anything like sprintf or multiplication.Thanks in advanceDensil推荐答案 转换后你想要什么''麻木'? 1515? 5151? 12? 其他东西 - 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 #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 stringnumb = atoi(carray);....--Stephen Sprunk "Stupid people surround themselves with smartCCIE #3723 people. Smart people surround themselves withK5SSS smart people who disagree with them." --Aaron Sorkin 您还可以做什么(但这只适用于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 的字节顺序。 AhmedWhat 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 8bits this will only set num to either 15 or 51 depending on the endianity ofthe machine you''re using.Ahmed 这篇关于将char数组转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-31 05:01