I have come across some Base64 conversion functions in .net(FromBase64.string etc). What i want is, for a sample, i have a base 64 encoded string as
"48YwojCi4yaiow==".
I need to convert this string to the corresponding Hexadecimal text(The sample stands for “Thisistest” in hex text)
The below link is an online converter from base64 to hexadecimal text. If you give the same base64 encoded data in the link , click on convert, the one that is seen below “Hexadecimal text” is what I need.
http://www.hcidata.info/base64.htm
Is there a standard library function in vb.net which does this? Converting a base 64 data to a hexadecimal text?
You’ll need to use the
Convert.FromBase64Stringmethod to first convert the base64-encoded string into an array of bytes.You can then take that array of bytes and individually convert them to their equivalent hexadecimal representation using the overload of
Byte.ToStringthat accepts a format specifier. The format specifiers available are documented here, but the you want is eitherXorxfor hexadecimal, depending on whether you want the letters to be capitalized or not.You could encapsulate all of this into an extension method for the
Stringclass if you wanted to hide it all away for ease of use purposes. For example:Alternatively, you could investigate using the
BitConverter.ToStringmethod instead of the loop to convert each of the values in the byte array to their equivalent hexadecimal string representations. This will produce a hyphen-delineated string of hexadecimal pairs.I have no idea which method is “better” or more performant. You’ll have to profile the code, or chose whichever you find more readable.
If you don’t want the dashes in the string that the
BitConverter.ToStringmethod returns, you’ll need to remove them: