Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Code Samples & Tips > Additional Libraries
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Additional Libraries Users contributed libraries.
This sub-forum is only available to licensed users.

ByteConverter library

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-19-2008, 12:45 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default ByteConverter library

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
File Type: zip BytesConverter1.4a.zip (42.8 KB, 166 views)
File Type: zip AdaptorsInfo1.0.zip (3.9 KB, 35 views)

Last edited by agraham : 06-10-2011 at 02:24 PM.
Reply With Quote
  #2 (permalink)  
Old 06-21-2008, 06:02 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

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 by agraham : 06-23-2008 at 06:50 PM.
Reply With Quote
  #3 (permalink)  
Old 09-19-2009, 12:12 PM
Knows the basics
 
Join Date: Dec 2007
Posts: 76
Default 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
Reply With Quote
  #4 (permalink)  
Old 09-19-2009, 01:10 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

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
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #5 (permalink)  
Old 09-19-2009, 01:21 PM
Knows the basics
 
Join Date: Dec 2007
Posts: 76
Default 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
Reply With Quote
  #6 (permalink)  
Old 01-23-2010, 03:16 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

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.
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #7 (permalink)  
Old 03-11-2010, 11:06 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

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.
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #8 (permalink)  
Old 11-03-2011, 10:22 AM
derez's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Posts: 918
Awards Showcase
Beta Tester Competition Winner 
Total Awards: 2
Default 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 ?
__________________
David Erez
Ramat Hasharon, Israel

Last edited by derez : 11-03-2011 at 10:27 AM.
Reply With Quote
  #9 (permalink)  
Old 11-03-2011, 11:16 AM
Basic4ppc Expert
 
Join Date: May 2008
Location: Berkshire, UK
Posts: 762
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Quote:
Originally Posted by derez View Post
B4ppc B4A
59 59
48 48
178 -78
113 113
130 -126
173 -83
43 43
157 -99
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.
Reply With Quote
  #10 (permalink)  
Old 11-03-2011, 11:49 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Bytes are treated as signed numbers in Java. See here. It should only be a representational problem, the underlying byte values are the same.
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Door library (Beta) - Special library Erel Official Updates 60 01-13-2011 12:23 PM
Merging Outlook library and Phone library Erel Official Updates 11 09-15-2010 10:22 AM
PhoneticAlgorithms Library (ex-StringComparison Library) moster67 Additional Libraries 10 11-11-2008 08:46 PM


All times are GMT. The time now is 02:17 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0