Link to home
Start Free TrialLog in
Avatar of bibobas
bibobas

asked on

OCTET NumberString to ASCII

Hey Experts,
I'm trying to find a way to convert a OCTET NumberString to a ascii string.
I have a OCTET NumberString value called 3230303730373035313434373336.
And the result should be ASCII value = 20070705144736

As you can se it's a date 2007xxxxx

The OCTET value commes from a ASN decoded file. I'm using C#

Thanks,
Jan
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi bibobas;

Try this.

            String ByteString = "3230303730373035313434373336";
            StringBuilder AscOut = new StringBuilder();

            for (int idx = 1; idx < ByteString.Length; idx += 2)
            {
                AscOut.Append(ByteString.Substring(idx, 1));
            }
            MessageBox.Show(AscOut.ToString());

Fernando
Nice code, my friend, but you need 2 strings (Substring(idx, 2)) *BIG GRIN*

Bob
No no, I am indexing the string by 2 and just reading the second character which is the character needed for the result string. *BIG GRIN*
Avatar of drichards
drichards

If you are doing that operation a lot, I would make a small mod to avoid the creation of all the tiny substrings.  Rather than get a substring, just use the characters directly:

    public static string NumberOctetStringToAscii(string input)
    {
        char baseDigit = (char)0x30;
        StringBuilder dateStr = new StringBuilder();
        for (int ii = 0; ii < input.Length; ii += 2)
        {
            char c = (char)(((input[ii] - baseDigit) * 16) + (input[ii + 1] - baseDigit));
            dateStr.Append(c);
        }
        return dateStr.ToString();
    }
Or optimized per FernandoSoto's comment:

    public static string NumberOctetStringToAscii(string input)
    {
        char baseDigit = (char)0x30;
        StringBuilder dateStr = new StringBuilder();
        for (int ii = 0; ii < input.Length; ii += 2)
        {
            dateStr.Append(input[ii+1]);
        }
        return dateStr.ToString();
    }


   

Avatar of bibobas

ASKER

Hey experts,
Thanks for solutions :-)
What if I want to convert a OCTET String to Ascii ??
Thanks,
Jan