1

I'm communicating with a device that requires a password to be embedded into the command using hex. The password must be 8 bytes. If the ASCII numeric password is less then 16 digits the password is padded with a F.

For example if the password is 12345 I need to be able to convert the password to 0x12 0x34 0x5F 0xFF 0xFF 0xFF 0xFF 0xFF.

What I am not sure how to do is get 0x12 from "12".

I was thinking about if I could convert 1 to 0x10 and 2 to 0x02 I could then add the two bytes together to get 0x12. However is there a way to get the 0x10 and 0x02 without having to create a hash table or is there a better approach?

Edit: To whomever was rude enough to down vote. This is a unique question I'm not looking to do simple get the hex value of 12345 where as hex it would be represented as 31 32 33 34 35. If one of the other answers in that question does cover this, this at least presents the answer to the question where the questioned is presented directly. I intend to look into the bit shifting and the answers to the linked question flew right over my head. If this site is intended to facilitate learning then we cannot have a catch all section because the answer becomes too complex to help a layman learn.

Daisy
  • 121
  • 11
  • Possible duplicate of [How do you convert a byte array to a hexadecimal string, and vice versa?](https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa) – Jeroen Mostert May 20 '19 at 15:14
  • 1
    Convert the character to an int and then you can combine two ints using the bitwise OR operation. `byte foo = (digit1 << 4) | (digit2 & 0xF);` – itsme86 May 20 '19 at 15:15
  • What if there are non-digit characters inside a password? – Aleks Andreev May 20 '19 at 15:15
  • Jeroen, I disagree, this is different because the input is ascii and I am not converting a simple ascii character to a byte. – Daisy May 20 '19 at 15:15
  • thank you itsme86. Giving it a shot now. Once I have this working I will post source code for anyone else with a similar question. – Daisy May 20 '19 at 15:17
  • Aleks Andreev, if there are non digit characters the password would be invalid and the method would abort and log a error so that the user knows to fix their password supplied to the driver. – Daisy May 20 '19 at 15:23

1 Answers1

3

Big thanks to Itisme86. =) Based on Itisme86's help above I put together the following code to do the conversion. This is pre error handling and enforcing the format before running the code.

    internal string FormatPassword(string password)
    {
        string formattedPassword = string.Empty;

        for (int pos = 0; pos < password.Length; pos+=2)
        {
            string partialPassword = string.Empty;
            if (password.Length > pos + 1)
            {
                partialPassword = GetPasswordByte(password.Substring(pos, 2));
            }
            else
            {
                partialPassword = GetPasswordByte(password.Substring(pos, 1));
            }

            if (!string.IsNullOrEmpty(partialPassword))
            {
                formattedPassword += partialPassword;
            }
        }

        if (formattedPassword.Length < 8)
        {
            formattedPassword = formattedPassword.PadRight(8, (char)0xFF);
        }

        return formattedPassword;
    }

    internal string GetPasswordByte(string partialPassword)
    {
        string byteString = string.Empty;

        int digit1 = Convert.ToInt16(partialPassword.Substring(0, 1));
        int digit2 = 15;
        if (partialPassword.Length > 1)
        {
            digit2 = Convert.ToInt16(partialPassword.Substring(1, 1));
        }
        byte[] passwordByte = BitConverter.GetBytes((digit1 << 4) | (digit2 & 0xF));
        byteString = System.Text.Encoding.Default.GetString(passwordByte, 0, 1);

        return byteString;
    }
Daisy
  • 121
  • 11