Randomisation Services iOS (Random bytes conversion to NSString) - ios

I am trying to generate a random bytes received from the SecRandomCopyBytes to a Random String.
uint8_t resultBytes[10];
SecRandomCopyBytes(kSecRandomDefault, 10, resultBytes);
The resultBytes is shown below.
(uint8_t [10]) resultBytes = ([0] = '\xf5', [1] = '[', [2] =
'\x0e', [3] = '\xb0', [4] = '\xaf', [5] = '|',
[6] = '\x13', [7] = '\xfb', [8] = 'r', [9] = '\xb8')
If I convert to NSString with ASCII encoding it works but does not work with UTF encoding.
NSLog(#"Random Number Results=<%#>",[[NSString alloc] initWithBytes:resultBytes length:10 encoding:NSASCIIStringEncoding]);
Number Results =<õ[°¯|ûr¸>
Is the output string correct or I am doing something wrong?

Related

Python3 Error ValueError: invalid literal for int() with base 10: '1.8'

im trying to make a calculation script with python but im running into this problem:
ValueError: invalid literal for int() with base 10: '1.8'
Im trying to ask a user for an input that is in numbers like 2000 but when the input contains a ',' or '.' it will output that error.
This is the code:
Buiten = int(input("Buiten: "))
Binnen = int(input("Binnen: "))
DikteF = int(input("Dikte: "))
Sortle = int(input("Soortelijk gewicht: "))
inputs = Buiten,Binnen,DikteF,Sortle
Pi = 3.14
R1 = Buiten / 2
R2 = Binnen / 2
UD1 = Pi*R1/1000*R1/1000
UD2 = Pi*R2/1000*R2/1000
Tpv = UD1 - UD2
Ltr = Tpv * DikteF
Srt = Ltr * Sortle
print("")
print("Uitwendige Diameter = ",(round(UD1, 3)),"m²")
print("Inwendige Diameter = ",(round(UD2, 3)),"m²")
print("Product Oppervlak = ",(round(Tpv, 3)), "m²")
print("")
print("Inhoud in L = ",(round(Ltr, 3)),"Liters")
print("Totaal gewicht = ",(round(Srt, 3), "Kilos" ))
print("")
os.system("pause")
while True:
try:
Bereken()
except:
print("Fout: Gebruik alleen cijfers.")
time.sleep(4)
Bereken()
Python returns an error since it is expecting an integer value as string to convert to integer. However, when passing decimals, the attempt to convert to integer fails since they are not integers.
if you are going to insert either integers or decimals, use float
Buiten = float(input("Buiten: "))
Binnen = float(input("Binnen: "))
DikteF = float(input("Dikte: "))
Sortle = float(input("Soortelijk gewicht: "))

Showing the latest value in a line chart with Aspose.Words Net

Following the Working with Charts tutorial, I've been trying to show the latest value in a chart line series for the current or previous year. I receive two matrices as shown below in which their Count property is always either between one and two. As said, the matrix of DateTime may have one or two lists having each at least 365 days. The second matrix has the corresponding double for each day.
List<List<DateTime>> AxisX, List<List<double>> AxisY, List<string> seriesName
shape = (Shape)doc.Range.Bookmarks[bookmarkName.GetDescription()].BookmarkStart.NextSibling;
Chart chart = shape.Chart;
chart.Legend.Position = LegendPosition.TopRight;
chart.Legend.Overlay = true;
chart.Series.Clear();
chart.AxisX.CategoryType = AxisCategoryType.Time;
chart.AxisX.BaseTimeUnit = AxisTimeUnit.Days;
chart.AxisX.MajorUnitScale = AxisTimeUnit.Months;
chart.AxisX.MajorUnit = 1;
chart.AxisX.TickLabelAlignment = ParagraphAlignment.Center;
if (eixoX.Any() && eixoX.ElementAt(0).Any())
{
chart.AxisX.Scaling.Maximum = new AxisBound(eixoX.ElementAt(0).Last());
chart.AxisX.Scaling.Minimum = new AxisBound(eixoX.ElementAt(0).First());
}
Until now I was able to show the latest value in the chart if the AxisY[1] has continuous values like this:
chart.AxisX.NumberFormat.FormatCode = "mmm";
eixosY.ForEach((eixoY, i) =>
{
if (eixoX.ElementAt(i).Any() && eixoY.Any())
{
var x = eixoX.ElementAt(i).ToArray();
var y = eixoY.ToArray();
var s = nomeSeries.ElementAt(i);
chart.Series.Add(s, x, y);
}
});
And then I iterate through AxisY[1] and get the latest value as follows:
var array = chart.Series.Count - 1;
var serie = eixosY[array].ToArray();
var last = 0;
for (int i = serie.Length - 1; i >= 0; i--)
{
if (Double.IsNaN(serie[i])) continue;
last = i;
break;
}
var labels = chart.Series[array].DataLabels;
ChartDataLabel l = labels.Add(last);
l.ShowValue = true;
That produces the following result as expected:
Now the problem. When AxisY[1] is non-continuous I can't get the same result. Instead, I got something like this:
I just can't show the tuple X and Y value as in the previous image. I wanted the chart to show the value 57.1 since it is the latest value for the data in AxisY[1]:
- y {double[366]} double[]
[0] 29.9338 double
[1] 29.5862 double
[2] NaN double
[3] NaN double
[4] NaN double
[5] NaN double
[6] NaN double
[7] NaN double
[8] NaN double
[9] NaN double
[10] NaN double
[11] NaN double
[12] NaN double
[13] NaN double
[14] NaN double
[15] NaN double
[16] NaN double
[17] NaN double
[18] NaN double
[19] NaN double
[20] NaN double
[21] NaN double
[22] NaN double
[23] NaN double
[24] NaN double
[25] NaN double
[26] NaN double
[27] NaN double
[28] NaN double
[29] NaN double
[30] NaN double
[31] NaN double
[32] NaN double
[33] NaN double
[34] 23.282 double
[35] NaN double
[36] NaN double
[37] NaN double
.
.
.
[147] NaN double
[148] 16.5327 double
.
.
.
.
[254] 57.1 double
Any help will be much appreciated. Thanks in advance.
In your case you do not need to use DataLabels.Add method, since it is obsolete. It is enough to set ChartSeries.HasDataLabels property to true and then access the required DataLabel by index. I created a simple code example where X values has NaN values in the middle and at the end. The data label is displayed correctly as seen on the screenshot.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Line, 500, 300);
Chart chart = shape.Chart;
chart.Series.Clear();
double[] xValues = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
double[] yValues = new double[] { 1, 2, double.NaN, double.NaN, 5, 6, 7, 8, 9, 10, 11, double.NaN };
ChartSeries series = chart.Series.Add("Test", xValues, yValues);
// Determine the last no NaN value index.
int last = yValues.Length - 1;
while (double.IsNaN(yValues[last]) && last > 0)
last--;
// Enable data labels.
series.HasDataLabels = true;
// Display the value of the last not NaN value.
series.DataLabels[last].ShowValue = true;
doc.Save(#"C:\Temp\out.docx");

Converting C unsigned char array with null-terminating char in middle to Objective-C NSString

In an iOS app, I'm using a 3rd party SDK written in C.
As a result of an SDK method call I receive an unsigned char array (see example below).
I need to convert this array to an Objective-C String (NSString) as to save it and later on convert it back to C to pass it as a parameter to another SDK method.
I've seen multiple ways to convert a C string to Objective-C.
NSString *example1 = [NSString stringWithFormat:#"%s", myArray]; // "8oFDO{c."rägÕªö"
NSString *example2 = [[NSString alloc] initWithBytes:myArray length:sizeof(myArray) encoding:NSASCIIStringEncoding]; // "8oFDO{c."rägÕªö"
...
But none of them seems to allows \0 (null-terminating character) in the middle (see [29] in the example below).
Example unsigned char array response:
{
[0] = '8'
[1] = '\b'
[2] = '\x01'
[3] = 'o'
[4] = '\x01'
[5] = '\x03'
[6] = 'F'
[7] = 'D'
[8] = 'O'
[9] = '\x02'
[10] = '\x10'
[11] = '\x0e'
[12] = '{'
[13] = '\x8d'
[14] = 'c'
[15] = '.'
[16] = '\x19'
[17] = '"'
[18] = 'r'
[19] = '\xe4'
[20] = 'g'
[21] = '\x18'
[22] = '\xd5'
[23] = '\xaa'
[24] = '\xf6'
[25] = '\x95'
[26] = '\x18'
[27] = '\x03'
[28] = '\x01'
[29] = '\0'
[30] = '\x04'
[31] = '\x01'
[32] = '\x05'
[33] = '\x05'
[34] = '\x01'
[35] = '\x05'
[36] = '\x06'
[37] = '\x01'
[38] = '\x01'
...
}
How can I convert from C to Objective-C and then back from Objective-C to C?
This unsigned char array doesn't sims to be a string at all. You should handle it as if it is void * and use NSData to store such arguments.

iOS: wrong value for characteristic with low energy bluetooth device?

I have a low energy bluetooth device which picks up signal comparable to a heart-monitoring device. This data is plotted in real-time on a separate plotting screen.
My issue is that I think the values I'm getting from the hardware is wrong.
In my method didUpdateValueForCharacteristic a few lines of relevant info is:
case TI_KEYFOB_ACCEL_X_UUID: // x value characteristic
{
UInt8 xval[20];
[characteristic.value getBytes:&xval length:TI_KEYFOB_ACCEL_READ_LEN];
I then proceed to do some calculations on each value in the array which should each be a byte. Through each iteration (I think 256 Hz) I get 20 bytes of data and this gets pushed into an array of size 20.
These are the values from the characteristic.value that I'm getting for all 20 bytes:
[0] UInt8 '\x9b'
[1] UInt8 '\xe1'
[2] UInt8 '\xba'
[3] UInt8 '9'
[4] UInt8 '\x01'
[5] UInt8 '\0'
[6] UInt8 '\0'
[7] UInt8 '\0'
[8] UInt8 '\x01'
[9] UInt8 '\0'
[10] UInt8 '\0'
[11] UInt8 '\0'
[12] UInt8 '\b'
[13] UInt8 'M'
[14] UInt8 '\xd5'
[15] UInt8 '''
[16] UInt8 '\x1f'
[17] UInt8 '\xe6'
[18] UInt8 '\xbb'
[19] UInt8 '9'
I'm not sure what these values are actually representing. It looks like part is binary and part is maybe hexidecimal values of addresses.
All I know is that I can multiply these values by other integers and somehow get a constant string of the same numbers.
1,0,0,0,1,0,0,0,8,125,214,39,31,230,187,57,49,225,186,57
But this isn't the value that I want. I'm kind of lost, I've tried switching the characteristic.value to characteristic.valueHandle and other things, but I think it should be importing the data into the portion of value so the way it's written should be good.
I've also tried changing the TI_KEYFOB_ACCEL_X_UUID to other characteristics like ...Y_UUID but I'm pretty sure it should be obtaining the relevant data from X_UUID.
Anyways, any help at all or pointing me in the right direction would help.
Looking around, I am seeing that TI_KEYFOB_ACCEL_READ_LEN should only by 1.
case TI_KEYFOB_ACCEL_X_UUID: {
char xval;
[characteristic.value getBytes:&xval length:TI_KEYFOB_ACCEL_READ_LEN];
float x = (float)xval;
...

IOS:Convert string to hexadecimal array

i have string representing data .i need to convert those data to the hex array .By using the hex array data i can pass it to the CRC for writing to the peripheral
My string data is like this
NSString *stringsdata=#"helloworld1234567812345q";
i need to convert to hex format array like
{0x0h,0x0e............0x0q}.
so by using this array i can keep the data in the crc and write it to the peripheral data as
Byte comm[24];
comm[0]=0x01;
comm[1]=0x30;
comm[2]=0x62;
comm[3]=0x00;................
have tried with many possible solutions but not luck.can any body help will be greatly appreciated.
A. The hexadecimal format is simply another representation of the same data.
B. You do not convert them into hex array. Every character has a number. For example in ASCII and UTF-8 the A has the number 65 (decimal representation). This is 0x41 in hex representation.
'A' (ASCII) == 65 == 0x41.
A hex number has the the digits 0-9, a-f, wherein a has the value of 10, b the value of 11 … It is converter into decimal representation by multiplying the upper digit by 16 and adding the lower digit. (0x41: 4 x 16 + 1 = 65.)
Please read and understand this: http://en.wikipedia.org/wiki/Hexadecimal
C. To convert a string into its number, you have to know, which code you want to apply. Probably you want to use UTF-8.
NSString *text = #"helloworld123123989128739";
NSUInteger length = [text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
char data[length];
[text getCString:data maxLength:length usingEncoding:NSUTF8StringEncoding];
// Here we go
Byte[] class is an array of characters. I mean you can only set one character at it's index.
If we have
Byte comm[24]; then comm[0]=0x01; is looks like confusing here because it only saves one character.
And the statement will be like comm[0]='x';.
Below code will creates Byte[] from given string.
NSString *stringsdata=#"helloworld1234567812345q";
CFStringRef cfString = (__bridge CFStringRef)stringsdata;
char *array = charArrayFromCFStringRef(cfString);
size_t length= strlen(array);
Byte comm[24];
for (int i = 0; i < length; i++) {
comm[i] = array[i];
}
Conversion function:
char * charArrayFromCFStringRef(CFStringRef stringRef) {
if (stringRef == NULL) {
return NULL;
}
CFIndex length = CFStringGetLength(stringRef);
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
char *buffer = (char *)malloc(maxSize);
if (CFStringGetCString(stringRef, buffer, maxSize, kCFStringEncodingUTF8)) {
return buffer;
}
return NULL;
}
OutPut:
Printing description of comm:
(Byte [24]) comm = {
[0] = 'h'
[1] = 'e'
[2] = 'l'
[3] = 'l'
[4] = 'o'
[5] = 'w'
[6] = 'o'
[7] = 'r'
[8] = 'l'
[9] = 'd'
[10] = '1'
[11] = '2'
[12] = '3'
[13] = '4'
[14] = '5'
[15] = '6'
[16] = '7'
[17] = '8'
[18] = '1'
[19] = '2'
[20] = '3'
[21] = '4'
[22] = '5'
[23] = 'q'
}
The thing here is if you still convert any character from Byte[] then you can only save one character at any index.
Because for above characters it's hex value is more than one character and you can only save one character in Byte[].
I suggest to use NSArray to save each character's hex value in NSString format.

Resources