# Android Questionconvert string to hex without using library

#### Binary Soft

##### Member
Hello,

In vb6

B4X:
``````Public Function HexToString(ByVal HexToStr As String) As String
Dim strTemp   As String
Dim strReturn As String
Dim I         As Long
For I = 1 To Len(HexToStr) Step 2
strTemp = Chr(Val("&H" & Mid(HexToStr, I, 2)))
strReturn = strReturn & strTemp
Next I
HexToString = strReturn
End Function

Public Function StringToHex(ByVal StrToHex As String) As String
Dim strTemp   As String
Dim strReturn As String
Dim I         As Long
For I = 1 To Len(StrToHex)
strTemp = Hex\$(Asc(Mid\$(StrToHex, I, 1)))
If Len(strTemp) = 1 Then strTemp = "0" & strTemp
strReturn = strReturn & strTemp
Next I
StringToHex = strReturn
End Function``````
eg. string A1 is changed with StringToHex
result is 4131

eg. Hex 4131 is changed with HexToString
result is A1.

Now In b4x
B4X:
``````Sub HexToString(HexToStr As String) As String
Dim strTemp   As String
Dim strReturn As String
For i=0 To HexToStr.Length -1 Step 2
strTemp= Chr( Bit.ParseInt( HexToStr.SubString2( i, i+2), 16))
strReturn = strReturn & strTemp
Next
Return strReturn
End Sub``````
is working.

How can I convert String to Hex without using library?

Thanks.

#### Binary Soft

##### Member
I got it.

B4X:
``````Sub StringToHex(StrToHex As String) As String
Dim strTemp   As String
Dim strReturn As String
Dim I  As Long
For I = 0 To  StrToHex.Length-1
strTemp =Bit.ToHexString(Asc(StrToHex.SubString2(I,I+1)))
If  strTemp.Length = 1 Then strTemp = "0" & strTemp
strReturn = strReturn & strTemp
Next
Return strReturn
End Sub``````

• Xicu

#### emexes

##### Well-Known Member
Or try this: 10x faster, optional anti-crosseye spaces between bytes, doesn't use even that implied library call Bit.ToHexString:

B4X:
``````Sub StringToHexString(S As String, SpaceFlag As Boolean) As String

Dim A As Short
Dim HighNibble As Short
Dim LowNibble As Short
Dim DigitBase As Short

Dim OutChar(S.Length * 3) As Char    'make it big enough for spaces too

Dim PutPtr As Int = 0
For I = 0 To S.Length - 1
A = Asc(S.CharAt(I))

HighNibble = A / 16    'rounds down
LowNibble = A - HighNibble * 16

If HighNibble < 10 Then DigitBase = 48 Else DigitBase = 65 - 10    'to convert nibble to ASCII
OutChar(PutPtr) = Chr(DigitBase + HighNibble)
PutPtr = PutPtr + 1

If LowNibble < 10 Then DigitBase = 48 Else DigitBase = 65 - 10    'to convert nibble to ASCII
OutChar(PutPtr) = Chr(DigitBase + LowNibble)
PutPtr = PutPtr + 1

If SpaceFlag Then
OutChar(PutPtr) = Chr(32)    'space
PutPtr = PutPtr + 1
End If
Next

If SpaceFlag Then
Return CharsToString(OutChar, 0, S.Length * 3 - 1)    'trim trailing space
Else
Return CharsToString(OutChar, 0, S.Length * 2)
End If

End Sub``````

• Binary Soft

#### Binary Soft

##### Member
Or try this: 10x faster, optional anti-crosseye spaces between bytes, doesn't use even that implied library call Bit.ToHexString:

B4X:
``````Sub StringToHexString(S As String, SpaceFlag As Boolean) As String

Dim A As Short
Dim HighNibble As Short
Dim LowNibble As Short
Dim DigitBase As Short

Dim OutChar(S.Length * 3) As Char    'make it big enough for spaces too

Dim PutPtr As Int = 0
For I = 0 To S.Length - 1
A = Asc(S.CharAt(I))

HighNibble = A / 16    'rounds down
LowNibble = A - HighNibble * 16

