Skip to content

Commit

Permalink
Merge pull request #63 from hyperledger/fix/string-to-hex
Browse files Browse the repository at this point in the history
Fix wrong data from hex string converting
  • Loading branch information
MukhinAlexey authored May 12, 2018
2 parents 289ec65 + fd86e3a commit 64e6327
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
5 changes: 0 additions & 5 deletions SwiftyIroha/ObjC++/ModelCrypto/IRModelCrypto.mm
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ - (KeypairObjC*) generateFromExistingKeypair:(IRKeypair*) keypair {
-(string)getStringCppWithString:(NSString*)stringObjC {
string stringCpp = string([stringObjC UTF8String],
[stringObjC lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
cout << stringCpp;
cout << "\n";
cout << stringCpp.length();
cout << "\n";
cout << "\n";
return stringCpp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,18 @@ -(string)getStringCppFromStringObjC:(NSString*)stringObjC {
}

- (NSData *)dataFromHexString:(NSString *) string {
if ([string length] % 2 == 1){
string = [@"0" stringByAppendingString:string];
[string stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData* dataValue = [[NSMutableData alloc] init];
unsigned char byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [string length]/2; i++) {
byte_chars[0] = [string characterAtIndex:i*2];
byte_chars[1] = [string characterAtIndex:i*2+1];
byte = strtol(byte_chars, NULL, 16);
[dataValue appendBytes:&byte length:1];
}
const char *chars = [string UTF8String];
int i = 0, len = (int)[string length];

NSMutableData *data = [NSMutableData dataWithCapacity:len / 2];
char byteChars[3] = {'\0','\0','\0'};
unsigned long wholeByte;

while (i < len) {
byteChars[0] = chars[i++];
byteChars[1] = chars[i++];
wholeByte = strtoul(byteChars, NULL, 16);
[data appendBytes:&wholeByte length:1];
}
return data;
return dataValue;
}

@end

0 comments on commit 64e6327

Please sign in to comment.