Question ASCII code to ANSI code

anthor

Member
Joined
Jul 12, 2008
Messages
20
Programming Experience
Beginner
how to change the ASCII code to ANSI code?
Example :-

wxcb014041 ->hex 77 78 63 62 30 31 34 30 34 31
 
Ansi usually means text encoding "Windows-1252" / codepage 1252, here's how you get the bytes:
VB.NET:
Dim ansi As System.Text.Encoding = System.Text.Encoding.GetEncoding(1252)
Dim bytes() As Byte = ansi.GetBytes("wxcb014041")
If you need the hex values do ToString("x2") on each byte value (or X2 for uppercase hex). Example using your 2008 platform:
VB.NET:
Dim hex() As String = Array.ConvertAll(bytes, Function(b) b.ToString("x2"))        
MessageBox.Show(String.Join(" ", hex))
 
The Encoding also translates from bytes to string with GetString method:
VB.NET:
Dim s As String = ansi.GetString(bytes)
It would be strange if the source was a hex string, computing systems usually operate on byte level while hex is only used for display purposes, for example when using a binary file/stream viewer. Anyway, reversing the converter can easily be done:
VB.NET:
hex = "77 78 63 62 30 31 34 30 34 31".Split(" "c)
bytes = Array.ConvertAll(hex, Function(h) Byte.Parse(h, Globalization.NumberStyles.HexNumber))
 
Back
Top