|
老哥们能帮忙看看代码哪里有问题么?
十进制转十六进制
const int inByte;
String tohex(int n) {
if (n == 0) {
return "00"; //n为0
}
String result = "";
char _16[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
const int radix = 16;
while (n) {
int i = n % radix; // 余数
result = _16[i] + result; // 将余数对应的十六进制数字加入结果
n /= radix; // 除以16获得商,最为下一轮的被除数
}
if (result.length() < 2) {
result = '0' + result; //不足两位补零
}
return result;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(inByte,HEX);
delay(3000);
}
|
|