Convert text to bytes in

Posted on 2010-01-08
Last Modified: 2012-05-08
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
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
This isn't what I need. Is there anyway I could do this?
Question by:whiwex
    LVL 51

    Expert Comment

    use integer array ;) why do you need byte array?
    LVL 51

    Expert Comment

    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

    LVL 2

    Expert Comment

    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

    LVL 2

    Accepted Solution

    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)
        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)
        Return result
    End Function
    Private Function charToHexConverter(ByVal value As Char) As String
        Return CByte(value).ToString("x")
    End Function

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now