Microchip
直播中

郑雅颖

7年用户 198经验值
私信 关注
[问答]

atoi转换错了?

我有下面的变量BabyCar NoMeNoT(10);未设置的TunMurnPosiv[5]=4;NoMultPosie[4]=9;NoMultPosi[3 ]=4;NoMultPoo[4 ]=9;NoMultPosi[O]=I.;NoMultPosi[O]=I.;NoMultPosi[O]=I.;NoMultPosi[y]=I.;然后我做了。NeWAtiON= ATOI(NUMLBONT);结果是0x00 00 7CBF,但我预期0xFFFF7CBFF如果我设置了No.RealPosiv4到424950495,我就得到0xFFFFEB5F,就像我预期的那样,为什么我在一个案例中得到0x000而不是0xFFFF,而另一个它工作得很好呢?

以上来自于百度翻译


      以下为原文

    I have the following variables
char numberInput[10];
unsigned long newNumber = 0;

I set numberInput to
numberInput[0] = 4;
numberInput[1] = 2;
numberInput[2] = 9;
numberInput[3] = 4;
numberInput[4] = 9;
numberInput[5] = 3;
numberInput[6] = 3;
numberInput[7] = 6;
numberInput[8] = 9;
numberInput[9] = 5;

Then I do newNumber = atoi(numberInput);
The result is 0x00007CBF but I was expecting 0xFFFF7CBF

If I set numberInput to 4294950495 I get 0xFFFFBE5F like I would expect

Why am I getting 0x0000 instead of 0xFFFF in one case but in the other it works fine?

回帖(10)

苗雨

2018-10-15 16:22:51
据我所知,这不是ATOI工作的方式。首先,您需要为NUBLIN输入数组中的空终止符留出空间。接下来,试试这样的事情:

以上来自于百度翻译


      以下为原文

    To my understanding, that is not how atoi works. First, you'll need space for the null terminator in your numberInput array. Next, try something like this:
 

char numberInput[11];
numberInput[0] = '4';
numberInput[1] = '2';
numberInput[2] = '9';
numberInput[3] = '4';
numberInput[4] = '9';
numberInput[5] = '3';
numberInput[6] = '3';
numberInput[7] =' 6';
numberInput[8] = '9';
numberInput[9] = '5';
numberInput[10] = '';
举报

杨伟

2018-10-15 16:32:12
首先,你没有终止你的字符串。最后你需要一个零。而且输入数需要允许长度为零。它可能只是工作,因为你碰巧跟随另一个变量,一开始就被初始化为零。ATO()返回INT而不是未签名的long。使用正确的工作功能!

以上来自于百度翻译


      以下为原文

    First you have not terminated your string.  You need a zero on the end.  And also a InputNumber needs to allow for that zero in the length. Its is probably only just working because you happen to follow that variable with another that happens to by initialized to zero at the start!
 
atoi() returns an int not an unsigned long.  Use the correct function for the job!
举报

李涛

2018-10-15 16:42:13
INTX= ATOI(“295”);//ASCII到INTEGHYLY=ATOL(“4294933695”);//ASCII到龙龙Z= ATOL(“4294933695”);PrINTF(“签名%LIR”,Z);PrINTF(“未签名的%LUR”,Z);

以上来自于百度翻译


      以下为原文

     
int x = atoi("295");                 //ascii to integer
long y = atol("4294933695");    //ascii to long
 
long z = atol("4294933695");
printf("signed %lir",z);
printf("unsigned %lur",z);
举报

李维兴

2018-10-15 17:00:48
首先,我认为OP有一个空终止的字符串,只是在他的帖子中重新键入错误;否则,他不会得到他发布的结果。@ OP,你的结果的原因是因为你使用了错误的转换函数。ATOI()函数将字符串转换为16位整数。4294933695的下16位是0x7CBF,符号在分配给未签名的long之前扩展到0x000。7CBF,而4294950495的16位是0xBE5F,符号扩展到0xFFFFEB5F。对于您的输入号,需要使用将字符串转换成32位长整数的AtOLL()函数。

以上来自于百度翻译


      以下为原文

   
First, I think OP does have a null-terminated string and just retyped wrong in his post; otherwise, he would not get the results that he has posted.
 
@OP, the reason for your results is because you used the wrong conversion function. The atoi() function converts a character string to a 16-bit integer. The lower 16-bit of 4294933695 is 0x7CBF and sign-extends to 0x00007CBF before assigning to the unsigned long, while the lower 16-bit of 4294950495 is 0xBE5F and sign-extends to 0xFFFFBE5F.
 
For your input numbers, you need to use the atol() function that converts a string to a 32-bit long integer.
举报

更多回帖

发帖
×
20
完善资料,
赚取积分