New Framework for EncodeHex

Hi all,

I have been researching trying to convert a Xojo.Core.MemoryBlock to Hex so I can store a hash in my SQLite db. The old framework was was EncodeHex(MB) and I am having an issue finding the best way how to accomplish this in the new Framework. Any ideas would be appreciated!

Thank you in advance,

I don’t see it either. Is this for iOS? If not, you can always use the old framework call or you can roll your own method. The key is that Integer has a ToHex( minDigits ) function.

No its for Web and the current Realbasic.EncodeHex doesn’t allow me to pass a MB unfortunately. Am I missing something on the old framework EncodeHex? (i am on 2015R3). I’m basically trying to get my Hash encoded properly so I can insert it into my sqlite db.

Ill look at some other functions also.

Thanks Kem!

Ok so all I had to do what ensure I was using ASCII encoding vs. UTF8 when inserting into the database. It now submits properly now.

    Dim inboundNewHashMB as MemoryBlock = CreateHash(usersTypedInPassword, usersDBSalt)
    Dim thisText as Text =Xojo.Core.TextEncoding.ASCII.ConvertDataToText(inboundNewHashMB, True)

careful about bytes > 127 :stuck_out_tongue:
unless of course you can be sure your hashing function will never do such a thing

[quote=227395:@Norman Palardy]careful about bytes > 127 :stuck_out_tongue:
unless of course you can be sure your hashing function will never do such a thing[/quote]
Good point and thank you Norman. I was planning on 64 bytes using Crypto.PBKDF2

Oh I mean ASCII and code points > 127 since ASCII is only code points 0-127 :slight_smile:

Thanks Norman! :slight_smile:

Try this.

dim mb as MemoryBlock = nmb.Data
dim s as string = mb. StringValue( 0, nmb.Size )
encoded = EncodeHex( s )

Thanks Kem! Ill try that! Thanks!

Or never touching the classic framework :stuck_out_tongue:

dim mb as new xojo.Core.MutableMemoryBlock( 128 )

for i as integer = 0 to 127
mb.Int8Value(i) = i+30

dim hexbytes as new xojo.core.memoryblock( Array( Ctype(48, byte), Ctype(49, byte), Ctype(50, byte), _
Ctype(51, byte), Ctype(52, byte), Ctype(53, byte), _
Ctype(54, byte), Ctype(55, byte), Ctype(57, byte), _
Ctype(57, byte), Ctype(65, byte), Ctype(66, byte), _
Ctype(67, byte), Ctype(68, byte), Ctype(69, byte), _
Ctype(70, byte) ) )

dim mb2 as new xojo.Core.MutableMemoryBlock( mb.Size * 2 )
dim mb2offset as integer

for i as integer = 0 to mb.Size-1
dim upperNibble as int8 = (mb.Int8Value(i) and &hF0) / 16
dim lowerNibble as int8 = (mb.Int8Value(i) and &h0F)

mb2.int8value(mb2offset) = hexbytes.Uint8Value( upperNibble )
mb2offset = mb2offset + 1

mb2.int8value(mb2offset) = hexbytes.Uint8Value( lowerNibble )
mb2offset = mb2offset + 1



Integer.ToHex is part of the new framework, isn’t it?

oh probably
I was poking at something else with mb’s and basically diverted attention for a few minute to do this
then went back to what I was doing

tohex would remove a bunch and trade it off for concatenating a text array

something like

dim parts() as text

for i as integer = 0 to mb.Size-1
parts.apped mb.Int8Value(i).ToHex

return join(parts, “”)

no idea which might be quicker

that’s left as an exercise for the reader :stuck_out_tongue:

Thanks guys! Appreciate the feedback!