Strange behaviour with double.TryParse method - double

I have a very strange behaviour with the double.TryParse() method.
I have this
double result= -1;
bool ok;
ok = double.TryParse("0,28", out result); // ok=true, result=0.28000000000000003
ok = double.TryParse("0,52", out result); // ok=true, result=0.52000000000000002
ok = double.TryParse("0,44", out result); // ok=true, result=0.44
The CurrentCulture is French, I also tried specifying InvariantCulture, and this didn't solve anything.
I tried the same code in another project, and here it worked well (both WebSite in .NET 4.0). I don't know what is wrong in my project, no special references added, web.config pretty clean ... Any idea ?

Related

get IMEI on iPhone with CoreTelephony?

I have tried the accepted answer of
How to get IMEI on iPhone?
but I got an empty string.
I saw somebody suggested to use CoreTelephony framework,
but I am not sure how to use it to obtain the IMEI.
Any suggestion on how to use this private API?
NOTE: this does not work anymore!
Haven't tested on any new iOS.
You have to add CoreTelephony.h to your project.
Make sure the header has
int * _CTServerConnectionCopyMobileEquipmentInfo (
struct CTResult * Status,
struct __CTServerConnection * Connection,
CFMutableDictionaryRef * Dictionary
);
Then you can try this code:
#import "CoreTelephony.h"
void getImei() {
struct CTResult it;
CFMutableDictionaryRef kCTDict;
conn = _CTServerConnectionCreate(kCFAllocatorDefault, ConnectionCallback,NULL);
_CTServerConnectionCopyMobileEquipmentInfo(&it, conn, &kCTDict);
NSLog (# "kCTDict is %#", kCTDict);
CFStringRef meid = CFDictionaryGetValue(kCTDict, CFSTR("kCTMobileEquipmentInfoMEID"));
NSLog (# "kCTMobileEquipmentInfoMEID is %#", meid);
CFStringRef mobileId = CFDictionaryGetValue(kCTDict, CFSTR("kCTMobileEquipmentInfoCurrentMobileId"));
NSLog (# "kCTMobileEquipmentInfoCurrentMobileId is %#", mobileId);
}
Here's the CoreTelephony.h
You can check my example project.
Note: I don't think the code works on the simulator and your app might get rejected.

NPAPI plug-in in safari can not call js function?

all.I want to call a js function to show something in my plugin.This is my code
NPObject* npwindow = NULL;
NPError ret = browser->getvalue(mInstanceForJS, NPNVWindowNPObject, &npwindow);
if (ret != NPERR_NO_ERROR)
return ;
// Get window object.
NPVariant windowVar;
NPIdentifier winID = browser->getstringidentifier("window");
bool bRet = browser->getproperty(mInstanceForJS, npwindow, winID, &windowVar);
if (!bRet)
{
browser->releaseobject(npwindow);
return ;
}
NPObject* window = NPVARIANT_TO_OBJECT(windowVar);
NPVariant voidResponse;
NPVariant elementId;
STRINGZ_TO_NPVARIANT([info UTF8String], elementId);
NPVariant args[] = {elementId};
NPIdentifier funcID= browser->getstringidentifier([funName UTF8String]);
bRet = browser->invoke(mInstanceForJS, window, funcID, args, 1, &voidResponse);
browser->releasevariantvalue(&windowVar);
when called bRet = browser->invoke(mInstanceForJS, window, funcID, args, 1, &voidResponse);,Safari can not responsed.Is there any errors?
npwindow is already the window object; you're effectively querying for "window.window". Granted, I don't know why this wouldn't work, but it seems a little weird.
That's problem #1.
Problem #2 is that you're using STRINGZ_TO_NPVARIANT to store the result of UTF8String. STRINGZ_TO_NPVARIANT doesn't copy the memory, so you could be in trouble if the function wanted to hang onto that string, since the string returned by that will be freed when your autorelease pool cycles. Of course, that could also be a memory leak. Either way, the correct way to pass a string to the browser is to allocate memory for it using NPN_MemAlloc and then copy the string in. Then pass that pointer to the browser. See http://npapi.com/memory for more info.
Problem #3 is that you haven't given us any idea of when you are running this code; it's quite possible that you are trying to run this code too early in the plugin or page lifecycle and thus it may not work because of that.
Then there is another question: What do you mean by "Safari can no responsed"? Forgetting the grammatical error, I'm not sure what you mean by this. Does it hang? is bRet false? Does your computer suddenly get encased in ice, thus halting all processing? If the above is not helpful, please answer these questions and I'll try again.

quickfix MDEntryTime No Milliseconds

I'm trying to generate an MDEntryTime with a value that contains milliseconds. The milliseconds are getting dropped once they get entered into the Message. Upon further inspection, I find this little gem inside Field.h
explicit UtcTimeOnlyField( int field, const UtcTimeOnly& data, bool showMilliseconds = false )
The constructor for MDEntryTime doesn't allow for the coder to set showMilliseconds=true. How do people get around this?
The constructor for MDEntryTime doesn't allow for the coder to set showMilliseconds=true.
In fact, it does. The following should work for you:
msg.set(FIX::MDEntryTime(FIX::UtcTimeOnly(time(NULL), true)));
This one works for me. I did a method that ensure milliseconds are present in the UtcTimeOnly:
FIX::UtcTimeOnly getTimeMillis()
{
timeval tv;
gettimeofday(&tv, NULL);
return FIX::UtcTimeOnly(tv.tv_sec, tv.tv_usec / 1000);
}
And then, I set the field this way:
msg.setField(FIX::UtcTimeOnlyField(FIX::FIELD::MDEntryTime, getTimeMillis(), true));

CCKeyDerivationPBKDF on iOS5

I'm trying to write a password encryption function into my app, following this article.
I wrote a function that runs the CCCalibratePBKDF function and outputs the number of rounds.
const uint32_t oneSecond = 1000;
uint rounds = CCCalibratePBKDF(kCCPBKDF2,
predictedPasswordLength,
predictedSaltLength,
kCCPRFHmacAlgSHA256,
kCCKeySizeAES128,
oneSecond);
This works perfectly, but when I try to implement the next part it all goes wrong.
I can start writing the CCKeyDerivationPBKDF function call and it auto-completes the function and all the parameters. As I go through filling it in all the parameters are also auto-completed.
- (NSData *)authenticationDataForPassword: (NSString *)password salt: (NSData *)salt rounds: (uint) rounds
{
const NSString *plainData = #"Fuzzy Aliens";
uint8_t key[kCCKeySizeAES128] = {0};
int keyDerivationResult = CCKeyDerivationPBKDF(kCCPBKDF2,
[password UTF8String],
[password lengthOfBytesUsingEncoding: NSUTF8StringEncoding],
[salt bytes],
[salt length],
kCCPRFHmacAlgSHA256,
rounds,
key,
kCCKeySizeAES128);
if (keyDerivationResult == kCCParamError) {
//you shouldn't get here with the parameters as above
return nil;
}
uint8_t hmac[CC_SHA256_DIGEST_LENGTH] = {0};
CCHmac(kCCHmacAlgSHA256,
key,
kCCKeySizeAES128,
[plainData UTF8String],
[plainData lengthOfBytesUsingEncoding: NSUTF8StringEncoding],
hmac);
NSData *hmacData = [NSData dataWithBytes: hmac length: CC_SHA256_DIGEST_LENGTH];
return hmacData;
}
But as soon as I hit ; it marks an error saying "No matching function for call to 'CCKeyDerivationPBKDF'" and it won't build or anything.
I've imported CommonCrypto/CommonKeyDerivation.h and CommonCrypto/CommonCryptor.h as both of these were necessary for the enum names.
First, make sure that you haven't done anything funny with your include path (in particular, I do not recommend #HachiEthan's solution, which just confuses things). In general, leave this alone, and specifically don't add things like /usr/include to it. Make sure you've added Security.framework to your link step. This is the usual cause of problems.
The biggest thing you want to be sure of is that you're getting the iOS 5 Security.framework (rather than some other version like the OS X 10.6 or iOS 4 versions). But my suspicion is that you have a problem with your build settings.
If you want to see a framework that does all of this for reference, take a look at RNCryptor.
Right, I've found the problem (and solution).
Because I was using ZXing I had to rename the .m file to .mm so it could run the C++ stuff in the ZXing library.
I don't know why but renaming the file this way broke the CCKeyDerivationPBKDF function.
I've now moved the crypto code into it's own class and left it as .m and all I need now is to include the two imports as I did in the original post.
I didn't have to include any frameworks or anything.

Incorrect NSStringEncoding value 0x0000 detected

I am using ASIHttpRequest library in iphone and when i try to do a GET request on the below URL i get this message and the request fails, please anyone if he/she has encountered this problem or know a possible solution to it please let me know. Secondly if i paste the link in the browser it works perfectly fine.
[URL]
http://www.rugsale.com/iphone/app/?type=product_list&params=id%3D334&ver=1.0&key=kaoud
[ERROR]
Incorrect NSStringEncoding value 0x0000 detected. Assuming NSStringEncodingASCII. Will stop this compatiblity mapping behavior in the near future.
Thx in advance
Regards
Syed Arsalan Pervez
www.saplogix.net
This notably happens if you call [asiHttpRequest getResponseString] before a valid response has been returned and the request encoding has been set (i.e. couldn't connect to server).
The easiest way to workaround this warning is by editing the ASIHTTPRequest class, remove the #synthesize responseEncoding and adding a simple custom getter/setter so you can return the default encoding if the response encoding isn't set:
- (NSStringEncoding) responseEncoding
{
return responseEncoding || self.defaultResponseEncoding;
}
- (void) setResponseEncoding:(NSStringEncoding)_responseEncoding
{
responseEncoding = _responseEncoding;
}
There's also a more specific workaround for the getResponseString method, which I think is the only place that uses the encoding without checking for a value - since the encoding should be set for any non-zero length response:
- (NSString *)responseString
{
NSData *data = [self responseData];
if (!data) {
return nil;
}
// --- INSERT THIS BLOCK ---
// If the 'data' is present but empty, return a simple empty string
if (data.length == 0) {
return #"";
}
//assert(self.responseEncoding); // if you're into runtime asserts, uncomment this
// --- END OF BLOCK TO INSERT ---
return [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[self responseEncoding]] autorelease];
}
For me the problem was timeout wasn't enough. So it returns an empty response. So it gives this error because you'r trying to do something with an empty response. I increased the time for it. So it solved my problem.[request setTimeOutSeconds:200];
this is just saying that the encoding was nil, when a value was expected. Try doing a search for "encoding:nil" or "encoding:NULL" in your source code and replace it with a valid encoding, for example NSStringEncodingASCII...
This looks like something that's cropped up in the 4.2 SDK. I didn't see this until I updated my development environment myself.
All-Seeing Interactive will probably have to update the code to ensure compatibility.
The error is where you are creating a string. You don't supply an encoding which is required.