How to covert a sbyte[] to BitArray? C#.Net - casting

I'm trying to integrate two systems that deal with images. One system provides an image as a sbyte[] and the other uses a BitArray. I need to take the data from the sbyte[] and convert it into a BitArray. Anyone know how to do this?
Thanks, Paul

The simplest way would be to convert the sbyte[] to a byte[] and then pass it into the normal BitArray constructor. If you're using .NET 3.5 that's easy with LINQ:
byte[] bytes = sbytes.Select(s => (byte) s).ToArray();
BitArray bitArray = new BitArray(bytes);
This is assuming you're executing in an unchecked context already. Otherwise you might want to make the conversion explicitly unchecked:
byte[] bytes = sbytes.Select(s => unchecked((byte) s)).ToArray();
BitArray bitArray = new BitArray(bytes);

BitArray has a constructor that takes a byte array that you might try:
sbyte[] sbytes = ...
BitArray ba = new BitArray(sbytes.Select(x => (byte)x).ToArray());

Related

Node C++ Addon - How do I access a Typed Array (Float32Array) when it's been passed as an argument?

I'd like to make use of the V8 Float32Array data structure. How can I initialise it?
I'd also be interested in direct memory access to the data. How could that be done?
Updated
The best way now is to use the helper Nan::TypedArrayContents.
assert(args[i]->IsFloat32Array());
Local<Float32Array> myarr = args[i].As<Float32Array>();
Nan::TypedArrayContents<float> dest(myarr);
// Now use dest, e.g. (*dest)[0]
There's a good example of this in node-canvas.
Original Answer, which shows why the helper is useful
The v8 API is changing rapidly right now so this depends on your version of node/io.js. To access the data from the array provided as an argument, this should work for node 0.12 through io.js <3.0:
assert(args[i]->IsFloat32Array()); // These type-check methods are available.
Local<Float32Array> myarr = args[i].As<Float32Array>();
void* dataPtr = myarr->GetIndexedPropertiesExternalArrayData();
In io.js >=3.0 (v8 4.3) you should instead use ArrayBuffer::GetContents. (I haven't used this yet and will update this when 3.0 is released.) Docs are here.
In node 0.10, TypedArrays were home-brewed. This was one way of doing it:
Local<Object> buffer = args[i]->Get(NanNew("buffer"))->ToObject();
void* dataPtr = buffer->GetPointerFromInternalField(0);
Constructing a typed array can be done with:
Float32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), size * 4), 0, size);

Storing c++ structs/objects in NSData

I am developing an iOS with OpenCV.
OpenCV is an Open Source Image Library and works with C++.
I want to store some data from the Library in an NSData object.
Is it possible to convert a c++ struct to NSData without losing the object?
- (void)addMat:(cv::Mat)mat andImageName:(NSString *)name {
self.myMat = [NSData dataWithBytes:&mat length:sizeof(mat)];
self.imageName = name;
}
Now I'm using the code above.
But this NSData only stores the pointers not the actual data.
When I try to get the object back I
pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
My data should be stored in a SqLite Database.
Do you have an idea to convert everything ?
I tried to convert every single property. This worked for some properties quite well and for others it didn't work.
I hope you could help me.
Thank you.
Greetings,
Alexander Heinrich
You may write bytes from objects inside data, but it doesn't store object. For example object can have pointers at other objects and they can become invalid after some time, but yours NSData copy will not know about that.
You should turn on Objective-C++ and work direct with C++ structures/classes.

Accessing values in Flex Object result from Zend AMF originated as PHP associative array

