What does subtracting by char 0 do? - c#

On this page, the sample code for C# contains the following code, where cardNumber is a string:
cardNumber.Select(c => c - '0').ToArray()
Now, what this appears to do is subtract 0 from each char in cardNumber, returning each as ints, then put them into an array. My questions, specifically about the c - '0' part of this code are:
1) Why does it do this? What is the underlying behavior/explanation?
2) Why would this be done instead of using Int32.Parse?
Note: Here is a similar question in C, this question may have the same/similar explanation.

It's subtracting unicode code points. So if c was something like 9 the operation is '9' - '0'. The code point for 9 is 57 and 0 is 48, so it will perform 57 - 48 which results in 9.
The only reason I can think of doing this over an int.Parse would be speed - this is an arithmetic operation which is much faster than whatever int.Parse does.

What Dave Zych points out in his answer is absolutely correct. This converts a string into an array of digits.
You often will see things like this done on embedded systems where parsing an int is an expensive operation. There is one significant flaw with this approach. (Other than that it's not immediately obvious to the uninitiated what it does.)
Consider what happens with the following input string:
اختبار
The resulting array comes out to
{ 1527, 1534, 1530, 1528, 1527, 1537 }
The failure when using this method is silent because اختبار is as valid a string as 123456. Using int.Parse, while it may be slower, will throw a FormatException to let you know that something went wrong.
While it is certainly acceptable in some cases to use the c - '0' method, just be sure when using it that you understand the risks, and make sure to test edge cases.
(For anyone who's interested the Arabic is 'test' translated using Google Translate)

Each character has a numerical representation so it can be stored in memory, and the c - '0' operation is simply subtracting the number that represents '0' from the number that represents the character held by c.
Hence, if c holds '5' the result of the operation will be the number 5.
Here is quick refrence to the ASCII printable code chart, to which Unicode is backward compatible:
Binary Oct Dec Hex Glyph
010 0000 040 32 20 (space)
010 0001 041 33 21 !
010 0010 042 34 22 "
010 0011 043 35 23 #
010 0100 044 36 24 $
010 0101 045 37 25 %
010 0110 046 38 26 &
010 0111 047 39 27 '
010 1000 050 40 28 (
010 1001 051 41 29 )
010 1010 052 42 2A *
010 1011 053 43 2B +
010 1100 054 44 2C ,
010 1101 055 45 2D -
010 1110 056 46 2E .
010 1111 057 47 2F /
011 0000 060 48 30 0
011 0001 061 49 31 1
011 0010 062 50 32 2
011 0011 063 51 33 3
011 0100 064 52 34 4
011 0101 065 53 35 5
011 0110 066 54 36 6
011 0111 067 55 37 7
011 1000 070 56 38 8
011 1001 071 57 39 9
011 1010 072 58 3A :
011 1011 073 59 3B ;
011 1100 074 60 3C <
011 1101 075 61 3D =
011 1110 076 62 3E >
011 1111 077 63 3F ?
100 0000 100 64 40 #
100 0001 101 65 41 A
100 0010 102 66 42 B
100 0011 103 67 43 C
100 0100 104 68 44 D
100 0101 105 69 45 E
100 0110 106 70 46 F
100 0111 107 71 47 G
100 1000 110 72 48 H
100 1001 111 73 49 I
100 1010 112 74 4A J
100 1011 113 75 4B K
100 1100 114 76 4C L
100 1101 115 77 4D M
100 1110 116 78 4E N
100 1111 117 79 4F O
101 0000 120 80 50 P
101 0001 121 81 51 Q
101 0010 122 82 52 R
101 0011 123 83 53 S
101 0100 124 84 54 T
101 0101 125 85 55 U
101 0110 126 86 56 V
101 0111 127 87 57 W
101 1000 130 88 58 X
101 1001 131 89 59 Y
101 1010 132 90 5A Z
101 1011 133 91 5B [
101 1100 134 92 5C \
101 1101 135 93 5D ]
101 1110 136 94 5E ^
101 1111 137 95 5F _
110 0000 140 96 60 `
110 0001 141 97 61 a
110 0010 142 98 62 b
110 0011 143 99 63 c
110 0100 144 100 64 d
110 0101 145 101 65 e
110 0110 146 102 66 f
110 0111 147 103 67 g
110 1000 150 104 68 h
110 1001 151 105 69 i
110 1010 152 106 6A j
110 1011 153 107 6B k
110 1100 154 108 6C l
110 1101 155 109 6D m
110 1110 156 110 6E n
110 1111 157 111 6F o
111 0000 160 112 70 p
111 0001 161 113 71 q
111 0010 162 114 72 r
111 0011 163 115 73 s
111 0100 164 116 74 t
111 0101 165 117 75 u
111 0110 166 118 76 v
111 0111 167 119 77 w
111 1000 170 120 78 x
111 1001 171 121 79 y
111 1010 172 122 7A z
111 1011 173 123 7B {
111 1100 174 124 7C |
111 1101 175 125 7D }
111 1110 176 126 7E ~

Related

Same Regex over same content returns 3 different results discriminated by environment

Here is the piece of code
var content = #"Script 1 Line 1;
GO
Script 1 Line 2;
GO
";
var regex = new Regex("^GO$", RegexOptions.Multiline);
MatchCollection mc = regex.Matches(content);
Debug.WriteLine(mc.Count);
When I run this code in "dotnetfiddle.com" in Roslyn or Framework 4.7.2 - same result - 2 matches.
When I run this code in the Unit Test project, directly in TestMethod in Framework 4.7.2 - 0 matches
When I run this code in the class method in project compiled targeting netstandard2.0, - 1 match
This is a major headache I need to solve
Additional Test
var sb = new StringBuilder();
sb.AppendLine("Script 1 Line 1;");
sb.AppendLine("GO");
sb.AppendLine("Script 1 Line 2;");
sb.AppendLine("GO");
sb.AppendLine();
var content = sb.ToString();
Console.WriteLine(content);
// ^^^ changed string creation ^^^
var regex = new Regex("^GO$", RegexOptions.Multiline);
MatchCollection mc = regex.Matches(content);
Console.WriteLine(mc.Count);
With this ^^^, even "dotnetfiddle.com" returns 0 matches
I am still not getting the picture here but obviously something about line breaks in different editors. Then why string builder is doing this?
In MSDN(https://learn.microsoft.com/en-us/dotnet/standard/base-types/anchors-in-regular-expressions?redirectedfrom=MSDN), it states:
If you use $ with the RegexOptions.Multiline option, the match can also occur at the end of a line. Note that $ matches \n but does not match \r\n (the combination of carriage return and newline characters, or CR/LF). To match the CR/LF character combination, include \r?$ in the regular expression pattern.
When I printed each byte of content in visual studio, the result was
83 99 114 105 112 116 32 49 32 76 105 110 101 32 49 59 13 10 71 79 13 10 83 99 114 105 112 116 32 49 32 76 105 110 101 32 50 59 13 10 71 79 13 10 with carriage return. It does not match GO.
while in dotnetfiddle.com and python, the result was
83 99 114 105 112 116 32 49 32 76 105 110 101 32 49 59 10 71 79 10 83 99 114 105 112 116 32 49 32 76 105 110 101 32 50 59 10 71 79 10 without carriage return. It matches GO.
When I used StringBuilder in dotnetfiddle, the result was
83 99 114 105 112 116 32 49 32 76 105 110 101 32 49 59 13 10 71 79 13 10 83 99 114 105 112 116 32 49 32 76 105 110 101 32 50 59 13 10 71 79 13 10 13 10 with carriage return. It does not match GO.
So changing ^GO$ to ^GO\r?$ will make it work.

Getting different LRC (Long Range Checksum) result

I'm trying to send a request to a TCP/IP terminal. My LRC function as below:
public byte GetLRC(byte[] bArr)
{
byte LRC = 0x00;
foreach (byte b in bArr)
{
LRC ^= b;
}
return LRC;
}
But the problem is my LRC and their sample LRC are totally different. How can they calculate this?
My message data request is almost the same as their except the LRC.
Sample data:
Hex: 0 66 30 30 30 30 30 30 30 30 32 34 31 30 33 30 30 31 30 1C 54 32 0 2 30 31 1C 34 33 0 1 30 1C 34 30 0 12 30 30 30 30 30 30 30 30 30 30 30 31 1C 34 32 0 12 30 30 30 30 30 30 30 30 30 30 30 30 1C 79
Bytes:
{byte[68]}
[0]: 0
[1]: 102
[2]: 48
[3]: 48
[4]: 48
[5]: 48
[6]: 48
[7]: 48
[8]: 48
[9]: 48
[10]: 50
[11]: 52
[12]: 49
[13]: 48
[14]: 51
[15]: 48
[16]: 48
[17]: 49
[18]: 48
[19]: 28
[20]: 84
[21]: 50
[22]: 0
[23]: 2
[24]: 48
[25]: 49
[26]: 28
[27]: 52
[28]: 51
[29]: 0
[30]: 1
[31]: 48
[32]: 28
[33]: 52
[34]: 48
[35]: 0
[36]: 18
[37]: 48
[38]: 48
[39]: 48
[40]: 48
[41]: 48
[42]: 48
[43]: 48
[44]: 48
[45]: 48
[46]: 48
[47]: 48
[48]: 49
[49]: 28
[50]: 52
[51]: 50
[52]: 0
[53]: 18
[54]: 48
[55]: 48
[56]: 48
[57]: 48
[58]: 48
[59]: 48
[60]: 48
[61]: 48
[62]: 48
[63]: 48
[64]: 48
[65]: 48
[66]: 28
[67]: 121
My data:
0 66 30 30 30 30 30 30 30 30 32 34 31 30 33 30 30 31 30 1C 54 32 0 2 30 31 1C 34 33 0 1 30 1C 34 30 0 12 30 30 30 30 30 30 30 30 30 30 30 31 1C 34 32 0 12 30 30 30 30 30 30 30 30 30 30 30 30 1C 1F
{byte[68]}
[0]: 0
[1]: 102
[2]: 48
[3]: 48
[4]: 48
[5]: 48
[6]: 48
[7]: 48
[8]: 48
[9]: 48
[10]: 50
[11]: 52
[12]: 49
[13]: 48
[14]: 51
[15]: 48
[16]: 48
[17]: 49
[18]: 48
[19]: 28
[20]: 84
[21]: 50
[22]: 0
[23]: 2
[24]: 48
[25]: 49
[26]: 28
[27]: 52
[28]: 51
[29]: 0
[30]: 1
[31]: 48
[32]: 28
[33]: 52
[34]: 48
[35]: 0
[36]: 18
[37]: 48
[38]: 48
[39]: 48
[40]: 48
[41]: 48
[42]: 48
[43]: 48
[44]: 48
[45]: 48
[46]: 48
[47]: 48
[48]: 49
[49]: 28
[50]: 52
[51]: 50
[52]: 0
[53]: 18
[54]: 48
[55]: 48
[56]: 48
[57]: 48
[58]: 48
[59]: 48
[60]: 48
[61]: 48
[62]: 48
[63]: 48
[64]: 48
[65]: 48
[66]: 28
[67]: 31

Selection Sort trouble with indexes [duplicate]

This question already has an answer here:
Selection Sort trouble with indexes
(1 answer)
Closed 6 years ago.
Actually I'm dealing with CodeAbbey problem, so I don't want answer as code, but explenation about that, what I am doing wrong. http://www.codeabbey.com/index/task_view/selection-sort
My Selection Sort actually works without any problems, but I don't know why I do not get proper indexes (when sorting works!). I.e. for input data:5 1 3 6 2 4 7 9 8 0 I got it sorted to 0 1 2 3 4 5 6 7 8 9, as I wished.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SelectionSort
{
class Program
{
static void Main(string[] args)
{
int howMany = int.Parse(Console.ReadLine()); //length of array
List<int> Base = new List<int>(Array.ConvertAll(Console.ReadLine().Split(), int.Parse)); //input to array (i.e. 5 1 3 6 2 4 7 9 8 0 => { 5, 1, 3, 6, 2, 4, 7, 9, 8, 0 })
List<int> Output = new List<int>(); // list to store sorted array
string[] ans = new string[howMany]; // array for storing answers
int loops = Base.Count();
for(int i = 0; i != loops; i++)
{
int topID = 0, topValue = 0;
for(int j = 0; j != Base.Count(); j++)
{
if (j == 0)
{
topID = 0;
topValue = Base[0];
}
else
{
if(topValue < Base[j])
{
topValue = Base[j];
topID = j;
}
}
}
ans[i] = topID.ToString(); //after looping through array save topID to answer array
Output.Add(Base[topID]); //add topValue to output
Base.RemoveAt(topID); //remove topValue with index topID from list
}
//Output.Reverse(); // Writing on stdout
//foreach(var s in Output) // sorted array
//{ //
// Console.Write(s + " "); // It works without any problems
//} //
//Console.ReadLine(); //
foreach(var s in ans)
{
Console.Write(s + " "); // write on stdout stored indexes
}
Console.ReadLine();
}
}
}
I.e. for such a test data:
124
144 146 4 121 106 142 153 168 122 42 135 127 126 16 193 52 29 161 186 83 152 72 51 125 37 116 187 133 183 132 80 53 185 129 7 189 98 128 32 33 56 157 49 50 10 77 11 196 160 162 68 43 14 181 112 113 94 100 165 79 172 159 156 57 9 6 66 86 17 63 46 178 130 88 192 124 105 182 34 18 76 155 24 89 123 12 179 109 188 13 40 5 163 45 27 85 103 93 69 58 25 81 145 92 30 138 154 177 158 140 91 171 139 67 175 184 120 8 54 147 84 174 95 55
I got it sorted to:
4 5 6 7 8 9 10 11 12 13 14 16 17 18 24 25 27 29 30 32 33 34 37 40 42 43 45 46 49 50 51 52 53 54 55 56 57 58 63 66 67 68 69 72 76 77 79 80 81 83 84 85 86 88 89 91 92 93 94 95 98 100 103 105 106 109 112 113 116 120 121 122 123 124 125 126 127 128 129 130 132 133 135 138 139 140 142 144 145 146 147 152 153 154 155 156 157 158 159 160 161 162 163 165 168 171 172 174 175 177 178 179 181 182 183 184 185 186 187 188 189 192 193 196
and I got such an indexes:
47 14 72 34 84 25 17 29 107 25 69 46 76 63 94 100 105 52 96 7 49 76 41 15 39 47 86 33 46 61 82 6 15 87 1 75 0 3 76 77 75 5 16 16 46 18 20 5 5 11 42 48 3 1 64 10 26 25 44 1 36 48 25 12 58 23 45 49 50 37 32 28 41 49 5 43 8 21 16 29 5 35 16 37 21 22 32 18 11 34 33 7 3 4 9 8 15 22 10 1 18 3 12 5 4 17 2 14 14 10 9 8 1 4 7 6 3 2 2 4 1 1 1 0
when I was supposed to get these:
47 14 74 35 88 26 18 32 115 28 77 53 86 71 107 28 74 60 86 7 58 92 49 17 48 61 28 41 62 81 60 6 20 88 1 6 0 5 74 53 5 10 27 29 72 33 37 11 12 23 12 53 8 3 32 25 55 54 0 4 11 41 57 36 14 56 28 56 8 5 12 8 41 35 19 19 30 14 45 29 21 28 35 37 5 32 19 11 19 30 26 31 15 22 21 28 3 11 19 9 6 9 11 5 15 7 16 3 3 10 12 3 3 8 1 6 6 3 4 3 1 0 1
What am I doing wrong?
Greetings
You should be swapping topValue with the last unsorted element instead of removing it. I guess you could remove it after swapping if you wanted. But if you don't remove it and you properly ignore the sorted elements, the original collection will be sorted at the end, so you won't have to make any Add() or Reverse() calls or create a new collection.

Listing Elements of Array / List<String>

I found some helpful post, but need clarification. I have a matrix with a list of numbers in it, separated by a space between each number.
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
I have read that data into my program with the following code:
string[] listData = File.ReadAllLines("\\Folder1\\GridDat.txt");
List<string[]> partialData = new List<string[]>();
// Read all lines and put them into this array
foreach (string s in listData)
{
partialData.Add(s.Split(' '));
}
fileOut.WriteLine("{0}", partialData);
I believe I have read this into an array or a List of strings, but not sure how access the data from here, so I can print it out to a file.
I'm not certain how you want it formatted in your output file, but this will print them one number on each line:
var lines = System.IO.File.ReadAllLines("C:\\temp\\numbers.txt");
var lineArray = lines.SelectMany(x=>x.Split(' '));
System.IO.File.WriteAllLines("C:\\temp\\txt.txt", lineArray);
SelectMany
This will allow you to pull in the data, manipulate the numbers and then put it back in the original format:
var lines = System.IO.File.ReadAllLines("C:\\temp\\numbers.txt");
var lineArray = lines.Select(x =>
{
var numbers = x.Split(' ');
//do stuff with individual numbers here.
return string.Join(" ", numbers);
}
);
System.IO.File.WriteAllLines("C:\\temp\\txt.txt", lineArray);
Select

month wise reports by date

Is it possible to get this report format in sql. I tried various ways but no luck..any light on this would help me a lot.. I can get name, tickets from table but how can date wise report in sql. I dont want use any reports
NAME 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 (date)
smrithi 10 20 34 45 55 55 66 77 33 44 55 56 44 66 77 88 55 22 33 11 44 99 77 88 (tickets)
XYZ 10 20 34 45 55 55 66 77 33 44 55 56 44 66 77 88 55 22 33 11 44 99 77 88
this is what I tried ..
SELECT *
FROM
( SELECT CAST(DAY(t_date_time_issued) AS VARCHAR(4)) AS SaleDay,
CAST(MONTH(t_date_time_issued) AS VARCHAR(4)) AS SaleYear
FROM dbo.tickets) as ts
PIVOT
(
count(t_reference)
FOR SaleDay IN ( [1],[2],[3],[4],[5],[6],[7],[8],[9],
[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],
[22],[23],[24] ) ) AS pvt
Try something like this:
SELECT Name, [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24]
FROM
(
SELECT Name,
CAST(DAY(t_date_time_issued) AS VARCHAR(4)) AS SaleDay,
t_reference
FROM dbo.tickets) AS ts
PIVOT
(
count(t_reference)
FOR SaleDay IN ( [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24] )
) AS pvt
http://sqlfiddle.com/#!6/052ff/5

Categories

Resources