Bin2String problem...

Byak@

Active Member
Licensed User
in my program i must read binary file,convert it to string and then convert string to bytes and create new file.
but i have problem...my algorithm work only with one file!

please see example and help me
 

Byak@

Active Member
Licensed User
i can convert binary to hex and all work normal but length of hex's string big (in 2 times of original bin file) and for my program it is very very bad...
 

IoSonoPiero

Active Member
Licensed User
Longtime User
So, please forgive me if I've not understood well:

I've seen that in your app you open a binary file, copy its content into a binary object, then assign to s the content of it converting the binary data into a string made of bytes.

Then, you'll do the opposite: take the content of the s string, convert it to bytes, then write it to the disk.

This operation is not completed correctly, because the original file and the target one are different.

This is right? I've understood well?
 

Byak@

Active Member
Licensed User
Yes! and i'm don't understan why.
 

IoSonoPiero

Active Member
Licensed User
Longtime User
Modified: It's false what I said.


mmh, it seems like the length of content of string "s" is different from the lenght of the buffer buff
 
Last edited:

Byak@

Active Member
Licensed User
i'm try
B4X:
Sub Globals
Dim buff(0) As byte
s=""
End Sub

Sub App_Start
path=AppPath&"\1.jpg"
bit.New1
FileOpen(c,path,cRandom)
Dim buff(FileSize(path)) As byte
bin.New1(c,True)
bin.ReadBytes(buff(),ArrayLen(buff()))
FileClose(c)
[COLOR=Red]Msgbox(ArrayLen(buff()))[/COLOR]
s=bin.BytesToString(buff(),0,ArrayLen(buff()))
[COLOR=Red]Msgbox(StrLength(s))[/COLOR]
button1_click
'textbox1.Text=s
'Form1.Show
End Sub

Sub Button1_Click
path=AppPath&"\test.jpg"
If FileExist(path) Then FileDel(path)
Dim buff(0) As byte
FileOpen(c,path,cRandom)
bin.New1(c,True)
buff()=bin.StringToBytes(s)
bin.WriteBytes(buff())
FileClose(c)
[COLOR=Red]Msgbox(ArrayLen(buff()))[/COLOR]
End Sub

all 3 msgboxes show one length :(

and if you try program with file "edit_table.zip" from my archive you see...it work!but only with this file
 

klaus

Expert
Licensed User
Longtime User
In your code you are using the BytesToString routine from the Binary Library. In that case the bytes of the buffer are converted into ASCII characters and only the 127 first are used all others are replaced by a questionmark "?".

If you use the BytesToString routine from the Bitwise Library and initializes the bit object with bit.New2(1252), the Windows ANSI encoding, instead of bit.New1, ASCII encoding, you get a correct test.jpg image back.

Attached the modified version of your program.

Code changes:
B4X:
[FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]'bit.New1[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]bit.New2([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1252[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT][/SIZE][/FONT]
B4X:
[FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]'s=bin.BytesToString(buff(),0,ArrayLen(buff()))[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]s=bit.BytesToString(buff(),[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2],[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]ArrayLen[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2](buff()))[/SIZE][/FONT][/SIZE][/FONT]
B4X:
[FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]'buff()=bin.StringToBytes(s)[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]buff()=bit.StringToBytes(s,[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2],[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]StrLength[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2](s))[/SIZE][/FONT][/SIZE][/FONT]

Best regards.
 

Byak@

Active Member
Licensed User
Big thanks Klaus and ghale!
 
Top