none
convert any string to 24 hexadecimal digits RRS feed

  • Question

  • Hi,

    I would like to ask, in C#, how can I convert any string to a 24 hexadecimal digits like this:

    XXXX-XXXX-XXXX-XXXX-XXXX-XXXX

    if the string was 1 character length or more or even more than 24 character length or even if it was 240 characters?


    Thanks
    Jassim
    Tuesday, December 16, 2014 7:40 PM

Answers

  • Here is a simple function that converts an ASCII string of up to 12 characters into 24 hex characters in the format you asked for. If the string is more than 12 characters, the function returns an empty string. If the string is less than 12 characters, the hex characters are padded on the right with "00".

    Private Function StringToHex(str As String) As String
        If str.Length > 12 Then Return ""
        Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(str)
        ReDim Preserve bytes(11)
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To 11
            If i > 0 And i Mod 2 = 0 Then sb.Append("-")
            sb.AppendFormat("{0:x2}", bytes(i))
        Next
        Return sb.ToString
    End Function

    You could use it to convert the contents of TextBox1 like this:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim hexString As String = StringToHex(TextBox1.Text)
        If hexString = "" Then
            MessageBox.Show("Input string is too long")
        Else
            MessageBox.Show(hexString)
        End If
    End Sub
    I have assumed that you need to be able to convert from string to hex and then hex back to the original string. If instead you just want a one-way hashing scheme where the same hex characters might represent many different strings, then you should look at the System.Security.Cryptography namespace. particularly the HashAlgorithm class.


    • Edited by Blackwood Tuesday, December 16, 2014 8:59 PM Add reference to System.Security.Cryptography
    • Marked as answer by Fred BaoModerator Monday, December 29, 2014 9:02 AM
    Tuesday, December 16, 2014 8:50 PM

All replies

  • You can't get something for nothing. 24 hexadecimal digits can only hold 8E+28 different values. A string of 24 characters made up of only the 26 upper case letters can have 9E+35 different values, 10 million times as many as a 24 digit hexadecimal string.

    You should be able come up with a way to represent any string you like in 24 hexadecimal digits as long as the string can have no more than 8E+28 (about 10 to the 29th power) different values. That would be about a 18 character string if the characters are limited to A-Z and 0-9.

    • Edited by Blackwood Tuesday, December 16, 2014 8:03 PM Add second paragraph.
    Tuesday, December 16, 2014 7:54 PM
  • Here is a simple function that converts an ASCII string of up to 12 characters into 24 hex characters in the format you asked for. If the string is more than 12 characters, the function returns an empty string. If the string is less than 12 characters, the hex characters are padded on the right with "00".

    Private Function StringToHex(str As String) As String
        If str.Length > 12 Then Return ""
        Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(str)
        ReDim Preserve bytes(11)
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To 11
            If i > 0 And i Mod 2 = 0 Then sb.Append("-")
            sb.AppendFormat("{0:x2}", bytes(i))
        Next
        Return sb.ToString
    End Function

    You could use it to convert the contents of TextBox1 like this:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim hexString As String = StringToHex(TextBox1.Text)
        If hexString = "" Then
            MessageBox.Show("Input string is too long")
        Else
            MessageBox.Show(hexString)
        End If
    End Sub
    I have assumed that you need to be able to convert from string to hex and then hex back to the original string. If instead you just want a one-way hashing scheme where the same hex characters might represent many different strings, then you should look at the System.Security.Cryptography namespace. particularly the HashAlgorithm class.


    • Edited by Blackwood Tuesday, December 16, 2014 8:59 PM Add reference to System.Security.Cryptography
    • Marked as answer by Fred BaoModerator Monday, December 29, 2014 9:02 AM
    Tuesday, December 16, 2014 8:50 PM
  • I actually want to encrypt the processor ID and/or other hardware ID then convert it into a 24 character as a license key.
    Tuesday, December 16, 2014 9:44 PM
  • I actually want to encrypt the processor ID and/or other hardware ID then convert it into a 24 character as a license key.

    Then perhaps you could use String.GetHashCode that creates a 32-bit integer from any string. If you want to display the integer as hex characters, you can use the following code. Note that the string is 8 hex characters since the integer is only 64 bits. You can change the X8 in the format string to X24 if you want, but the leading digits will all be zero.

    Dim licenseKey As Integer = idString.GetHashCode
    MessageBox.Show(String.Format("{0:X8}", licenseKey))
    

    I pointed out to you earlier that using hardware characteristics to create a licence key is not reliable. The user will find that your software no longer works after the hardware is upgraded.

    Tuesday, December 16, 2014 9:59 PM
  • I actually want to encrypt the processor ID and/or other hardware ID then convert it into a 24 character as a license key.

    Here is code to convert any string into an MD5 Hash (an array of 16 bytes) and to convert the MD5 hash to a hex string of the format XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX (32 hex characters).

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim idString As String = "SomeString"
        Dim key() As Byte = GetKey(idString)
        Dim hex As String = KeyToHex(key)
        MessageBox.Show(hex)
    End Sub
    
    Private Function GetKey(inputString As String) As Byte()
        Dim inputBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(inputString)
        Return New System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(inputBytes)
    End Function
    
    Private Function KeyToHex(key() As Byte) As String
        If key.Length <> 16 Then Return ""
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To key.Length - 1
            If i > 0 And i Mod 2 = 0 Then sb.Append("-")
            sb.AppendFormat("{0:x2}", key(i))
        Next
        Return sb.ToString
    End Function
    

    Tuesday, December 16, 2014 10:22 PM
  • Excellent...

    I have one more question please..

    that result will be my application serial number, correct?

    How can I get another number.. I don't know how to explain it but when you install Microsoft Applications it shows a set of alphanumeric which you need to give to Microsoft and using that code they will give you your serial number if you are not doing an online activation?

    Wednesday, December 17, 2014 5:53 PM
  • Excellent...

    I have one more question please..

    that result will be my application serial number, correct?

    How can I get another number.. I don't know how to explain it but when you install Microsoft Applications it shows a set of alphanumeric which you need to give to Microsoft and using that code they will give you your serial number if you are not doing an online activation?

    I don't know how Microsoft creates serial numbers and I doubt that they publish the details. 

    If what you want to do is use a combination of the user's hardware ID and a unique invoice number that identifies the sale of your product to this customer. You could do it like this.

    • Each time you sell your product, create a invoice number and send it to the customer. The invoice number could just be a simple incrementing number (1 for the first sale, 2 for the second), or you could append a random number if you like (1XXXXXXXX for the first sale, 2YYYYYYYY for the second sale, etc.).
    • When the user activates the product, create your serial number by concatenating the hardware ID string with the invoice number (as a string) and use that as the input to the GetKey function.
    Dim idString As String = hardwareId & invoiceNumber.ToString()
    Dim key() As Byte = GetKey(idString)
    Dim hex As String = KeyToHex(key)
    

    Wednesday, December 17, 2014 9:10 PM
  • but how can I assure that no fake customer will try to use the invoice number 2XXXXXXX to activate his downloaded product?

    Wednesday, December 17, 2014 10:39 PM
  • but how can I assure that no fake customer will try to use the invoice number 2XXXXXXX to activate his downloaded product?

    You will presumable have made a note of the fact that you sold invoice number 2XXXXXXXX and that it has not yet been activated. For additional security, you could include the invoice number on the CD or DVD that contains the product, or as part of the download if that is how you distribute your product. 

    Wednesday, December 17, 2014 10:54 PM