How Convert Decimal , Octal to Ascii

# How Convert Decimal , Octal to Ascii

• Monday, June 15, 2009 10:34 AM

How Convert Decimal , Octal to Ascii ?

### All Replies

• Monday, June 15, 2009 10:59 AM

You can use AsciiEncoding for achieving this.

ASCIIEncoding.Convert
 Thanks, A.m.a.L .Net Goodies Remember to click "mark as answered" when you get a correct reply to your question
• Monday, June 15, 2009 11:02 AM

You can use AsciiEncoding for achieving this.

ASCIIEncoding.Convert
 Thanks, A.m.a.L .Net Goodies Remember to click "mark as answered" when you get a correct reply to your question

I don't understand
• Monday, June 15, 2009 11:09 AM

Use the oct function

Be aware that Octal has no special notation

http://msdn.microsoft.com/en-us/library/3yyk62cc.aspx

Success
Cor
• Monday, June 15, 2009 12:45 PM

If the decimal or octal number is in a string, you can use the same Convert.ToInt32 method that was suggested in yout thread about converting hex strings to ASCII.  Just change the base (second parameter) from 16 to 10 or 8 as appropriate.  Here is a link to the earlier thread http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e208485a-a191-4ef3-9772-a6b446e01d10
• Monday, June 15, 2009 1:33 PM

How Convert Decimal , Octal to Ascii ?

Hi MEHD13,

All values in the ASCII table are in the range 0 to 255 in DECIMAL
(or Base10) notation as 255 if the maximum value a BYTE can hold.

See.>> http://asciitable.com/

If you take a close look at that table under DEC is the column for the DECimal value.
under Hx    is the column for the HEXadecimal value.
under OCT is the column for the OCTal value.

The trouble is if you were to write OCTAL values down ( between 0 and 7 ) in a continuous manner like.>>

375134124201357246

You would have to group the numbers in blocks of 3 so it becomes.>>

375 134 124 201 357 246

If any of the numbers were to exceed 377 in OCTAL which is 255 in Base 10 or FF in Hexadecimal
it then exceeds the 255 maximum value that a BYTE can hold, which is what the ASCII table
was created around as older computers were only 8 bit.

Regards,

John
• Monday, June 15, 2009 5:06 PM

Hi MEHD13,

I have a long program in this thread.>>

which will do conversions from one format to another.

I had to go as far as page 30 of my threads to find the above thread.

I would have to add code for conversion(s) to and from OCTAL.

Regards,

John

• Monday, June 15, 2009 5:52 PM

For clarification the only printable ASCII characters are the ones in the decimal-byte-value range of 32 to 126 (and 32 only counts if you call SPACE printable).  Also in VB.NET, ALL Byte values are presented in decimal.

That said, this is really a lot easier than it sounds.

Take the following example:

'Use a simple System.String as our test foundation.
Dim strTest As System.String = "This is a test string."
'And an array of System.Char constructed from the String to represent
'each individual ASCII printable character in the String.
Dim strCharBreakdown As System.Char() = strTest.ToCharArray
'And last, an array of System.Byte we can use to demo Decimal and Octal
'representations and transformations.
'We'll set the upper boundary of this Byte Array to match the upper boundary of the CharArray.
Dim strByteBreakdown(UBound(strCharBreakdown)) As System.Byte
'In order to populate the ByteBreakdown, we iterate through the CharArray index-by-index...
'and at the same time, demonstrate several representations of each character in a separate String.

'First the Display String initialized with a simple header...
Dim strDisplay As System.String = "CHR  -  DEC  -  OCT" & vbNewLine

'Then a For Each by Index...
For strchar_index As Integer = 0 To UBound(strCharBreakdown)
strByteBreakdown(strchar_index) = CType(Asc(strCharBreakdown(strchar_index)), System.Byte)
'Add a line to the Display String...
strDisplay &= "   " & strCharBreakdown(strchar_index) & "   -    " & strByteBreakdown(strchar_index) & "  -  " & Oct(strByteBreakdown(strchar_index)) & vbNewLine
Next
'Finally display the string.
MsgBox(strDisplay)

VB displays all numeric values in Decimal format unless you tell it to do otherwise.  Using Oct() or Hex() does not change the value or even binary format of a Byte or Integer value at all - it just displays it in Octal notation for your benefit.  You would only ever need to pass these values (other than to end-user display) if you were for some reason transmitting data to a processor of (now) a very outdated architecture, and then you would not pass the Oct() value but a byte array constructed to match the Oct() value.

