如何在arduino中将hex转换为ascii [英] How convert hex to ascii in arduino

查看:1308
本文介绍了如何在arduino中将hex转换为ascii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不从下面串行Rx引脚十六进制值。



<预郎= 文本> 006900730020006D0065007300730061006700650020007700610073002000730065006E007400200074006F00200079006F0075002000620079002000310030002D0053007400720069006B00650020004E006500740077006F0072006B0020004D006F006E00690074006F007200200066006F007200200074006800650020





如何将它作为ASCII类型存储到String变量中?



我尝试了什么:



i尝试这种方式。但没有成功



 loop(){
String d,full_time;
char c;
while (Serial1.available()){
c = Serial1.read();
d + = c;
}

full_time =( String )strtol(& d [ 1 ],NULL, 16 );
}

解决方案

请看我对这个问题的评论。



你做错了。你不能做 d + = c; ,你必须有一个字符串,或更好的固定大小的数组(给2或4个字符的空间)并填写它在内循环中输入字符。在内循环的每次迭代中,将由此数组定义的字符串(可能需要添加结束零)解析为ASCII代码点的字节值。



一些不确定性是因为不知道你的语言是什么以及你正在使用或可以使用的库 - 请参阅我的第二条评论。 :-)

但我希望你能掌握这个想法。



-SA


你的arduino板正在接收类似的东西:

10-Strike Network Monitor为你发送了消息



可能编码为UTF-16。由于它只是消息的一部分,因此难以理解字节顺序(大或小端)。

根据经验,您可以仅使用2 nd ,4 th ,6 th ..字符,即

 loop(){
字符串消息;
char c;
int count = 0;
while(Serial1.available()){
c = Serial1.read();
if(count%2)message + = c;
count ++;
}
}


i had below Hex value from serial Rx pin.

006900730020006D0065007300730061006700650020007700610073002000730065006E007400200074006F00200079006F0075002000620079002000310030002D0053007400720069006B00650020004E006500740077006F0072006B0020004D006F006E00690074006F007200200066006F007200200074006800650020



How store it in to String variable as ASCII type?

What I have tried:

i tried this way. but not success

loop(){
 String d,full_time ;
  char c ;
  while ( Serial1.available() ) {
    c = Serial1.read();
    d += c;
  }
 
  full_time = (String) strtol( &d[1], NULL, 16);
}

解决方案

Please see my comments to the question.

You are doing it wrong. You cannot do d += c;, you have to have a string, or, better, fixed-size array (giving a room for 2 or 4 characters) and fill it with input characters in an inner cycle. On each iteration of inner cycle, parse the string defined by this array (you may need to add closing zero) into a byte value of ASCII code point.

Some uncertainty comes from the fact that it's not known what is your language and what libraries you are using or can use — please see my second comment. :-)
But I hope you can grasp the idea.

—SA


Your arduino board is receiveing somthing like:

"is message was sent to you by 10-Strike Network Monitor for the "


probably encode as UTF-16. Since it is just a piece of the message it is difficult to understand the byte order (big or little endian).
Empirically, you may build your string using just the 2nd,4th,6th.. characters, i.e.

loop(){
 String message;
  char c ;
  int count = 0;
  while ( Serial1.available() ) {
    c = Serial1.read();
    if (count % 2) message += c;
    count++;
  } 
}


这篇关于如何在arduino中将hex转换为ascii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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