C# - 将一串十六进制值转换为十六进制 [英] C# - Convert a string of hex values to hex

查看:145
本文介绍了C# - 将一串十六进制值转换为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来很奇怪,但我的问题是,我从文本文件中获得了十六进制值的文本字符串,如下所示:

This might sound odd, but my issue is that I have a text string of hex values from a text file, like so:

"0x0f, 0x40, 0xff, ...."

数组被分隔符分割,但我现在需要做的是在十六进制中有一个字节数组:

I have stored them in an array split by the delimiters, but what I now need to do is have a byte array of what thay are in hex:

stringArray[0] = "0x0f";

byteArray[0] = 0x0f;

我该怎么做(用户可以加载文本文件,所以我不知道是什么值是),是否有某种算术我可以使用?

How do I do this (the user can load the text file, so I don't know what the values are), is there some sort of arithmetic I can use?

推荐答案

你只需要解析每个字符串。因为每一个只有一个值,你可以这样做:

You just have to parse each string. Because each one is already only one value, you can do this:

byte b;
if (byte.TryParse(s, NumberStyles.HexNumber, 
    CultureInfo.InvariantCulture.NumberFormat, out b)) 
{
    // b contains the value.
}

其中s是要解析的字符串,b是结果值。

where s is the string you want to parse, and b is the resulting value.

这篇关于C# - 将一串十六进制值转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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