将字符串转换为该字符串的等效十六进制

将字符串转换为该字符串的等效十六进制

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

问题描述

我想将我输入的字符串转换为等效的十六进制。



例如:

我有字符串D7 strong>,我希望它被转换为十六进制D7 ,以便只能读取十六进制值的软件读取它。



bla bla bla =错误,因为它没有相当于它的十六进制
$ b aBc = ABC ,因为它有一个十六进制等价物。



123 = 123



12 AB 3.14 = 错误,因为它没有十六进制等同物



3.F1 = 错误,因为没有相当于它的十六进制元素

每个
文本的十六进制等价物,那么它就会出错。



编辑:我试图将C#代码德米特里,但它仍然没有工作。我会在星期一再试一次



以下是代码

  Dim source As String =abc789Def

Dim Sb As New StringBuilder(source.Length)

对于每个c作为字符在源
如果((c> =0并且还有c =a并且还有c =A并且还有c Sb.Append(Char.ToUpper(c))
Dim result As String = Sb.ToString


Console.WriteLine(result&结果)
其他
Console.Write(Error)
结束如果
下一个


解决方案

通过使用System.Globalization.NumberStyles.HexNumber样式的整数类型的TryParse方法提供验证十六进制数的能力。

  Dim inputValue As String =23
Dim returnValue As String = Nothing
如果ValidateAndFormatHex(inputValue ,returnValue)Then
Console.WriteLine(returnValue)
else
Console.WriteLine(Error)
End If






  Private Shared Function ValidateAndFormatHex(input As String,ByRef formattedValue As String)作为布尔
input = input.Trim()。ToUpperInvariant
Dim result As Int32
Dim parses As Boolean = Int32.TryParse(input,System.Globalization.NumberStyles.HexNumber,System.Globalization。 CultureInfo.InvariantCulture,result)
如果解析然后formattedValue =输入
返回解析
End Function


I would like to convert the string that I've inputted to it's equivalent hex.

For example:

I have the string D7 and I would like it to be converted to hex D7 so that it can be read by the software that can only read hex values.

bla-bla-bla = Error, because there's no hex equivalent of it

aBc = ABC because there's a hex equivalent of it.

123 = 123 because there's a hex equivalent of it.

12 AB 3.14 = Error, because there's no hex equivalent of it

3.F1 = Error, because there's no hex equivalent of it

Though I'm not sure about that, but I guess that will be the result. As long as there's no hex equivalent of each text then it will be error.

Edit: I've tried to convert the C# code of Dmitry, but it is still not working. I'll try it again on Monday

Here's the code

Dim source As String = "abc789Def"

Dim Sb As New StringBuilder(source.Length)

For Each c As Char In source
    If ((c >= "0" AndAlso c <= "9") Or (c >= "a" AndAlso c <= "f") Or (c >= "A" AndAlso c <= "F")) Then
        Sb.Append(Char.ToUpper(c))
        Dim result As String = Sb.ToString


        Console.WriteLine("result " & result)
    Else
        Console.Write("Error")
    End If
Next
解决方案

The ability to validate a Hex number is provided by the TryParse method on integer types using the System.Globalization.NumberStyles.HexNumber style.

Dim inputValue As String = "23"
Dim returnValue As String = Nothing
If ValidateAndFormatHex(inputValue, returnValue) Then
   Console.WriteLine(returnValue)
Else
   Console.WriteLine("Error")
End If


Private Shared Function ValidateAndFormatHex(input As String, ByRef formattedValue As String) As Boolean
   input = input.Trim().ToUpperInvariant
   Dim result As Int32
   Dim parses As Boolean = Int32.TryParse(input, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, result)
   If parses Then formattedValue = input
   Return parses
End Function

这篇关于将字符串转换为该字符串的等效十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:38