473,383 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Converting a base64 string to a Hex string

Are there any library functions that can help me to do this? If
necessary I can convert the string to a byte array. I don't want to
have to write my own Hex conversion if it isn't necessary.

Thanks for any help
Jeremy Kitchen

Mar 13 '07 #1
9 9678
On Mar 13, 9:54 am, "Jeremy Kitchen" <J.t.Kitc...@gmail.comwrote:
Are there any library functions that can help me to do this? If
necessary I can convert the string to a byte array. I don't want to
have to write my own Hex conversion if it isn't necessary.

Thanks for any help
Jeremy Kitchen
Forgive me if I'm way off, but do you want to take a string such as
"1234ABCDEFG" and end up with "3132333441424344"?

You can use the ASCII object in System.Text.AsciiEncoding to get the
bytes of the string, then convert each byte to a Hex string with the
ToString overload that accepts a format.

Dim sBase As String = "1234ABCD"
Dim bBase() As Byte
Dim sHex As String = String.Empty
Dim sHexByte As String

bBase = System.Text.ASCIIEncoding.ASCII.GetBytes(sBase)

For i As Integer = 0 To bBase.Length - 1
sHexByte = bBase(i).ToString("x")
If sHexByte.Length = 0 Then
sHexByte = "0" & sHexByte
End If
sHex &= sHexByte
Next

Console.WriteLine(sHex)
Console.ReadLine()

Mar 13 '07 #2
jayeldee wrote:
>
Dim sBase As String = "1234ABCD"
Dim bBase() As Byte
Dim sHex As String = String.Empty
Dim sHexByte As String

bBase = System.Text.ASCIIEncoding.ASCII.GetBytes(sBase)

For i As Integer = 0 To bBase.Length - 1
sHexByte = bBase(i).ToString("x")
If sHexByte.Length = 0 Then
sHexByte = "0" & sHexByte
End If
sHex &= sHexByte
Next

Console.WriteLine(sHex)
Console.ReadLine()
Just a few suggestions, hope you don't mind...

You don't need to keep track of the byte index (the For i As
Integer... loop). Instead you may want to use a For Each loop -- just
a question of preference, though, but the loop may become more
"readable":

For Each B As Byte In Sytem.Text.Encoding. _
ASCII.GetBytes(Base64Text)
...
Next

Also, you'll be glad to know that the ToString("x") method of the
integer data types (Byte, Integer, Long, UInteger, etc) allows a
"length" parameter, so you don't have to manually padd the returned
string:

For Each B As Byte In Sytem.Text.Encoding. _
ASCII.GetBytes(Base64Text)
'HexChar will be zero-padded up to two digits
Dim HexChar As String = B.ToString("x2")
...
Next

Finally, just remember that base64 encoded strings are *usually* used
to encode a large amount of data. Concatenating strings in a loop
using the "&" operator is not a real problem if the number of
iterations is small -- but, how many is "small"? Don't ask me.
Personally, I'll resort to a StringBuilder whenever I see a loop. Of
course, there's no rule in stone, here. But since we may consider that
the typical content of a base64 encoded string can be quite large, the
use of a StringBuilder may be advised:

Dim S As New System.Text.StringBuilder
For Each B As Byte In Sytem.Text.Encoding. _
ASCII.GetBytes(Base64Text)
S.Append(B.ToString("x2"))
Next
HexText = S.ToString
Just for the fun of it (and if the OP doesn't mind writing VB code
that threads on the limits of readability), we could also have:

Public Function CharToHex(Value As Char) As String
Return Convert.ToByte(Value).ToString("x2")
End Function

'...
HexText = String.Join(Nothing, _
System.Array.ConvertAll(Of Char, String)( _
Base64Text.ToCharArray, AddressOf CharToHex))
:-))

Regards,

Branco.