Then using the code above as the first part of a demo Sub or Function, since we've constructed a Byte Array that will return Decimal values:

'Reset strDisplay to blank for recycling in the next process...
strDisplay = "DEC  -  CHR" & vbNewLine

'Iterate through the Byte Array to convert it all back to a String...
For Each strByte As System.Byte In strByteBreakdown
strDisplay &= strByte & "  -  " & Chr(strByte) & vbNewLine
Next
'And display anew...
MsgBox(strDisplay)

Just remember that when you convert any Byte Value to an OCT() representation, you're actually returning a human-readable STRING and not an actual Octal bit pattern.  So as one final example:

Dim intASCII_Letter_L As System.Byte = 76
strDisplay = "DECIMAL Number: " & intASCII_Letter_L & vbNewLine & _
"DECIMAL ASCII:  " & Chr(intASCII_Letter_L) & vbNewLine & vbNewLine & _
"OCTAL Number: " & Oct(intASCII_Letter_L) & vbNewLine & _
"OCTAL ASCII:  " & Chr(Oct(intASCII_Letter_L))
MsgBox(strDisplay)

If you run Visual Studio with Option Strict ON,  "OCTAL ASCII:  " & Chr(Oct(intASCII_Letter_L)) will alert you that implicit conversion from String to Integer cannot occur.  If you went and deliberately converted this human-readable-only Octal value to ASCII, it would return some other letter than uppercase L.

Hope this helps.
• Monday, June 15, 2009 8:03 PM

Just remember that when you convert any Byte Value to an OCT() representation, you're actually returning a human-readable STRING and not an actual Octal bit pattern .  So as one final example:

Dim intASCII_Letter_L As System.Byte = 76
strDisplay = "DECIMAL Number: " & intASCII_Letter_L & vbNewLine & _
"DECIMAL ASCII:  " & Chr(intASCII_Letter_L) & vbNewLine & vbNewLine & _
"OCTAL Number: " & Oct(intASCII_Letter_L) & vbNewLine & _
"OCTAL ASCII:  " & Chr(Oct(intASCII_Letter_L))
MsgBox(strDisplay)

If you run Visual Studio with Option Strict ON,  "OCTAL ASCII:  " & Chr(Oct(intASCII_Letter_L)) will alert you that implicit conversion from String to Integer cannot occur.  If you went and deliberately converted this human-readable-only Octal value to ASCII, it would return some other letter than uppercase L .

Hope this helps.

Hi Andrew,

I don't know what you mean by an OCTAL bit pattern. I do know BINARY, OCTAL and HEXADECIMAL though. :-)

Hexadecimal is not now (and never has been) anything but a mathematical notation method designed to make 8-bit bytes more human-readable.

Octal has however been at employed at the actual processor level in some legacy mainframe systems (most notably IBM) and their peripherals, where 24 or 36 bits (3 or 4.5 bytes) were used instead of the basic 8 bit (one byte), 16 bit (two bytes), 32 bit (four bytes), and 64 bit (8 bytes).  What this means is that, essentially, some older processors and peripheral devices implementing Octal bit patterns may consider a byte to be more or less than 8 bits.

So if he's looking to pass Octal bit patterns to one of these platforms?  He's constructing System.Collections.BitArray objects by hand because the string representation of an Octal value (and especially the 32-bit Integer representation of the same value!) WILL NOT COMPUTE in a real Octal environment.

It never hurts to try. In a worst case scenario, you get to learn what you did wrong and how to fix it and that is at least as valuable as getting it right the first time.
• Monday, June 15, 2009 8:28 PM

John:

I'll pass your opinion of the 36-bit binary word on to IBM - I'm sure they and everyone who ever worked with their machines will be interested to hear all about their programming errors in the 1960s.

In reality:

Since the original author specifically referred to Octal, one MUST assume that the original author is working with an actual file-format-image or processor specification that calls for Octal values.  If that is so, then the original author MUST acknowledge the reality that 36 bits (or 4.5 8-bit bytes) can represent a real number or letter and also MUST program accordingly - erroneous as modern standards may decree the practice to be.

It never hurts to try. In a worst case scenario, you get to learn what you did wrong and how to fix it and that is at least as valuable as getting it right the first time.