Convert hex to ASCII, like in Excel - vb.net

I am looking for a function in VB.NET which will convert a hex value to the corresponding ASCII, like in Excel.
For example, in Excel,
=CHAR(HEX2DEC("c7")) will return, 'Ç'
Is there any library function, which does the same, in .NET?

Dim hexValue = "FF"
Dim ascii = System.Convert.ToChar(System.Convert.ToUInt32(hexValue, 16))

You can convert a byte array to an ASCII string using System.Text.Encoding.ASCII.GetString. The byte array can be defined using hex literals.

You can use the ChrW method. You have to import the Microsoft.VisualBasic namespace:
ChrW(Convert.ToInt32("C7", 16))

Related

Convert unicode byte arrary to a binary byte arrary

I have a byte() array full of Unicode data. I need to convert this byte array to binary data.
The original data was binary data, but data was saved as Unicode, and thus these data structures are now all 2 times as large as they need to be.
Can I convert from a byte array of one type to another byte array, or is looping required to skip every other byte?
Edit:
Comments asked for more info
The original byte array is Unicode UTF32 looks to be the format.
The output byte array needs to remove that extra encoding.
So, assuming this, then EndianUnicode as bytes to toss out the extra data works quite well
This seems to work:
b2 = System.Text.Encoding.BigEndianUnicode.GetBytes
(System.Text.Encoding.UTF32.GetString(b))
Of course it not clear as to why the resulting array is not EXACTLY 1/2 in size but the above does seem to work.
Edit2:
Ok, as noted, the question was not ONLY how to convert, but was btye arrary to btye array. Further more, the array was indeed Unicode, but the original binary byte array was based on the users local code page (English).
So the CORRECT conversion I required was this:
b2 = System.Text.Encoding.Default.GetBytes
(System.Text.Encoding.Unicode.GetString(b))
However, the above converts from a byte arrary to a string, and then back to a byte array. My question was STILL how to do this from byte arrary to byte array. Turns out you can do this, and this is how:
Dim b() As Byte
b = reader(0) ' the array is filled with Unicode (air code)
Dim b2() As Byte
' convert byte array - not have to convert to strings
Dim cFrom As System.Text.Encoding = System.Text.Encoding.Unicode
Dim cto As System.Text.Encoding = System.Text.Encoding.Default
b2 = System.Text.Encoding.Convert(cFrom, cto, b)
As noted, above is byte() array to byte() array as per my original question.
Note that "default" in above is of course the default code page (in my case a computer running English version of windows).

Conversion of Multiple ascii code to char

I have a function that convert string to ascii code (e.g string = "system" the value of string in ascii = "115 121 115 116 101 109") and what i need is a way to convert the `ascii into char. Do i need to use loop to filter the converted ascii ? I need your suggestion what is the best way to convert it
The best way is one that checks your assumptions. You say the string contains ASCII bytes. So if someone slips in a byte that cannot be ASCII, you should be told. It does appear that someone slipped in an unexpected space, but that can be ignored.
Imports System.Linq
'…
Dim asciiEncoding = Encoding.GetEncoding("US-ASCII",
EncoderFallback.ExceptionFallback,
DecoderFallback.ExceptionFallback)
Dim ascii = "115 121 115 116 101 109"
Dim asciiBytes = ascii.Split( { " "c }, StringSplitOptions.RemoveEmptyEntries) _
.Select(Function (s) Byte.Parse(s)) _
.ToArray()
Dim s = asciiEncoding.GetString(asciiBytes)
Other ways might not catch invalid data.
Some ways automagically convert from the ASCII character set to the Unicode character set, which is valid when the data is, in fact, ASCII but at least deserves a comment about the conversion and that the data is trusted to be ASCII.
Speaking of whether the data is ASCII or not, there is no text but encoded text. When you read text, you have to use the encoding it was written with. The only way to know is for the writer to make it known.
My suggestion is:
First split the string using string.split(' ');
And then convert each split string to char like this:
foreach(string word in SplitedWords)
{
Convert.ToChar(int.Parse(word));
}
This is built-in. You can treat a String as an array of Char.
Dim s As String = "system"
Console.WriteLine(s(4) & " = ASCII " & Asc(s(4))) 'output: e = ASCII 101
Console.ReadKey()
You can use the Asc() function to get the ASCII value of the character, but be aware that Strings and Chars are actually Unicode, not ASCII. Depending on encoding, you might find that one character is more than one byte.
FOR THE NITPICKERS: When you treat a String as an array of Char, the compiler boxes the String, so it is less efficient that having a true Char array.

VB.NET - Convert String Representation of a Byte Array back into a Byte Array

I have an application that makes a call to a third party web API that returns a String that looks something like this:
"JVBERi0xLjMNCiXi48/TDQoxIDAgb2JqDQo8PA0KL1R5cGUgL091dGxpbmVzDQovQ291bnQgMA0KPj4NCmVuZG9iag0KMiAwIG9iag0KDQpbL1BERiAvVGV4dCAvSW1hZ2VDXQ0KZW"
(It's actually much longer than that but I'm hoping just the small snippet is enough to recognize it without me pasting a string a mile long)
The documentation says it returns a Byte array but when I try to accept it as a Byte array directly, I get errors. Part of my problem here is that the documentation isn't completely clear what the Byte array represents. Since it's a GetReport function I'm calling, I'm guessing it's a PDF but I'm not 100% sure as the documentation doesn't say at all.
So, anyway, I'm getting this String and I'm trying to convert it to a PDF. Here's what that looks like:
Dim reportString As String = GetValuationReport(12345, token.SecurityToken)
Dim report As Byte() = System.Text.Encoding.Unicode.GetBytes(reportString)
File.WriteAllBytes("C:\filepath\myreport.pdf", report)
I'm pretty sure that the middle line converts the String into a new Byte array rather than simply converting it into its Byte array equivalent but I don't know how to do that.
Any help would be fantastic. Thanks!
It looks like your string may be Base64 encoded, in which case you would use this to convert it to bytes:
Dim report As Byte() = Convert.FromBase64String(reportString)

Hexadecimal to 8-bit unsigned array in VB.NET

I have a hexadecimal value,
07A5953EE7592CE8871EE287F9C0A5FBC2BB43695589D95E76A4A9D37019C8
Which I want to convert to a byte array.
Is there a built-in function in .NET 3.5 that will get the job done or will I need to write a function to loop through each pair in the string and convert it to its 8-bit integer equivalent?
There is no built-in function that will do this. You will unfortunately have to code one up :(
Public Function ToHexList(ByVal str As String) As List(Of Byte)
Dim list As New List(Of Byte)
For i = 0 to str.Length-1 Step 2
list.Add(Byte.Parse(str.SubString(i,2), Globalization.NumberStyles.HexNumber))
Next
Return list
End Function
EDIT
Qualified the NumberStyles enumeration with the Globalization namespace qualifier. Another option is to import that namespace and remove the qualifier.
I think that you'll find what you are looking for here (codeproject.com)

Converting Greek characters to Unicode

Is there any easy way of converting a windows-1252 string into a Unicode one?
All strings in .NET are Unicode in memory.
If you have a byte array that was generated from a string encoded in 1252, you can recover the string using
Dim S as String = System.Text.Encoding.GetEncoding(1252).GetString(array)
It is now a unicode string in memory. If you then want to encode that string into a UTF-8 byte array for transmission or storage, you would do the converse:
Dim A as byte() = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(S)
(I think that is the right VB syntax!)