Mar 13 '07 #3
On Mar 13, 3:42 pm, "Branco Medeiros" <branco.medei...@gmail.com>
wrote:
jayeldee wrote:
Just a few suggestions, hope you don't mind...
Awesome suggestions. I just made it quick, dirty, and readable. I
forgot about the padding of the string and probably should've used a
StringBuilder (I almost always do in any examples I toss together.)
Mar 13 '07 #4
Jeremy Kitchen wrote:
Are there any library functions that can help me to do this? If
necessary I can convert the string to a byte array. I don't want to
have to write my own Hex conversion if it isn't necessary.
Is this what you want?

\\\
Dim ret As String
'b64 is a string containing base-64 encoded data
Dim b64 As String = "VXNlcm5hbWU6"
'Decode the base64 data to a byte array
Dim b64bytes() As Byte = System.Convert.FromBase64String(b64)

'Loop through each byte, adding the hex value for each byte to our
return string
For Each b As Byte In b64bytes
ret &= Hex(b).PadLeft(2, "0"c)
Next

'Display the finished string
MsgBox(ret)
///

--

(O)enone

Mar 14 '07 #5
On Mar 14, 5:39 am, "Oenone" <oen...@nowhere.comwrote:
Jeremy Kitchen wrote:
Are there any library functions that can help me to do this? If
necessary I can convert the string to a byte array. I don't want to
have to write my own Hex conversion if it isn't necessary.

Is this what you want?

\\\
Dim ret As String
'b64 is a string containing base-64 encoded data
Dim b64 As String = "VXNlcm5hbWU6"
'Decode the base64 data to a byte array
Dim b64bytes() As Byte = System.Convert.FromBase64String(b64)

'Loop through each byte, adding the hex value for each byte to our
return string
For Each b As Byte In b64bytes
ret &= Hex(b).PadLeft(2, "0"c)
Next

'Display the finished string
MsgBox(ret)
///

--

(O)enone

Thanks everyone.

Mar 14 '07 #6
On Mar 14, 8:38 am, "Jeremy Kitchen" <J.t.Kitc...@gmail.comwrote:
On Mar 14, 5:39 am, "Oenone" <oen...@nowhere.comwrote:
Jeremy Kitchen wrote:
Are there any library functions that can help me to do this? If
necessary I can convert the string to a byte array. I don't want to
have to write my own Hex conversion if it isn't necessary.
Is this what you want?
\\\
Dim ret As String
'b64 is a string containing base-64 encoded data
Dim b64 As String = "VXNlcm5hbWU6"
'Decode the base64 data to a byte array
Dim b64bytes() As Byte = System.Convert.FromBase64String(b64)
'Loop through each byte, adding the hex value for each byte to our
return string
For Each b As Byte In b64bytes
ret &= Hex(b).PadLeft(2, "0"c)
Next
'Display the finished string
MsgBox(ret)
///
--
(O)enone

Thanks everyone.

Ack, I am having trouble getting it to convert back to base64

Imports System.Text
Module Module1

Sub Main()
Dim baseStr As String = "4WUC/38ff+GAm2GGOeR5Up7fi5Q0RU
+h8Zf5vKsOtXE="
Console.WriteLine(baseStr)
Console.WriteLine(Base64ToHex(baseStr))
Console.WriteLine()

Console.WriteLine(baseStr)
Console.WriteLine(HexToBase64(Base64ToHex(baseStr) ))
Console.ReadLine()
End Sub

Function Base64ToHex(ByVal sBase As String) As String
Dim bBase() As Byte = Convert.FromBase64String(sBase)
Dim Hex As New StringBuilder

For Each HexByte As Byte In bBase
Console.Write(HexByte & " ")
Hex.Append(HexByte.ToString("x2"))
Next
Console.WriteLine()
Return Hex.ToString.ToUpper
End Function
Private Function HexToBase64(ByVal HexText As String) As String
Dim result(HexText.Length) As Byte

Dim i As Integer = 0
While i < HexText.Length
result(i) = Convert.ToByte(HexText.Substring(i, 2), 16)
Console.Write(result(i) & " ")
i += 2
End While
Console.WriteLine()

Return Convert.ToBase64String(result)
End Function
End Module

It is returning a much different string than I started with

