HEX到ASCII转换 [英] HEX to ASCII conversion

查看:90
本文介绍了HEX到ASCII转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#转换窗体时有十六进制形式的数据它给出了

名称

手机

CNIC

R-ID



姓名:Wajahat Abbas

35 37 20 36 31 20 36 41 20 36 31 20 36 38 20 36 31 20 37 34 20 32 30 20 34 31 20 36 32 20 36 32 20 36 31 20 37 33 20 30 30 20 30 30 20 30 30 20



Cellno: 03455172815

33 30 20 33 33 20 33 34 20 33 35 20 33 35 20 33 31 20 33 37 20 33 32 20 33 38 20 33 31 20 33 35 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30



CNIC:61101-0310105-1

33 36 20 33 31 20 33 31 20 33 30 20 33 31 20 32 44 20 33 30 20 33 33 20 33 31 20 33 30 20 33 31 20 33 30 20 33 35 20 32 44 20 33 31 20 30 30



ID:T-000039

35 34 20 32 44 20 33 30 20 33 30 20 33 30 20 33 30 20 33 33 20 33 39 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30



所有数据以16字节存在,例如name = 16 bytes,cell = 16 by测试...

如果我想用六进制转换它我怎么能得到字节,例如我想要选择数据

从1到16是NAME,然后17到32是冷杉CELL ....依此类推

在代码中指导我如何按顺序获取这些字节,以便我可以在数据库中添加数据。

谢谢

解决方案

以下代码将十六进制字符串转换为实际值。您可能需要根据源字符串的格式进行修改。

  //   source = hex bytes  
int i = 0 ;
int index = 0 ;
byte [] hexBytes = new byte [source.Length];
执行
{
string hs = source.Substring(index , 2 );
hexBytes [i] =( byte int .Parse(hs,System.Globalization。 NumberStyles.AllowHexSpecifier);
i ++;
index + = 2 ;
} while (index < source.Length);


  public   static   string  from_hex( string  s)
{
StringBuilder sb = new StringBuilder();
string [] a = s.Split( new char [] {' '});
foreach var h in a)
{
sb.Append(( char int .Parse (h,System.Globalization.NumberStyles.HexNumber));
}
return sb.ToString();
}



应该可以解决问题。

但请注意,在您的示例中,十六进制数据与文本不匹配。


I have data in hex form when i convert in C# windows form it give
Name
Mobile
CNIC
R-ID

Name:Wajahat Abbas
35 37 20 36 31 20 36 41 20 36 31 20 36 38 20 36 31 20 37 34 20 32 30 20 34 31 20 36 32 20 36 32 20 36 31 20 37 33 20 30 30 20 30 30 20 30 30 20

Cellno:03455172815
33 30 20 33 33 20 33 34 20 33 35 20 33 35 20 33 31 20 33 37 20 33 32 20 33 38 20 33 31 20 33 35 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30

CNIC:61101-0310105-1
33 36 20 33 31 20 33 31 20 33 30 20 33 31 20 32 44 20 33 30 20 33 33 20 33 31 20 33 30 20 33 31 20 33 30 20 33 35 20 32 44 20 33 31 20 30 30

ID:T-000039
35 34 20 32 44 20 33 30 20 33 30 20 33 30 20 33 30 20 33 33 20 33 39 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30

All data present in 16 bytes for e.g name = 16 bytes , cell = 16 bytes ...
If i want to convert it in hexa how can i get the bytes for e.g i want to pick the data
from 1 to 16 is for NAME, then 17 to 32 is fir CELL .... and so on
Guide me in a code how i get this bytes in sequence so that i can add the data in database.
Thanks

解决方案

The following code will convert a string of hex characters into their actual values. You may need to modify according to the format of your source string.

// source = hex bytes 
int i = 0;
int index = 0;
byte[] hexBytes = new byte[source.Length];
do
{
    string hs = source.Substring(index, 2);
    hexBytes[i] = (byte)int.Parse(hs, System.Globalization.NumberStyles.AllowHexSpecifier);
    i++;
    index += 2;
} while (index < source.Length);


This

public static string from_hex(string s)
    {
      StringBuilder sb = new StringBuilder();
      string [] a = s.Split( new  char[]{' '});
      foreach (var h in a)
      {
        sb.Append((char) int.Parse(h, System.Globalization.NumberStyles.HexNumber));
      }
      return sb.ToString();
    }


should do the trick.
Please note, however, in your examples, hexadecimal data do NOT match with text.


这篇关于HEX到ASCII转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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