If HighNibble < 10 Then DigitBase = 48 Else DigitBase = 65 - 10    'to convert nibble to ASCII
OutChar(PutPtr) = Chr(DigitBase + HighNibble)
PutPtr = PutPtr + 1

If LowNibble < 10 Then DigitBase = 48 Else DigitBase = 65 - 10    'to convert nibble to ASCII
OutChar(PutPtr) = Chr(DigitBase + LowNibble)
PutPtr = PutPtr + 1

If SpaceFlag Then
OutChar(PutPtr) = Chr(32)    'space
PutPtr = PutPtr + 1
End If
Next

If SpaceFlag Then
Return CharsToString(OutChar, 0, S.Length * 3 - 1)    'trim trailing space
Else
Return CharsToString(OutChar, 0, S.Length * 2)
End If

End Sub``````
emexes
It's great and doesn't use even that implied library
Thanks

#### Erel

Staff member
Why not use the ByteConverter library? Are you worried about the extra size that will be added to your app? It will be about 3kb.

#### emexes

##### Well-Known Member
In my case, my eyes are getting old, and I like a little space in between bytes. Inserting those into the ByteConverter result chews up clock cycles for which I have better uses.

So I posted the routine I use, except that I replaced my own Bit calls with a divide and multiply, so as to meet the "without using library" stipulation.

But you're right, I should have asked the same question. :-/

#### Binary Soft

##### Member
Why not use the ByteConverter library? Are you worried about the extra size that will be added to your app? It will be about 3kb.
MySQL Database server has many clients that created with different other programming languages (Qt C++, Web, VB6 etc. for other developers.)

I used B4XEncryption Library and save password as BLOB to mysql database.

eg.
B4X:
``````Dim c As B4XCipher
Dim bytes() As Byte = Null
Return bytes``````
I do not know how to decrypt it from other language eg. VB6 code. So I want to choose simple code without library and
my own encryption method is suitable for me to share similar code to others.

Thanks.

#### Erel

Staff member
1. I don't see any relation between your last post and the main question in this thread.
2. I don't see any reason not to use the well proven ByteConverter library.

B4X:
``````Sub HexToString(HexToStr As String) As String
Dim bc As ByteConverter
Dim b() As Byte = bc.HexToBytes(HexToStr)
Return BytesToString(b, 0, b.Length, "UTF8")
End Sub

Sub StringToHex(StrToHex As String) As String
Dim bc As ByteConverter
Return bc.HexFromBytes(s.GetBytes("UTF8"))
End Sub``````

• superkan, emexes and Binary Soft

#### emexes

##### Well-Known Member
A few minutes after posting that bit about saving clock cycles, I had a terrible(ish) thought: whatever I need in B4AIJ, nine times out of ten I find Erel's been there, done that. So I quickly checked to make sure there wasn't already a spaced-out-hex function eg HexFromBytes2, that would make me look like an idiot, and happily(ish) there is not (that I could see).

#### Erel

Staff member
But you're right, I should have asked the same question
The real question, is why use hex at all? The better way to convert raw bytes to string is with Base64 encoding (though if the data is small enough then hex is also fine).

• Binary Soft

#### emexes

##### Well-Known Member
When I was sorting through those BLE packets, I had enough problems in hexadecimal, I'm not sure that quadrosexagesimal would have made things better ;-)

But for transferring data eg squeezing data into size- and ASCII-constrained packets   Hmm. Would be fun trying to do that just with plus-minus-times-divide, without library calls. Not today, though.

#### Erel

Staff member
without library calls
There is no such thing. There is no performance penalty for calling library methods.

#### emexes

##### Well-Known Member
There is no such thing. There is no performance penalty for calling library methods.
Agreed.* You had eventually teased out of Binary Soft that his reason for avoiding library calls, was so that the source code could stand alone as a sample for implementing the same functionality in other languages. I figured that any new solutions, even those done just for fun, should also abide by that constraint.

In retrospect, I should have posted that line as:
<HUMOUR TYPE=IDLE>Hmm. Would be fun trying to do that just with plus-minus-times-divide, without library calls. Not today, though.</HUMOUR>

(* ignoring the miniscule overhead of the library call/return and parameter/result passing vs having the same code inline)

#### Erel

Staff member
Agreed, with no footnote 