Mar 14 '07 #7
Jeremy Kitchen wrote:
Ack, I am having trouble getting it to convert back to base64
It's because you're not accounting for each byte taking two characters
within the string.

Try replacing your HexToBase64 function with the following:

\\\
Private Function HexToBase64(ByVal HexText As String) As String
Dim result(HexText.Length \ 2) As Byte

Dim i As Integer = 0
While i < HexText.Length \ 2
result(i) = Convert.ToByte(HexText.Substring(i * 2, 2), 16)
Console.Write(result(i) & " ")
i += 1
End While
Console.WriteLine()

Return Convert.ToBase64String(result)
End Function
///

HTH,
--

(O)enone
Mar 14 '07 #8
On Mar 14, 2:01 pm, "\(O\)enone" <oen...@nowhere.comwrote:
Jeremy Kitchen wrote:
Ack, I am having trouble getting it to convert back to base64

It's because you're not accounting for each byte taking two characters
within the string.

Try replacing your HexToBase64 function with the following:

\\\
Private Function HexToBase64(ByVal HexText As String) As String
Dim result(HexText.Length \ 2) As Byte

Dim i As Integer = 0
While i < HexText.Length \ 2
result(i) = Convert.ToByte(HexText.Substring(i * 2, 2), 16)
Console.Write(result(i) & " ")
i += 1
End While
Console.WriteLine()

Return Convert.ToBase64String(result)
End Function
///

HTH,
--

(O)enone
I eventually realized said fact...and fixed it. Thanks for the help
though

Mar 14 '07 #9
Jeremy Kitchen wrote:
<snip>
Ack, I am having trouble getting it to convert back to base64
<snip>
Private Function HexToBase64(ByVal HexText As String) As String
Dim result(HexText.Length) As Byte

Dim i As Integer = 0
While i < HexText.Length
result(i) = Convert.ToByte(HexText.Substring(i, 2), 16)
Console.Write(result(i) & " ")
i += 2
End While
Console.WriteLine()

Return Convert.ToBase64String(result)
End Function
<snip>

The size of Result must be half of the hex-string size; also, its
index is independent of the index that traverses the hex-string.

<code>
Private Function HexToBase64(ByVal HexText As String) As String
Dim Size As Integer = HexText.Length
Dim Result(0 To (Size >1) - 1) As Byte
Dim Pos As Integer
For Index As Integer = 0 To Size - 1 Step 2
Result(Pos) = Convert.ToByte(HexText.Substring(Index, 2), 16)
Pos += 1
Next
Return Convert.ToBase64String(Result)
End Function
</code>

HTH.

Regards,

Branco.

Mar 14 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John | last post by:
Hi all, I've been going through google and yahoo looking for a certain base64 decoder in C without success. What I'm after is something that you can pass a base64 encoded string into and get back...
1
by: scott | last post by:
Hi all, trying to use base64. Ill get right to the problem. I am converting a string into base 64. No problem there. That base64 string can then be converted back to the orignal string. No...
7
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the...
0
by: Phil C. | last post by:
(Cross post from framework.aspnet.security) Hi. I testing some asp.net code that generates a 256 bit Aes Symmetric Key and a 256 bit entropy value. I encrypt the Aes key(without storing it as...
3
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
8
by: Jeremy Kitchen | last post by:
I have encoded a string into Base64 for the purpose of encryption. I then later decrypted it and converted it back from Base64 the final string returns with four nothing characters. "pass" what...
13
by: aruna.eies.eng | last post by:
i am currently trying to convert data into binary data.for that i need to know how to achieve it in c language and what are the libraries that we can use. so if any one can send me a sample code or...
2
by: =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post by:
Hi, I am using Visual C++ in Visual Studio 2005 to create a Managed Wrapper around some C++ LIBS. I've created some classes that contains a pointer to the LIB classes and everthing seems to...
2
by: joe shoemaker | last post by:
I would like to convert url into md5 hash. My question is that md5 hash will create collision at 2^64. If you do long(value,16), where value is the md5 hash string, would value returned from...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.