locked
convert a non nummerical string to its ascii hexadecimal representation RRS feed

  • Question

  • User2110873642 posted

    hello, i have a string that looks like this. it contains numbers as well as letters.

    987654321012013:07:16-09:57:081.00826TopSecret


    how do i convert that string above to its ascii hexadecimal representation:

    3938373635343332313031323031333a30373a31362d30393a35373a3038312e3030383236546f705365637



    ive tried different kinds of ways to convert, but it always gives me another outcome.

    in php it simply goes as bin2hex() but i dont know how it actually interprets letters to hex.

    and secondly, how do i convert back that hex to its normal string format?

    Thursday, February 7, 2019 5:38 PM

Answers

  • User2110873642 posted

    thanks, that was it. but also, i had to use chrw instead of cchar

    Dim chars As Char() = "987654321012013:07:16-09:57:081.00826TopSecret".ToCharArray()
            Dim hexString As String = String.Empty
    
            For Each letter As Char In chars
                Dim value As Integer = Convert.ToInt32(letter)
                hexString += $"{Convert.ToInt32(letter):X}"
            Next
    
            Console.WriteLine(hexString)
            Dim final As String = String.Empty
            Dim k As Integer = 0
            Dim hexStrings = hexString.ToCharArray().ToLookup(Function(c) Math.Floor(CDec((Math.Min(System.Threading.Interlocked.Increment(k), k - 1) / 2)))).[Select](Function(e) New String(e.ToArray()))
    
            For Each s As String In hexStrings
                final &= ChrW(Int32.Parse(s, Globalization.NumberStyles.HexNumber))
            Next
    
            Console.WriteLine(final)

    thank you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 9, 2019 3:52 PM

All replies

  • User475983607 posted

    Your example is missing a few characters.

                char[] chars = "987654321012013:07:16-09:57:081.00826TopSecret".ToCharArray();
                string hexString = string.Empty;
                foreach (char letter in chars)
                {
                    int value = Convert.ToInt32(letter);
                    hexString += $"{Convert.ToInt32(letter):X}";
                }
                Console.WriteLine(hexString);
    
    
                string final = string.Empty;
                int k = 0;
                var hexStrings = hexString.ToCharArray().ToLookup(c => Math.Floor((decimal)(k++ / 2))).Select(e => new string(e.ToArray()));
                foreach (string s in hexStrings)
                {
                    final += (char)Int32.Parse(s, System.Globalization.NumberStyles.HexNumber);
                }
                Console.WriteLine(final);
            }

    results

    3938373635343332313031323031333A30373A31362D30393A35373A3038312E3030383236546F70536563726574
    987654321012013:07:16-09:57:081.00826TopSecret
    Press any key to continue . . .

    Reference docs

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types

    Thursday, February 7, 2019 7:37 PM
  • User2110873642 posted

    thank you so much.

    usually this isnt a problem, but for some reason it doesnt give the same results in VB. do you have any idea why?

    Dim chars As Char() = "987654321012013:07:16-09:57:081.00826TopSecret".ToCharArray()
    Dim hexString As String = String.Empty
    
    For Each letter As Char In chars
    Dim value As Integer = Convert.ToInt32(letter)
    hexString += $"{Convert.ToInt32(letter)}"
    Next
    
    Console.WriteLine(hexString)
    Dim final As String = String.Empty
    Dim k As Integer = 0
    Dim hexStrings = hexString.ToCharArray().ToLookup(Function(c) Math.Floor(CDec((Math.Min(System.Threading.Interlocked.Increment(k), k - 1) / 2)))).[Select](Function(e) New String(e.ToArray()))
    
    For Each s As String In hexStrings
    final &= CChar(Int32.Parse(s, Globalization.NumberStyles.HexNumber).ToString)
    Next
    
    Console.WriteLine(final)

    output:

    57565554535251504948495048495158485558495445485758535558485649464848565054841111128310199114101116
    8888888877787788788786788888787777888111111212112
    Press any key to continue . . .

    Thursday, February 7, 2019 10:08 PM
  • User475983607 posted

    Missing Hex formatter

            For Each letter As Char In chars
                Dim value As Integer = Convert.ToInt32(letter)
                hexString += $"{Convert.ToInt32(letter):X}"
            Next

    Thursday, February 7, 2019 11:32 PM
  • User2110873642 posted

    thanks, that was it. but also, i had to use chrw instead of cchar

    Dim chars As Char() = "987654321012013:07:16-09:57:081.00826TopSecret".ToCharArray()
            Dim hexString As String = String.Empty
    
            For Each letter As Char In chars
                Dim value As Integer = Convert.ToInt32(letter)
                hexString += $"{Convert.ToInt32(letter):X}"
            Next
    
            Console.WriteLine(hexString)
            Dim final As String = String.Empty
            Dim k As Integer = 0
            Dim hexStrings = hexString.ToCharArray().ToLookup(Function(c) Math.Floor(CDec((Math.Min(System.Threading.Interlocked.Increment(k), k - 1) / 2)))).[Select](Function(e) New String(e.ToArray()))
    
            For Each s As String In hexStrings
                final &= ChrW(Int32.Parse(s, Globalization.NumberStyles.HexNumber))
            Next
    
            Console.WriteLine(final)

    thank you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 9, 2019 3:52 PM