• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

Convert text to bytes in vb.net

I have a field in a MySql database that contains the ascii values. For instance the field contains 41424344 which is the hex value for ABCD
I'm trying to place each hex value into a byte array as follows
array(0)=41
array(1)=42
array(2)=43
array(3)=44
I can split the text into the correct values i.e. 41 or 42 or 43 or 44 but I can't place them in the byte array unless I convert them to a byte which would then create the following array
array(0)=3431
array(1)=3432
array(2)=3433
array(3)=3434
This isn't what I need. Is there anyway I could do this?
Thanks
0
whiwex
Asked:
whiwex
  • 2
  • 2
1 Solution
 
HainKurtSr. System AnalystCommented:
use integer array ;) why do you need byte array?
0
 
HainKurtSr. System AnalystCommented:
I dont have any issue here...
        Dim a() As Byte
        ReDim a(10)
        a(0) = 41
        a(1) = 42
        a(2) = 43
        a(3) = 44

Open in new window

0
 
JRWoehlerCommented:
Is this what you're looking for?
private string hexToStringConverter(string hexValue)
        {
            int len = hexValue.Length / 2;
            string[] hexArray = new string[len];
            for (int i = 0; i < len; i++)
            {
                hexArray[i] = hexValue.Substring(i * 2, 2);
            }
            char[] charArray = System.Array.ConvertAll<string, char>(hexArray, hexToCharConverter);
            return new string(charArray);
        }

        private char hexToCharConverter(string value)
        {
            return (char)(Convert.ToInt32(value, 16));
        }

        private string stringToHexConverter(string value)
        {
            char[] charResult = value.ToCharArray();
            string[] hexResult = System.Array.ConvertAll<char, string>(charResult, charToHexConverter);
            string result = "";
            for (int i = 0; i < hexResult.Length; i++)
            {
                result += hexResult[i];
            }

            return result;
        }

        private string charToHexConverter(char value)
        {
            return ((byte)value).ToString("x");
        }

Open in new window

0
 
JRWoehlerCommented:
Here it is in VB.NET....
Private Function hexToStringConverter(ByVal hexValue As String) As String
    Dim len As Integer = hexValue.Length / 2
    Dim hexArray As String() = New String(len - 1) {}
    For i As Integer = 0 To len - 1
        hexArray(i) = hexValue.Substring(i * 2, 2)
    Next
    Dim charArray As Char() = System.Array.ConvertAll(Of String, Char)(hexArray, hexToCharConverter)
    Return New String(charArray)
End Function

Private Function hexToCharConverter(ByVal value As String) As Char
    Return CChar((Convert.ToInt32(value, 16)))
End Function

Private Function stringToHexConverter(ByVal value As String) As String
    Dim charResult As Char() = value.ToCharArray()
    Dim hexResult As String() = System.Array.ConvertAll(Of Char, String)(charResult, charToHexConverter)
    Dim result As String = ""
    For i As Integer = 0 To hexResult.Length - 1
        result += hexResult(i)
    Next
    
    Return result
End Function

Private Function charToHexConverter(ByVal value As Char) As String
    Return CByte(value).ToString("x")
End Function

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now