I cannot access values in Flex Object (ArrayCollection) after I receive it from Zend AMF. The original type sent is PHP associative array which is simply returned like
return $this->sections['initial_setup'];
PHP Variable view:
The required result sent looks like this in Charles AMF RPC tab:
But when I receive the data in Flex as Object (or String[] - it doesn't matter), I cannot access the property values in such code
var result:Object = event.result;
if (result['database'] == 'yes' && result['admin'] == 'yes')
// continue branch ...
and I get exception on the if-line:
Error: Unknown Property: 'database'.
at mx.collections::ListCollectionView ...
Finally, I can see in Eclipse variables view that ResultEvent instance carries a result of type ArrayCollection with 0 length and the values received are visible with D icon (I couldn't find what D adornment means):
But why I still can't access them at all and what should I do to use them?
I have tried to change types of Array or ArrayCollection instead of Object. Also there is a thread discussing similar problem, but after trying that out, it doesn't help too.
Any help will be much appreciated :o)
EDIT 1:
Here is the code of FB generated super class constructor for the ConfigurationService:
// Constructor
public function _Super_ConfigurationService()
{
// initialize service control
_serviceControl = new mx.rpc.remoting.RemoteObject();
// initialize RemoteClass alias for all entities returned by functions of this service
var operations:Object = new Object();
var operation:mx.rpc.remoting.Operation;
operation = new mx.rpc.remoting.Operation(null, "readSettings");
operation.resultType = Object;
operations["readSettings"] = operation;
operation = new mx.rpc.remoting.Operation(null, "writeSettings");
operations["writeSettings"] = operation;
operation = new mx.rpc.remoting.Operation(null, "readDBSettings");
operation.resultType = valueObjects.ConnectionParams;
operations["readDBSettings"] = operation;
operation = new mx.rpc.remoting.Operation(null, "writeDBSettings");
operations["writeDBSettings"] = operation;
operation = new mx.rpc.remoting.Operation(null, "readInitSetupCompletion");
operation.resultType = Object;
operations["readInitSetupCompletion"] = operation;
operation = new mx.rpc.remoting.Operation(null, "writeInitSetupCompletion");
operations["writeInitSetupCompletion"] = operation;
_serviceControl.operations = operations;
_serviceControl.convertResultHandler = com.adobe.serializers.utility.TypeUtility.convertResultHandler;
_serviceControl.source = "ConfigurationService";
_serviceControl.endpoint = "gateway.php";
preInitializeService();
model_internal::initialize();
}
So what's happened here is that the Array that's serving as the source for your ArrayCollection is acting as a generic Object with those same two properties. Probably the generated code is assuming you'll always get back more than one object and is having problems when your data doesn't fulfill the assumptions Adobe's engineers made about it. One of the reasons I don't like generated code :-).
Check out these resources on how to "roll your own."
Thoughts on Remoting
AMFPHP to Flex Object Relational Mapping
Implementing PHP Services
I think this last (3) is closest to what you probably have in PHP. If you do decide to go with a VO, you can probably just add an $explicitType to your row return and not need to change too much else on the PHP side. You'll probably need to regenerate your services if you go that route, because I suspect the generated code will be different. The good news is the Adobe engineers probably did think of the case where you have an explicit type, but only one record.
Another fix is to just check your AC for having a source of zero length that is not null and looking for your properties on the edges of that.

OpenISO8583.Net BCD Formatted Message Type

I am learning how to use the OpenISO8583.Net code. I have derived a new ISO8583 class from the original one. One of the differences in my new class is that the Message Type field is going to be [Numeric, BCD]; so I wrote this as part of the class contructor (I do have a custom DefaultTemplate declared as part of the class):
DefaultTemplate.MsgTypeFormatter = Formatters.Bcd;
I created a new message with a message type of 0100 (MsgType._0100_AUTH_REQ). Without assigning any other bits.
FDISO8583 fdISO8583 = new FDISO8583();
fdISO8583.MessageType = FDISO8583.MsgType._0100_AUTH_REQ;
byte[] testMsg = fdISO8583.ToMsg();
My ending result byte array is: 01-00-00-00-00-00-00-00-00-00 which I think is correct. First 2 bytes for the message type with the BCD value and the bitmap is all zeroes.
Now the problem is when I test unpacking it. Using:
fdISO8583 = new FDISO8583();
int pos = fdISO8583.Unpack(testMsg, 0);
The resulting message type gets a value of zero.
I need help understanding if this is a problem on the way I defined the class or a bug in the ISO8583 code.
You have indeed found a bug. I logged it as Issue 13 and have fixed it. I have release version 0.5.2 on nuget and uploaded the bin file to the googlecode project.

regarding conversion from reference to string

i am doing RSA encryption
i want to convert reference of public key class to string so that i can pass to server
//declaration
const CRSAPrivateKey &iRSAPrivateKey =iRSAKeyPair->PrivateKey();
const CRSAPublicKey &iRSAPublicKey =iRSAKeyPair->PublicKey() ;
i have convert &iRSAPublicKey into TBuf
i tried lot but fails to convert
plz help me out from situation
thanks in advance
If you're using CRSAPublicKey, you probably downloaded the Symbian cryptography library and its documentation from http://developer.symbian.com/main/tools_and_sdks/developer_tools/supported/crypto_api/index.jsp
Admitedly, the documentation isn't explicit but I would venture that you can just send the modulus and exponent components to any other RSA engine in order to reconstitute the public key:
HBufC8* localModulusBuffer = iRSAPublicKey.N().BufferLC();
HBufC8* localExponentBuffer = iRSAPublicKey.E().BufferLC();
Then simply copy the 2 HBufC8 into a TBuf if you really need it.
Just remember that methods with a trailing "C" leave what they return on the cleanup stack.