ByteConverter library

Discussion in 'Additional Libraries' started by agraham, Jun 19, 2008.

  1. agraham

    agraham Expert Licensed User

    Here is my latest library that is probably no use to anyone except me :) It should work on .NET 1.0 and later and on device and desktop.

    It grew out of playing with the AdaptorsInfo library that I knocked up for Cableguy. AdaptorsInfo returns a byte array which is basically a C Struct containing several different data types detailing a network adaptor's data.

    This library takes a specified start position in a byte array and returns the data in the array converted to a required datatype. It can also reverse that process, taking a datatype and returning a byte array. For good measure string conversions to and from byte arrays are provided. As this library is really intended for Win API use the byte arrays representing string data assume that the strings are terminated by a zero character which is C/Win32 convention. Hence, unlike B4ppc/.NET strings they cannot contain a zero character.

    With the BytesConverter library there is a help file, demo and the source for merging with compiled exes.

    I have also included an improved AdaptorsInfo library that can return data for any of the network adaptors on a system, the original only returned the first. With the library is a demo. For technical reasons it is not possible to merge this library with a compiled exe. This should be possible in the next version of B4ppc - Erel willing!

    EDIT: Note that the AdaptorsInfo demo needs the BytesConverter library. I didn't include it in the zip as I don't believe in scattering copies of dlls around in zips too often as they may need to be updated at some time in the future.

    EDIT: Version 1.2 with a CodepageFile object added. Updated help, new demo and updated source included.

    EDIT: Version 1.3 posted. See post #6 for details.

    EDIT: Version 1.4 posted. See post#7 for details.

    EDIT: Version 1.4a posted with an omission in the cs file that prevented merged compilation corrected.
     

    Attached Files:

    Last edited: Jun 10, 2011
  2. agraham

    agraham Expert Licensed User

    Version 1.2 posted above. It now includes a CodepageFile object that implements FileOpen, FileRead, FileReadToEnd, FileWrite and FileClose in imitation of the native B4ppc methods.

    Whereas native B4ppc File methods can only read/write ASCII or UTF8 text files this objects reads/writes using a specified code page. This enables direct use of a codepage without having to go through a byte array as an intermediate step.
     
    Last edited: Jun 23, 2008
  3. skipper

    skipper Member Licensed User

    time_t fields

    Hello Andrew,

    I'm using your library to access a thirdy party binary file containing stock data. The file has a fixed header of 144 bytes and a body made of n record of 44 bytes. Unfortunately, in both header and body, there are 4 bytes storing a "time_t" value that I'm unable to convert.

    Here is the

    'struct HistoryHeader
    '{
    ' Int Version; // database Version
    ' char copyright[64]; // copyright info
    ' char symbol[12]; // symbol name
    ' Int period; // symbol timeframe
    ' Int digits; // the amount of digits after decimal point in the symbol
    ' time_t timesign; // timesign of the database creation (4 bytes)
    ' time_t last_sync; // the last synchronization Time (4 bytes)
    ' Int unused[13]; // To be used in future (13 elem. array of Int -> 13*4=52 bytes)
    '};


    and

    'struct RateInfo
    '{
    ' time_t ctm; // current Time in seconds (4 bytes) Contains both date and time
    ' double open;
    ' double low;
    ' double high;
    ' double close;
    ' double vol;
    '};


    The format should be the same as the fields inclueded in the Adaptors structure

    '632 time_t LeaseObtained;4
    '636 time_t LeaseExpires;4


    but they have not been converted.

    Could you please give me any suggestion on how to convert such values or, better, could you please update your library to allow "to" and "from" conversion?

    Many thanks
    Mimmo
     
  4. agraham

    agraham Expert Licensed User

    A time_t is a signed 32 bit integer representing the number of seconds elapsed since midnight (00:00:00), January 1, 1970, Coordinated Universal Time (UTC). It will reach its' maximum value at midnight on the 18th Jan 2038 from whence it will be no use. You can convert it like this.

    Code:
    timeT = Converter.Int32FromBytes(buf(), posn) ' seconds since 01/01/1970
    timeTticks = timeT * cTicksPerSecond  ' ticks since 01/01/1970
    dotnetTicks = DateParse ("01/01/1970")  + timeTticks ' .NET date and time as ticks
     
  5. skipper

    skipper Member Licensed User

    Time_t

    Many thanks Andrew,
    as usual your help is invaluable. Having a definitive answer in less than one hour on saturday in the afternoon is another of your speed records....

    Thanks again
    Mimmo
     
  6. agraham

    agraham Expert Licensed User

    Version 1.3 posted has four new methods that let you manipulate single or multi dimensioned arrays of primitive types as a buffer of bytes. These are BlockCopy, ByteLength, GetByte and SetByte.

    Note that the string conversion methods names have changed in an attempt to make them a bit more consistent with what they do.
     
  7. agraham

    agraham Expert Licensed User

    Version 1.4 has BitmapToBytes and BitmapFromBytes methods to transform Bitmaps to and from arrays of bytes representing RGB pixel values.

    It also has a new Union object, see the help for details.
     
  8. derez

    derez Expert Licensed User

    Bytes from Hex

    Andrew, or anyone

    Please see if you can help with the following, I got lost...:
    I wrote an application in B4A, keeping passwords and data in an encrypted file using DES encryption, and it works fine.
    Now I write a parralel program in B4ppc for the desktop.
    The data file is a text file and the data lines are Hex.
    the sequence of operation for reading is :

    -read a line
    -convert from Hex to Bytes
    -Decrypt
    -convert from Bytes to string

    code in b4ppc:
    Code:
    FileOpen(c1,AppPath & "/codes/data.txt" , cRead)
    password = FileRead(c1)
    datax() = bc.HexToBytes(password)
    code() = Des.Decryptbytes(datax(),
    0)
    password = bc.StringFrom1Bytes(code(),
    0,ArrayLen(code()))
    code in b4a:
    Code:
    tr1.Initialize(File.Openinput(File.DirRootExternal & "/Codes""data.txt"))
    password = tr1.ReadLine
    datax = bc.HexToBytes(password)
    datax = c.Decrypt(datax, kg.Key, 
    True)   
    password = Bc.StringFromBytes(datax, 
    "UTF8")
    Seeing that the data is not read correctly, I found that there is difference between the bytes converted from Hex by B4A and those converted by B4ppc, like this:

    B4ppc B4A
    59 59
    48 48
    178 -78
    113 113
    130 -126
    173 -83
    43 43
    157 -99

    I tried to change the data file encoding after moving to the pc, but no success.
    How can I make the reading and writing be the same for both ?
     
    Last edited: Nov 3, 2011
  9. mjcoon

    mjcoon Well-Known Member Licensed User

    On the face of it that is clearly a sign-bit problem, with the top bit of the byte (values 128 and up) being interpreted as a sign bit. But I am sure you spotted that!

    What I don't understand is why the B4A works with that interpretation. Or is it just to do with the way you are viewing debug data?

    Although I worked a lot, at work, with encryption, never with B4xxx.

    Mike.
     
  10. agraham

    agraham Expert Licensed User

    Bytes are treated as signed numbers in Java. See here. It should only be a representational problem, the underlying byte values are the same.
     
  11. derez

    derez Expert Licensed User

    Thank you, then it must be something I do wrong in the decription part.
     
  12. moster67

    moster67 Expert Licensed User

    Thanks Agraham - the AdaptorsInfo-library was just what I needed to add some functionality to an old desktop-application of mine. Works great!
     
  13. lqg2118

    lqg2118 Member Licensed User

    I used this function in my program, the old version worked properly.

    It's powerful and easy.

    thanks for update.
     
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