Strange result when converting Hex String to int - iphone

I coded the following method to convert a Hex String to a int:
-(long)intFromHexString:(NSString*) string
{
char tempChar;
int temp;
tempChar=[string characterAtIndex:[string length]-1];
temp=strtol(&tempChar, NULL, 16);
NSLog(#"***>%c = %i",tempChar,temp);
return temp;
}
Most of the time it works properly but sometimes it really gets into big trouble like this:
2012-02-10 01:09:28.516 GameView[7664:f803] ***>7 = 7
2012-02-10 01:09:28.517 GameView[7664:f803] ***>7 = 7
2012-02-10 01:09:28.518 GameView[7664:f803] ***>D = 13
2012-02-10 01:09:28.519 GameView[7664:f803] ***>5 = 5
2012-02-10 01:09:28.520 GameView[7664:f803] ***>5 = 5
2012-02-10 01:09:28.520 GameView[7664:f803] ***>D = 13
2012-02-10 01:09:28.521 GameView[7664:f803] ***>4 = 4
2012-02-10 01:09:28.522 GameView[7664:f803] ***>4 = 4
2012-02-10 01:09:28.522 GameView[7664:f803] ***>5 = 5
2012-02-10 01:09:28.523 GameView[7664:f803] ***>4 = 1033 <------this
2012-02-10 01:09:28.524 GameView[7664:f803] ***>C = 12
2012-02-10 01:09:28.524 GameView[7664:f803] ***>B = 11
2012-02-10 01:09:28.525 GameView[7664:f803] ***>3 = 3
2012-02-10 01:09:28.526 GameView[7664:f803] ***>3 = 48 <------this
2012-02-10 01:09:28.527 GameView[7664:f803] ***>B = 11
Can anyone tell me what's wrong with my code?

You are passing a pointer to a single character into strtol(), rather than a NUL-terminated string, so strtol() is sometimes reading beyond the character you gave it. (For instance, "1033" is the result of it finding "409", rather than just "4".)
Fix:
-(long)intFromHexString:(NSString*) string
{
char tempChar[2];
int temp;
tempChar[0]=[string characterAtIndex:[string length]-1];
tempChar[1] = 0;
temp=strtol(tempChar, NULL, 16);
NSLog(#"***>%c = %i",tempChar[0],temp);
return temp;
}

Related

How to loop over 3 elements of an array?

I am trying to create a loop that selects 3 rows from an array at a time and does some calculation later.
For example:
array = [2 3 4; 4 5 6; 7 8 9; 10 11 23; 23 56 78; 67 55 89; 90 87 32]
So in the first loop it should select [2 3 4; 4 5 6; 7 8 9]
and in the second loop [10 11 23; 23 56 78; 67 55 89].
I am struggling to make this possible.
Hope the code Help:
clear, clc
% Array
Arr = [2 3 4; 4 5 6; 7 8 9; 10 11 23; 23 56 78; 67 55 89; 90 87 32];
% Define The Length to Prevent the Error of Iteration in For Loop
if mod(size(Arr,1),3) ==0
LenArr = size(Arr,1); % Length Examined
else
LenArr = size(Arr,1) - mod(size(Arr,1),3);
end
Counter = 1;
for i = 1:3: LenArr
Iter = Arr(i:i+2, :); % Here the Answer of the Question
% Disply Results ----------
fprintf('Iteration %d = \n',Counter)
disp(Iter)
Counter = Counter+1;
%--------------------------
end
Results:
Iteration 1 =
2 3 4
4 5 6
7 8 9
Iteration 2 =
10 11 23
23 56 78
67 55 89
you can do this like below code
your algorithms can be applied to selectBlockLocations variable.
array = [2 3 4; 4 5 6; 7 8 9; 10 11 23; 23 56 78; 67 55 89; 90 87 32];
number_of_loop = floor(length(array)/3);
for i=0:number_of_loop-1
selectBlockLocations = array(i*3+1:i*3+3, 1:end);
disp(selectBlockLocations)
end
result:
2 3 4
4 5 6
7 8 9
10 11 23
23 56 78
67 55 89
Have a good time!

SQL Server LEN function reports wrong result

Let's say we have the following casting of an int number into binary value i.e
cast(120 as binary(8)) or any other int number into binary(8).
What we normally expect from len(cast(120 as binary(8))) = 8 and this is true unless we try with number 32 where select len(cast(32 as binary(8))) returns 7 !
Is this a bug of SQL Server?
Not a bug, it's how LEN works. LEN:
Returns the number of characters of the specified string expression,
excluding trailing spaces.
The definition of "trailing space" seems to differ based the datatype. For binary values, a trailing space is when the binary representation "20". In the BOL entry for LEN there is a note that reads,
Use the LEN to return the number of characters encoded into a given
string expression, and DATALENGTH to return the size in bytes for a
given string expression. These outputs may differ depending on the
data type and type of encoding used in the [value]. For more
information on storage differences between different encoding types,
see Collation and Unicode Support.
With Binary the length (LEN) is reduced by 1 for binary values that end with 20, by 2 for values that end with 2020, etc. Again, it's treating that value like a trailing space. DATALENGTH resolves this. Note this SQL:
DECLARE
#string VARCHAR(100) = '1234567 ',
#binary BINARY(8) = 32;
SELECT [Type] = 'string', [Len] = LEN(#string), [Datalength] = DATALENGTH(#string)
UNION ALL
SELECT [Type] = 'binary(8)', [Len] = LEN(#binary), [Datalength] = DATALENGTH(#binary);
Returns:
Type Len Datalength
--------- ----------- -----------
string 7 8
binary(8) 7 8
Using my rangeAB function (here) I created this query:
SELECT
N = r.RN,
Binaryvalue = CAST(r.RN AS binary(8)),
[Len] = LEN(CAST(r.RN AS binary(8))),
[DataLength] = DATALENGTH(CAST(r.RN AS binary(8)))
FROM dbo.rangeAB(0,10000,1,0) AS r
WHERE LEN(CAST(r.RN AS binary(8))) <> 8
ORDER BY N;
Note these results:
N Binaryvalue Len DataLength
-------------------- ------------------ ----------- -----------
32 0x0000000000000020 7 8
288 0x0000000000000120 7 8
544 0x0000000000000220 7 8
800 0x0000000000000320 7 8
1056 0x0000000000000420 7 8
1312 0x0000000000000520 7 8
1568 0x0000000000000620 7 8
1824 0x0000000000000720 7 8
2080 0x0000000000000820 7 8
2336 0x0000000000000920 7 8
2592 0x0000000000000A20 7 8
2848 0x0000000000000B20 7 8
3104 0x0000000000000C20 7 8
3360 0x0000000000000D20 7 8
3616 0x0000000000000E20 7 8
3872 0x0000000000000F20 7 8
4128 0x0000000000001020 7 8
4384 0x0000000000001120 7 8
4640 0x0000000000001220 7 8
4896 0x0000000000001320 7 8
5152 0x0000000000001420 7 8
5408 0x0000000000001520 7 8
5664 0x0000000000001620 7 8
5920 0x0000000000001720 7 8
6176 0x0000000000001820 7 8
6432 0x0000000000001920 7 8
6688 0x0000000000001A20 7 8
6944 0x0000000000001B20 7 8
7200 0x0000000000001C20 7 8
7456 0x0000000000001D20 7 8
7712 0x0000000000001E20 7 8
7968 0x0000000000001F20 7 8
8224 0x0000000000002020 6 8
8480 0x0000000000002120 7 8
8736 0x0000000000002220 7 8
8992 0x0000000000002320 7 8
9248 0x0000000000002420 7 8
9504 0x0000000000002520 7 8
9760 0x0000000000002620 7 8
Note how the LEN of CAST(8224 AS binary(8) is 6; because 8224 ends with 2020 which is treated like two spaces:
8224 0x0000000000002020 6 8

while loop in my program does not fill the second row

I have 2 inner while loops. Code writes the results to first row on the matrix but when it gets to second row, it just passes the other rows and doesn't fill the columns of the rows. How to solve it? The code and output are like below:
while i <= m-1
i
while a <= m-1
a
den1 = sqrt(((xy{i,j}-xy{a+1,b})^2+(xy{i,j+1}-xy{a+1,b+1})^2 ));
dMat(i,a) = den1;
a = a+1;
end
i = i+1;
end
i = 1
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
a = 10
a = 11
a = 12
a = 13
a = 14
a = 15
a = 16
a = 17
a = 18
a = 19
a = 20
a = 21
a = 22
a = 23
a = 24
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
i = 11
i = 12
i = 13
i = 14
i = 15
i = 16
i = 17
i = 18
i = 19
i = 20
i = 21
i = 22
i = 23
i = 24
You have to restart a to a=1 for each iteration. Put it above the while a <= m-1

How can I select rows with specific column values from a matrix?

I have a matrix train3.
1 2 3 4 5 6 7
2 12 13 14 15 16 17
3 62 53 44 35 26 17
4 52 13 24 15 26 37
I want to select only those rows of whose 1st columns contain specific values (in my case 1 and 2).
I have tried the following,
>> train3
train3 =
1 2 3 4 5 6 7
2 12 13 14 15 16 17
3 62 53 44 35 26 17
4 52 13 24 15 26 37
>> ind1 = train3(:,1) == 1
ind1 =
1
0
0
0
>> ind2 = train3(:,1) == 2
ind2 =
0
1
0
0
>> mat1 = train3(ind1, :)
mat1 =
1 2 3 4 5 6 7
>> mat2 = train3(ind2, :)
mat2 =
2 12 13 14 15 16 17
>> mat3 = [mat1 ; mat2]
mat3 =
1 2 3 4 5 6 7
2 12 13 14 15 16 17
>>
Is there any better way to do this?
Presumably you are trying to get mat3 in a single step which you can do with:
mat3 = train3(train3(:,1)==1 | train3(:,1)==2,:)
A more general way to do this would be to use ismember to get all of the rows that match the values in a list:
train3 =[
1 2 3 4 5 6 7
2 12 13 14 15 16 17
3 62 53 44 35 26 17
4 52 13 24 15 26 37];
chooseList = [1 2];
colIndex = ismember(train3(:, 1), chooseList);
subset = train3(colIndex, :);
subset =
1 2 3 4 5 6 7
2 12 13 14 15 16 17

How to rearrange matrix column into block using for loop?

I am working on a project where i have used a image whose size is (512x512)then i have divided the whole image by 8 so that there will be 64x64 block then i have rearranged each 8x8 image patch into a single column and my new size is 64x4069.
Now i want to get back the original size i.e 512x512,please help me how to get back it using for loop instead of 'col2im'
a=imread('lena.png');
b=double(a);
[m,n] = size(b);
bl=8;
br=m/bl;
bc=n/bl;
out = zeros(size(reshape(b,bl*bl,[])));
count = 1;
for i = 1:br
for j= 1:bc
block = b((j-1)*bl + 1:(j-1)*bl + bl, (i-1)*bl + 1:(i-1)*bl + bl);
out(:,count) = block(:);
count = count + 1;
end
end
This is basically your script written backwards. Some assumptions have to be made, because not every rectangular matrix can arise from the process you described, and also because the dimensions of the original matrix cannot be uniquely determined from the set of blocks. So, I assume that the original matrix was a square one, and the blocks were squares as well.
By the way, in your code you use j in the formula for row indices and i for columns; I assumed this was a mistake, and amended it below.
out = kron((0:3)', 1:16); % for testing; you would have a 64x4096 matrix here
[m,n] = size(out);
osize = sqrt(n*m);
bl = sqrt(m);
br = osize/bl;
bc = br;
original = zeros(osize);
count = 1;
for i = 1:br
for j = 1:bc
block = zeros(bl);
block(:) = out(:,count);
original(1+(i-1)*bl : i*bl, 1+(j-1)*bl : j*bl) = block;
count = count + 1;
end
end
Input for testing:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48
Output:
0 2 0 4 0 6 0 8
1 3 2 6 3 9 4 12
0 10 0 12 0 14 0 16
5 15 6 18 7 21 8 24
0 18 0 20 0 22 0 24
9 27 10 30 11 33 12 36
0 26 0 28 0 30 0 32
13 39 14 42 15 45 16 48