In the following code:
var bytes:ByteArray = new ByteArray();
var i:int = -1;
var j:int;
bytes[0] = i; // bytes[0] = -1 == 0xff
j = bytes[0]; // j == 255;
The int j ends up with value 255, rather than -1. I can't seem to find a document defining how indexed access to a ByteArray is supposed to be sign extended - can I reliably assume this behavior, or should I take steps to truncate such values to 8 bit quantities? I'm porting a bunch of code from Java and would prefer to use the indexed access rather that the readByte() etc methods.
The IDataInput interface (implemented by ByteArray) says:
Sign extension matters only when you read data, not when you write it. Therefore you do not need separate write methods to work with IDataInput.readUnsignedByte() and IDataInput.readUnsignedShort().
The same would naturally apply to [] array access, so you wouldn't need to truncate before writing.
I can't see any explicit documentation of that, and nothing that states that array read access is unsigned. If you wanted to ensure that read access gave you an unsigned value back you could say:
j= j<<24>>>24;
and similarly with >> for signed. However with ActionScript being a single implementation, not a general standard, you probably don't have to worry about it
Related
As a newbie in Delphi I run into a problem with an external API.
This external API expects a parameter with one or two value, I think called bitwise parameter.
In Delphi this is done by a set of
The basic is an Enumeration.
TCreateImageTask = (
citCreate = 1,
citVerify
);
This I have put into a set of:
TCreateImageTasks = set of TCreateImageTask
In a function I fill this set with:
function TfrmMain.GetImageTask: TCreateImageTasks;
begin
Result:=[];
if chkCreate.checked then Include(Result, citCreate);
if chkVerify.checked then Include(Result, citVerify);
end;
Now I have to give this Tasks to a external DLL, written in C++
The DLL expects a __int8 value. It may contain one or two TCreateImageTasks. In C++ done by:
__int8 dwOperation = 0;
if (this->IsDlgButtonChecked(IDC_CHECK_CREATE))
{
dwOperation = BS_IMGTASK_CREATE;
}
if (this->IsDlgButtonChecked(IDC_CHECK_VERIFY))
{
dwOperation |= BS_IMGTASK_VERIFY;
}
int32 res = ::CreateImage(cCreateImageParams, dwOperation);
So I have to convert my set of to an integer. I do by
function TfrmMain.SetToInt(const aSet;const Size:integer):integer;
begin
Result := 0;
Move(aSet, Result, Size);
end;
I call with
current task := GetImageTask;
myvar := SetToInt(currentTask, SizeOf(currentTask));
The problem I have now, that myvar is 6 when 2 values are inside the set, 2 if only create is inside the set and 4 if only verify is inside the set. That do not look right to me and the external DLL do not know this values.
Where is my fault?
I guess it works when you remove the = 1 in the declaration of TCreateImageTask?
The = 1 shifts the ordinal values by 1 giving the results you see, but is probably not what is needed. For that we need to know the values for BS_IMGTASK_CREATE and BS_IMGTASK_VERIFY.
My psychic powers tell me that BS_IMGTASK_CREATE = 1 and BS_IMGTASK_VERIFY = 2. Given that these are bit masks they correspond to the values 2^0 and 2^1. This matches the ordinal values 0 and 1.
Thus you should declare
TCreateImageTask = (citCreate, citVerify);
to map citCreate to 0 and citVerify to 1.
It's all about something called Bitwise Operation!
Converting a SET to LONGWORD is widely used in Delphi implementation of Windows API.
This would be what you are looking for:
How to save/load Set of Types?
This was already answered here too:
Bitwise flags in Delphi
I need to compute a hash of a text that is in two pieces, held by two different parties, who should not have access to each others' texts.
Since SHA-1 is incremental I have heard that this should be possible but cannot find any answers with Google of any libraries that implement this.
I'd like the first party to SHA-1 hash its part of the text, and then feed the hash (digest) to the 2nd party, and they will continue with hashing, and compute the total digest of the combined texts. If this is possible, does anyone know of any library that takes a text and a previous hash as input arguments? Preferably in javascript or python but I'm actually happy to accept any language.
Update: I am looking at the source code of the forge (javascript) implementation of SHA-1, and see this code in the update method:
// initialize hash value for this chunk
a = s.h0;
b = s.h1;
c = s.h2;
d = s.h3;
e = s.h4;
And at the end of that method:
s.h0 = (s.h0 + a) | 0;
s.h1 = (s.h1 + b) | 0;
s.h2 = (s.h2 + c) | 0;
s.h3 = (s.h3 + d) | 0;
s.h4 = (s.h4 + e) | 0;
So it seems that by carrying these values + any remaining data in the input buffer over, one should be able to re-create the state of the SHA-1 object at party number 2. So a bit of the buffer will leak over but I think that should be fine. Will investigate further to see if this works.
I am trying to learn how to use the Sparse Coding algorithm with the mlpack library. When I call Encode() on my instance of mlpack::sparse_coding:SparseCoding, I get the error
[WARN] There are 63 inactive atoms. They will be reinitialized randomly.
error: solve(): solution not found
Is it simply that the algorithm cannot learn a latent representation of the data. Or perhaps it is my usage? The relevant section follows
EDIT: One line was modified to fix an unrelated error, but the original error remains.
double* Application::GetSparseCodes(arma::mat* trainingExample, int atomCount)
{
double* latentRep = new double[atomCount];
mlpack::sparse_coding::SparseCoding<mlpack::sparse_coding::DataDependentRandomInitializer> sc(*trainingExample, Utils::ATOM_COUNT, 1.0);
sc.Encode(Utils::MAX_ITERATIONS);
arma::mat& latentRepMat = sc.Codes();
for (int i = 0; i < atomCount; i++)
latentRep[i] = latentRepMat.at(i, 0);
return latentRep;
}
Some relevant parameters
const static int IMAGE_WIDTH = 20;
const static int IMAGE_HEIGHT = 20;
const static int PIXEL_COUNT = IMAGE_WIDTH * IMAGE_HEIGHT;
const static int ATOM_COUNT = 64;
const static int MAX_ITERATIONS = 100000;
This could be one of a handful of issues but given the description it's a little difficult to tell which of these it is (or if it is something else entirely). However, these three ideas should provide a good place to start:
Matrices in mlpack are column-major. That means each observation should represent a column. If you use mlpack::data::Load() to load, e.g., a CSV file (which are generally one row per observation), it will automatically transpose the dataset. SparseCoding will act oddly if you pass it transposed data. See also http://www.mlpack.org/doxygen.php?doc=matrices.html.
If there are 63 inactive atoms, then only one atom is actually active (given that ATOM_COUNT is 64). This means that the algorithm has found that the best way to represent the dictionary (at a given step) uses only one atom. This could happen if the matrix you are passing consists of all zeros.
mlpack will provide verbose output, which may also be helpful for debugging. Usually this is used by using mlpack's CLI class to parse command-line input, but you can enable verbose output with mlpack::Log::Info.ignoreInput = false. You may obtain a lot of output that way, but it will give a better look at what is going on...
The mlpack project has its own mailing list where you may be likely to get a quicker or more comprehensive response, by the way.
I have to communicate with a dll and it lua and this is the function I use to write strings by bytes:
writeString = function(pid, process, address, value)
local i = 1
while i <= String.Length(value) do
local byte = string.byte(value, i, i)
DLL.CallFunction("hook.dll", "writeMemByte", pid..','..process..','..address + (i-1)..','..byte, DLL_RETURN_TYPE_INTEGER, DLL_CALL_CDECL)
i = i + 1
end
DLL.CallFunction("hook.dll", "writeMemByte", pid..','..process..','..address + (i-1)..',0', DLL_RETURN_TYPE_INTEGER, DLL_CALL_CDECL)
end
I basically need to adapt this to write a double value byte by byte.
I just can't think how to make the memory.writeDouble function.
EDIT: this is my readString function:
readString = function(pid, process, address)
local i, str = 0, ""
repeat
local curByte = DLL.CallFunction("hook.dll", "readMemByte", pid..','..process..','..(address + i), DLL_RETURN_TYPE_INTEGER, DLL_CALL_CDECL)
if curByte == "" then curByte = 0 end
curByte = tonumber(curByte)
str = str .. string.char(curByte)
i = i + 1
until (curByte == 0)
return str
end,
My first recommendation would be: try to find a function that accepts strings representing doubles instead of doubles. Implementing the lua side of that would be incredibly easy, since you already have a writeString - it could be something very similar to this:
writeDouble = function(pid, process, address, value)
writeString(pid, process, address, tostring(value))
end
If you don't have that function, but you have access to the dll source, you can try to add that function yourself; it shouldn't be much more complicated than getting the string and then calling atof on it.
If you really can't modify the dll, then you need to figure out the exact double format that the lib is expecting - there are lots of factors that can change that format. The language and compiler used, the operative systems, and the compiler flags, to cite some.
If the dll uses a standard format, like IEE-754, the format will usually have well documented "translations" from/two bites. Otherwise, it's possible that you'll have to develop them yourself.
Regards and good luck!
There are many libraries available for Lua that do just this.
If you need the resulting byte array (string), string.pack should do it; you can find precompiled binaries for Windows included with Lua for Windows.
If you are more interested in using the double to interface with foreign code, I would recommend taking a different approach using alien, a Foreign Function Interface library that lets you directly call C functions.
If you able to, I even more highly recommend switching to LuaJIT, a Just-In-Time compiler for Lua that provides the power, speed and reach of C and assembly, but with the comfort an flexibility of Lua.
If none of these solutions are viable, I can supply some code to serialise doubles (not accessible at the moment).
let reader = selectCommand.ExecuteReader()
let getBytesData (x : IDataReader) =
let len = reader.GetBytes(1, int64 0, null, 0, 0);
// Create a buffer to hold the bytes, and then
// read the bytes from the DataTableReader.
let buffer : byte array = Array.zeroCreate (int32 len)
x.GetBytes(1, int64 0, buffer, 0, int32 len) |> ignore
buffer
let retVal =
List [ while reader.Read() do
yield (reader.GetString(0), getBytesData reader,
reader.GetDateTime(2)) ]
I have above code to read bytes[] from datareader.
getBytesData function takes reader and returns bytes[] from reader.
everything works fine but it getBytesData function is working very non-functional way.
i am creates zero filled byte array just to create array, is there any way of creating dynamic expanding or fixed lenght array
Is there any way i can optimize in F#?
Sorry for kind of question, but i have started a new project on F# to squeeze all juice out of it, so trying to get each line most optimal way
The GetBytes method of the IDataReader doesn't really provide any options for writing the code in a more functional way (it takes an array that it wants to modify, so we simply must give it some array...).
So your version of code is perfectly fine - even though it's not fully functional, you can at least keep the imperative part localized in that single function and keep the rest of your program functional (which is a good result)!
The only change I would do in your code is that I would move reader to the sequence comprehension (to make it more localized) and I would use the use keyword to make sure that it gets properly disposed (also, you don't need the List identifier in the sequence expression):
let retVal =
[ use reader = selectCommand.ExecuteReader()
while reader.Read() do
yield (reader.GetString(0), getBytesData reader, reader.GetDateTime(2)) ]
In my experience that is the best way to do this. Interacting with native .Net methods need to be used somewhat emparitiviley (thus the |> ignore), so encapsulating in a function then using the fn as part of your functional programming. I have asked questions related to using .Net methods in F# if you are interested.
Also make sure you dispose of the reader afterwards too.