Page 1 of 1

Base64Str empty string return

Posted: Wed Sep 06, 2023 4:26 pm
by fingus

Code: Select all

s$ = "AACrj6j/AAAAAAAUDgAAAAAAAf////8gIYAECQcohwAAiAIABAQAAAAAAAOAIIf/gBDn/5QQGgBr/A="
data$ = Base64Str(s$, true)
print(data$)
Gives back an empty String under Hollywood Linux 64bit, Version 9.1.

Re: Base64Str empty string return

Posted: Wed Sep 06, 2023 6:00 pm
by Flinx
I think your Base64 string results in binary data. What did you expect?

Code: Select all

Print(ByteLen(data$))

Re: Base64Str empty string return

Posted: Thu Sep 07, 2023 9:07 am
by fingus
You are right.

I want to translate this Javascript-Function translate_bytes to Hollywood. Its needed for a visualisation of our Mitsubishi-AC Units.

So i need maybe a Binary Bytes to Hex-Converter.

In my other Hollywood-Projects i did not face Byte, Binary, Hex..etc conversions so i'm a bit confused about it.

I tried ChatGTP for convertig, but it only know normal LUA which differs much from the Hollywood-Accent.
Hollywood with its String-Library has much of powerful Commands inbuild.

Re: Base64Str empty string return

Posted: Thu Sep 07, 2023 9:05 pm
by Flinx
I don't understand this Javascript program in a hurry, it's too big for that. But I can help with special conversions, I did need a lot by myself.
For example this shows your binary data as hex string without the $ sign of the HexStr() function:

Code: Select all

Local Debugstr$=""
For Local i=0 To StrLen(data$, #ENCODING_RAW)-1
	Local byte$=MidStr((HexStr(ByteAsc(data$, i))),1)
	If StrLen(byte$)<2 Then byte$="0"..byte$
	Debugstr$=Debugstr$ ..byte$.." "
Next
DebugPrint(Debugstr$)

Re: Base64Str empty string return

Posted: Thu Sep 07, 2023 10:47 pm
by plouf
however isnt something wrong with encoding fingus original Base64 encoded? seems that misses last byte
while with other encoded tects does ok

Code: Select all

datas$ = "AACrj6j/AAAAAAAUDgAAAAAAAf////8gIYAECQcohwAAiAIABAQAAAAAAAOAIIf/gBDn/5QQGgBr/A="  ; Original after reencode you NO /A=
;datas$="cHVyZXRleHQ="  ; puretext ok
;datas$="VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIDEzIGxhenkgZG9ncy4=" ; other binary also ok


data$ = Base64Str(dataS$, True)
DebugPrint(dataS$)
dataS$ = Base64Str(data$,False)
DebugPrint(dataS$)

Re: Base64Str empty string return

Posted: Fri Sep 08, 2023 10:50 am
by Flinx
plouf wrote: Thu Sep 07, 2023 10:47 pm however isnt something wrong with encoding fingus original Base64 encoded? seems that misses last byte
Yes. Maybe the string should end with ==

Re: Base64Str empty string return

Posted: Fri Sep 08, 2023 11:26 am
by fingus
Try this string, taken from https://community.home-assistant.io/t/m ... r/411025/5
AACyiKT/AAAAAAASCgAAAAAAAf////8sv4AEEAAkigAAiAAAAgAAAAAAAAOAIIr/gBCN/5QQAQBufw==
Maybe there was a copy-paste error by me.

Re: Base64Str empty string return

Posted: Fri Sep 08, 2023 11:29 am
by Flinx
By the way, now I just see that this hex output I needed some time ago can be made more elegant.

Code: Select all

Local Debugstr$=""
For Local i=0 To StrLen(data$, #ENCODING_RAW)-1
	Local byte$=FormatStr("%.2X", ByteVal(MidStr(data$,i,1, #ENCODING_RAW),#BYTE))
	Debugstr$=Debugstr$ ..byte$.." "
Next
DebugPrint(Debugstr$)

Re: Base64Str empty string return

Posted: Fri Sep 08, 2023 8:06 pm
by jPV
fingus wrote: Wed Sep 06, 2023 4:26 pm

Code: Select all

s$ = "AACrj6j/AAAAAAAUDgAAAAAAAf////8gIYAECQcohwAAiAIABAQAAAAAAAOAIIf/gBDn/5QQGgBr/A="
data$ = Base64Str(s$, true)
print(data$)
Gives back an empty String under Hollywood Linux 64bit, Version 9.1.
It works as expected, because the decoded data starts with a null byte, which terminates a string and cuts out the rest of the data when processed as a string. So if you try to print that, or do any other string manipulation, it shows up as an empty string. Whatever you plan to do with the data, handle it as binary data.

You can see all the data when you write it as a file and examine it with a hex editor. Use StringToFile(data$, "test.dat") to try it.

Re: Base64Str empty string return

Posted: Wed Sep 13, 2023 9:42 am
by fingus
Ok, then this isn't a bug.

Thanks all here for your help/hints.

Topic can be closed now.