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 32303037303730353134343733 36.
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
I'm trying to find a way to convert a OCTET NumberString to a ascii string.
I have a OCTET NumberString value called 32303037303730353134343733
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Nice code, my friend, but you need 2 strings (Substring(idx, 2)) *BIG GRIN*
Bob
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*
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(s tring 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();
}
public static string NumberOctetStringToAscii(s
{
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(s tring 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();
}
public static string NumberOctetStringToAscii(s
{
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();
}
ASKER
Hey experts,
Thanks for solutions :-)
What if I want to convert a OCTET String to Ascii ??
Thanks,
Jan
Thanks for solutions :-)
What if I want to convert a OCTET String to Ascii ??
Thanks,
Jan
Try this.
String ByteString = "3230303730373035313434373
StringBuilder AscOut = new StringBuilder();
for (int idx = 1; idx < ByteString.Length; idx += 2)
{
AscOut.Append(ByteString.S
}
MessageBox.Show(AscOut.ToS
Fernando