转换十六进制字符串中的Arduino为十进制 [英] convert HEX string to Decimal in arduino

查看:6273
本文介绍了转换十六进制字符串中的Arduino为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个十六进制字符串这样的:0005607947,并希望将其转换为十进制数,我测试它的 这个网站 并正确转换为十进制数,答案是:90208583,但是当我用这个codeI得到错误的价值!
我的code哪里错了还是没有任何一个,一些新的code这个问题?

 长整型decimal_answer = getDEC(0005607947);长整型getDEC(字符串str110){
   长整型ID = 0;
   INT LEN = str110.length();
   字符的buff [LEN]
   INT功率= 0;   对(INT I = 0; I&下; len个;我++){抛光轮[I] = str110.charAt(ⅰ); }   对(INT I =(LEN-1); I> = 0;我 - ){
      INT NUM = BUFF [I] - '0';
      ID = ID + NUM *战俘(16,功率);
      功率=功率+ 1;
     }
    Serial.println(字符串(ID,DEC));
  返回ID;
}//感谢,我也用这个,但得到错误:从'无效*'无效转换的char **'[-fpermissive]
unsigned int类型SIZE = sizeof的(F_value);
焦炭charBuf [SIZE]
F_value.toCharArray(charBuf,SIZE);长decimal_answer =与strtol(charBuf,NULL,16);
Serial.println(decimal_answer,DEC);


解决方案

您正试图将价值90208583存储在一个int。 Arduino的有2个字节INT大小的含义,你可以存储最多为2 ^ 16-1(65535)。你有两个选择:


  1. 使用一个unsigned int

    • 分钟数:0

    • 最大数量:4,294,967,295

    • 缺点:只能用于正数


  2. 使用一个长整型

    • 分钟数:2,147,483,648

    • 最大数量:2,147,483,647


i have an Hex String like this : "0005607947" and want to convert it to Decimal number , i test it on this site and it correctly convert to decimal number and answer is : "90208583" but when i use this code i get wrong value ! where of my code is wrong or did have any one , some new code for this problem ?

long int decimal_answer = getDEC("0005607947") ;

long int getDEC(String str110) {
   long int ID = 0 ;
   int len = str110.length() ;
   char buff[len] ;
   int power = 0 ;

   for(int i = 0 ; i <len ; i++) {  buff[i] = str110.charAt(i); }

   for(int i = (len-1) ; i >=0 ; i--) { 
      int num = buff[i] - '0' ;
      ID = ID + num * pow(16 , power) ;
      power = power + 1 ;   
     }
    Serial.println(String(ID , DEC));
  return ID ;
}



// thanks , i also use this but , get error : invalid conversion from 'void*' to  'char**' [-fpermissive]
unsigned int SiZe = sizeof(F_value) ;
char charBuf[SiZe];
F_value.toCharArray(charBuf , SiZe);

long decimal_answer = strtol(charBuf , NULL , 16);
Serial.println(decimal_answer , DEC);

解决方案

You are trying to store the value 90208583 in an int. Arduino has a 2 byte int size meaning that the largest number you can store is 2^16-1 (65535). You have a couple of options:

  1. Use an unsigned int
    • min number: 0
    • max number: 4,294,967,295
    • cons: can only be used for positive numbers
  2. Use a long int
    • min number: -2,147,483,648
    • max number: 2,147,483,647

这篇关于转换十六进制字符串中的Arduino为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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