使用c#.net进行hex2utf8转换 [英] hex2utf8 conversion using c#.net

查看:404
本文介绍了使用c#.net进行hex2utf8转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在项目中使用串行端口通信,其中使用串行端口发送4k数据.

最后2个字节是我发送到天线的所有字节的总和,大于(大于)7F.
ASCII代码无法识别该值,因此我想使用UTF8编码实现传输.
我需要将十六进制转换为UTF8,反之亦然的代码.

请帮帮我

Hi,

I am using serial port communication in my project, wherein I am sending a 4k of data using serial port.

The last but 2 bytes are the sum of all bytes I send to the antenna, which is more than (greater than) 7F.
The value is not recognisible by the ASCII code, so I want to implement the transfer using UTF8 encoding.
I need the code for converting hex to UTF8 and vice versa.

Please Help me out

推荐答案

为什么不只使用8位ASCII?如果您在通过串行端口传输大于0x7F的值时遇到问题,请检查您使用的是8位数据,而不是7位.如果不是,则将编码从ASCII更改为UTF8至少没有帮助.天线期望什么?
Why not just use 8 bit ASCII? If you are having problems transferring values greater that 0x7F via the serial port, then check you are using 8 bit data, rather than 7. If you are not, then changing encoding from ASCII to UTF8 will not help in the least. What is the antenna expecting?


感谢您的答复,但问题是我将以
的形式发送数据
例如:3A 54 31 32 33 ..... 9B 82 0D 0A

这里带下划线的数据不过是iam发送到天线的所有字节的总和(不包括3A,0D,0A:这些是通信的开始和结束)
最后但2个字节是完成身份验证的校验和,因此必须将其直接发送到天线.
我不知道ASCII的8位,只是让我知道.
Thank u for the reply but the problem is i will send the data as

Ex: 3A 54 31 32 33 ..... 9B 82 0D 0A

here the underlined data is nothing but the sum of all the bytes iam sending to the antenna (excluding 3A, 0D, 0A: those are start and end of communication)
the last but 2 bytes are the checksum by which the authentication is done, so it must be sent as it is to the antenna.
I don''t know about 8 bit of ASCII just let me know about it.
SerialPort.Write(hex2ascii(3A5431323334353637383942545695965835679BA50D0A));
Thread.sleep(500);
string resp = SerialPort.ReadExisting;
response = resp.Length;
string token = resp.substring(14,1);
if(token == 04)
{
.....
} 


resp是天线的响应,在这里它给出了数据,我必须拆分并找到校验和并验证数据,然后在天线的响应正确后开始传输数据.


the resp is the response from the antenna here it gives the data and i have to split and find the checksum and validate the data and start transfer of the data after the response from the antenna is correct


继续 OriginalGriff 答案

这是用于将编码从ascii更改为utf的代码
Continue to OriginalGriff Answer

Here is the code for you to change the encoding from ascii to utf
SerialPort port = new SerialPort();
port.Encoding  = System.Text.Encoding.GetEncoding(1252);


这篇关于使用c#.net进行hex2utf8转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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