十六进制转换为float 等进制转换问题

一、进制转换

开始做进制转换的时候,将string 转换为double 再将double转换为 float,然后发现十六进制中的字母转换为double是不成功的,失败的,所以进行小小探索。

 string hexString = "3d8f5c29";

  uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);

   byte[] floatVals = BitConverter.GetBytes(num);

   float f = BitConverter.ToSingle(floatVals, 0);

  Console.WriteLine("float convert = {0}", f);

2.把数组转换成16进制字符串  string stringdata = BitConverter.ToString(datashow);

3.获取字符串中每个字符的十六进制值。 首先,它将 string 分析为字符数组,然后对每个字符调用 ToInt32(Char) 以获取相应的数字值。最后,在 string 中将数字的格式设置为十六进制表示形式。 

string input = "Hello World!";
char[] values = input.ToCharArray();
foreach (char letter in values)
{
    // Get the integral value of the character.
    int value = Convert.ToInt32(letter);
    // Convert the decimal value to a hexadecimal value in string form.
    string hexOutput = String.Format("{0:X}", value);
    Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
}

输出:Hexadecimal value of H is 48 ;Hexadecimal value of e is 65 ;Hexadecimal value of l is 6C ;
Hexadecimal value of l is 6C ;Hexadecimal value of o is 6F ;Hexadecimal value of is 20 ;Hexadecimal value of W is 57 
Hexadecimal value of o is 6F ;Hexadecimal value of r is 72 ;Hexadecimal value of l is 6C ;Hexadecimal value of d is 64 
Hexadecimal value of ! is 21  ;

4.十六进制值的 string 并输出对应于每个十六进制值的字符

string hexValues = "48 65 6C 6C 6F 20 57 6F 72 6C 64 21";
string[] hexValuesSplit = hexValues.Split(' ');
foreach (String hex in hexValuesSplit)
{
    // Convert the number expressed in base-16 to an integer.
    int value = Convert.ToInt32(hex, 16);
    // Get the character corresponding to the integral value.
    string stringValue = Char.ConvertFromUtf32(value);
    char charValue = (char)value;
    Console.WriteLine("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
    hex, value, stringValue, charValue);
}

Output: 
hexadecimal value = 48, int value = 72, char value = H or H 
hexadecimal value = 65, int value = 101, char value = e or e 
hexadecimal value = 6C, int value = 108, char value = l or l 
hexadecimal value = 6C, int value = 108, char value = l or l 
hexadecimal value = 6F, int value = 111, char value = o or o 
hexadecimal value = 20, int value = 32, char value = or 
hexadecimal value = 57, int value = 87, char value = W or W 
hexadecimal value = 6F, int value = 111, char value = o or o 
hexadecimal value = 72, int value = 114, char value = r or r 
hexadecimal value = 6C, int value = 108, char value = l or l 
hexadecimal value = 64, int value = 100, char value = d or d 
hexadecimal value = 21, int value = 33, char value = ! or ! 

5.

将十六进制 string 转换为整数的另一种方法,

即调用 Parse(String, NumberStyles) 方法。 

C# 代码:

string hexString = "8E2";
int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(num);     Output: 2274

6.

使用 System..::.BitConverter 类和 Int32..::.Parse 方法将十六进制 string 转换为浮点型。 
C# 代码:string hexString = "43480170";

uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);

byte[] floatVals = BitConverter.GetBytes(num);
float f = BitConverter.ToSingle(floatVals, 0);
Console.WriteLine("float convert = {0}", f);         Output: 200.0056

7.

使用 System..::.BitConverter 类将字节数组转换为十六进制字符串。 

C#代码:

byte[] vals = { 0x01, 0xAA, 0xB1, 0xDC, 0x10, 0xDD };

string str = BitConverter.ToString(vals);
Console.WriteLine(str);
str = BitConverter.ToString(vals).Replace("-", "");
Console.WriteLine(str);

Output: 01-AA-B1-DC-10-DD ;01AAB1DC10DD 

8.

public string StrToHex(string mStr) //返回处理后的十六进制字符串
{
    return BitConverter.ToString(
    ASCIIEncoding.Default.GetBytes(mStr)).Replace("-", " ");

/* StrToHex */
public string HexToStr(string mHex) // 返回十六进制代表的字符串
{
    mHex = mHex.Replace(" ", "");
    if (mHex.Length <= 0) return "";
    byte[] vBytes = new byte[mHex.Length / 2];
    for (int i = 0; i < mHex.Length; i += 2)
    if (!byte.TryParse(mHex.Substring(i, 2), NumberStyles.HexNumber, null, out vBytes[i / 2]))
    vBytes[i / 2] = 0;
    return ASCIIEncoding.Default.GetString(vBytes);

 

 

 

本文转自https://blog.csdn.net/qingfeng45697/article/details/78557521

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值