Bin2String problem...

Discussion in 'Questions (Windows Mobile)' started by Byak@, Aug 8, 2009.

  1. Byak@

    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
     
  2. Byak@

    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...
     
  3. ghale

    ghale Active Member Licensed 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?
     
  4. Byak@

    Byak@ Active Member Licensed User

    Yes! and i'm don't understan why.
     
  5. ghale

    ghale Active Member Licensed User

    I'm trying the code...
     
  6. ghale

    ghale Active Member Licensed 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: Aug 8, 2009
  7. Byak@

    Byak@ Active Member Licensed User

    i'm try
    Code:
    Sub Globals
    Dim buff(0As 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(pathThen FileDel(path)
    Dim buff(0As 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
     
  8. klaus

    klaus Expert Licensed 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:
    Code:
    [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]
    Code:
    [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]
    Code:
    [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.
     
  9. ghale

    ghale Active Member Licensed User

    Thanks!
     
  10. Byak@

    Byak@ Active Member Licensed User

    Big thanks Klaus and ghale!
     
  11. ghale

    ghale Active Member Licensed User

    Only klaus, he is the expert!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice