B4A Library ByteConverter library

Discussion in 'Additional libraries, classes and official updates' started by agraham, Dec 7, 2010.

  1. agraham

    agraham Expert Licensed User

    It's occasionally useful to be able to poke around in things as arrays of bytes so this library lets you do it.

    It can transform arrays of primitive types to and from arrays of bytes with the "endian-ness" of the conversion specified. If you need endian-ness you will either know about it or rather quickly find out. :)

    Byte arrays can be transformed to and from a hexadecimal string representation.

    Strings can be transformed to and from arrays of Bytes using specified encodings and to and from arrays of Chars.

    An array copy method is thrown in for good measure.

    EDIT :- Version 1.1 posted. See post #4 for details.

    Download link: www.b4x.com/android/files/ByteConverter1.1.zip
     
    Last edited by a moderator: Dec 15, 2015
  2. klaus

    klaus Expert Licensed User

    The HexToBytes function works only with upper case strings.
    The help says it should work with both upper and lower case.

    Best regards.
     
  3. agraham

    agraham Expert Licensed User

    It should have, except for a stupid mistake translating a trivial bit of C# code to a just as trivial bit of Java code :eek:.

    I'll fix it.
     
  4. agraham

    agraham Expert Licensed User

    Version 1.1 now posted fixes the stupid error in HexToBytes.
     
  5. klaus

    klaus Expert Licensed User

    CodePageFile object

    Hi Andrew,

    I would be pleased having in the ByteConverter a CodePageFile object like in B4PPC. It would be intersting to read files encoded with the ANSI (1252) code page for the special characters.
    Or, why not, in the StringUtils LoadCSV3 and SaveCSV3.

    Best regards and thank you in advace.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. klaus

    klaus Expert Licensed User

    Thank's Erel :)
    Sorry, missed that one :(

    Best regards.
     
  8. alan1968

    alan1968 Active Member Licensed User

    hello ,

    the result byte 2's complement is how to have it in pure binary
    ex: FFH = 255

    thx!
    my code
    ....
    string_data=mid(string_data,28,string_data.Length-27)
    nb_data=string_data.Length/3
    Log("data:" & string_data)
    Log("nb:" & nb_data)
    For i=0 To nb_data-1
    hex=mid(string_data,1+3*i,3).Trim
    Log("hex:>"&hex&"<")
    Bytes=conv.HexToBytes(hex)
    Log("Byte:"&Bytes(0))
    Next
    ...

    log:
    ** Activity (main) Resume **
    ** Activity (main) Create, isFirst = true **
    data:03 ce 00 00 00 a1 00 00
    nb:8
    hex:>03<
    data:3
    hex:>ce<
    data:-50
    hex:>00<
    data:0
    hex:>00<
    data:0
    hex:>00<
    data:0
    hex:>a1<
    data:-95
    hex:>00<
    data:0
    hex:>00<
    data:0
    ** Activity (main) Resume **
     
  9. agraham

    agraham Expert Licensed User

    That is correct. Bytes are signed numbers in Java spanning -128 = 80H to +127 = EFH.
     
  10. alan1968

    alan1968 Active Member Licensed User

    it's possible unsigned byte value from 0 to 255 ?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    There is no unsigned byte in Basic4android (which uses the same type system as Java).
    You can use Integer to show values larger than 127.
     
  12. transittransist

    transittransist New Member Licensed User

    byte array length

    I use byteconverter library to convert byte values to hex string.
    I send a udp packet("test" string) which is size 4 bytes. When I get it and convert to hex I see "74657374000000000000000000000.............." in the textbox. There is 8000 bytes, first 4 byte is ok, but I send 4 bytes, so why I see these zeros. my code is below;

    Dim data() As Byte
    data = PacketReceived.data
    EditText6.Text = Conv.HexFromBytes(data)

    PacketReceived.Length returns 4.
    when I try to restrict byte array length with such a code:
    Dim data(PacketReceived.Length) As Byte

    nothing changes. Conv.HexFromBytes(data) returns "74657374000000000000000000000.............." again.

    How can I get rid of this zeros?
     
    KitCarlson likes this.
  13. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the complete code that converts the bytes to hex string?
     
  14. transittransist

    transittransist New Member Licensed User

    I realize that my problem is about UDP library. When I checked the data length I saw the problem.

    PacketReceived.Length 'returns 4

    However,
    PacketReceived.data.Length 'returns 8000

    So, the problem is in UDP library. I have used string manipulation and get rid of zeros.

    Thanks,
     
  15. PaulR

    PaulR Active Member Licensed User

    I am having a bit of trouble with the syntax necessary for this library. Could somebody please post an example of how to convert an integer value of -1 to a 32 bit sequence of bytes please?

    And how would I append that to another Byte variable, or string?

    Any assistance would be most appreciated!

    Cheers

    Paul

    edit: nevermind, my issue is trying to send raw data without any encoding. I worked out the syntax and got a log of all the encoders, but there doesn't seem to be a "none" option.
     
    Last edited: Mar 17, 2012
  16. bluedude

    bluedude Well-Known Member Licensed User

    Permission to use libraries in an application

    Hi,

    I see you don't accept private messages so it is hard to ask permission to use libraries. Anyway, hereby my public request to use your libraries.

    Hereby i'm asking you official permission to use the following libraries in commercial and non-commercial applications. I hereby also request how I should atrribute your work in a proper manner. My plan is to provide a separate list view with all the library developers of the libraries that I use.

    Libraries to use
    Byteconvertor
    Dialogs

    The current application I develop can be found here (in Dutch)

    ThingsTank - Android applicatie
     
  17. bluedude

    bluedude Well-Known Member Licensed User

    I forgot one, Reflection library.
     
  18. agraham

    agraham Expert Licensed User

    You've found the Creative Commons license I see. Your attribution plan looks fine to me. Any of my libraries utilising 3rd party code with different licensing conditions, basically Jpeg (GNU PL 2), SVG (Apache 2.0) and PageTurnView (Apache 2.0), mention this somewhere in the xml help.
     
  19. bluedude

    bluedude Well-Known Member Licensed User

    Alright, I will store a screenshot of this conversation for my own archiving purposes as a kind of confirmation/prove.

    I want to handle all this stuff in a professional matter to prevent problems later on.

    Cheers and thanks for the great work!
     
  20. Gearcam

    Gearcam Active Member Licensed User

    Is it possible to use this to convert any image.png into a binary representation of it to save in a databse ?

    Any help ?

    Steve
     
    kammerhoferj likes this.
Loading...