I am trying to write a regular expression where only values from 1 thru 99 are allowed.
Valid values:
1
01
2
02
3
03
...
19
20
21
...
98
99
Everything else must throw an error like:
00
0
-1
1.0
1.
100
I currently have this:
^[1-9]([0-9]+$)
This is not working. It only accepts values from 10 and above.
This should do:
^(0[1-9]|[1-9][0-9]?)$
^(0?[1-9]|[1-9][0-9])$
first part covers 1 to 9 and the 0x
second part is for everything between 10 and 99
/^(0?[1-9]|[1-9][0-9])$/gm
Demo here.
Try this Regex:
^([1-9]|[1-9][0-9]|0[1-9])$/mg
Demo here Regex101
It uses the | operator which is equivalent to OR.
So basically this Regex is saying:
Is the number between 1 and 9?
OR
Is the first number between 1 and 9, and the second number between 0
and 9?
OR
Does the number begin with a 0 and end with a number between 1 and 9?
The flags on the end /mg just make it check each line for the demo, you may want to change or remove these.
Edit: For whatever reason I didn't see Kuba's answer. His is actually cleaner, but works in roughly the same way as this one should you have wanted an explanation.
This shoud work : ^(([0-9][1-9])|([1-9][0-9])|[1-9])$
Related
On the internet the rules says for XOR it becomes one if one value is 0 and another is 1. I got that point so:
0 1 1 1 0
0 1 1 0 1
0 0 0 1 1
What I don't get is how come when I add
23
12
31
How did it come up with 31?
What I suspect is the following:
23 is actually 2 followed with 3
12 is actually 1 followed with 2
In binary:
2 3 -> 10 11
1 2 -> 01 10
XOR gives you:
11 01 -> 3 1
Edit:
As suggested in comments, it could also be that you used OR instead of XOR:
10111 // 23
01100 // 12
11111 // 31
I have an MVC project where the user must fill a form which contain a 14 digits element.
I need to check if the user input is an integer of 14 digits (min 14 digits, max 14 digits) with the first digit 1 or 2.
Can you please help me with a regular expression.
I tried this for 14 digits : [RegularExpression(#"^\d{14}$")] but is not working, I mean It see that I must enter 14 digits but does not let me saved into database.
the property which I need to fill is:
[Required]
[RegularExpression(#"^\d{14}$")]
public int CNP { get; set; }
#"^[12]\d{13}$"
The [12] ensures only 1 or 2 appear at the beginning of the string. See character classes for more info.
I'm sort of new at this and I'm writing a small application to read data from a voltmeter. It's a RadioShack Digital Multimeter 46-range. The purpose of my program is to perform something automatically when it detects a certain voltage. I'm using C# and I'm already familiar with the SerialPort class.
My program runs and reads the data in from the voltmeter. However, the data is all unformatted/gibberish. The device does come with its own software that displays the voltage on the PC, however this doesn't help me since I need to grab the voltage from my own program. I just can't figure out how to translate this data into something useful.
For reference, I'm using the SerialPort.Read() method:
byte[] voltage = new byte[100];
_serialPort.Read(voltage, 0, 99);
It grabs the data and displays it as so:
16 0 30 0 6 198 30 6 126 254 30 0 30 16 0 30 0 6 198 30 6 126 254 30 0 30 16 0 3
0 0 6 198 30 6 126 254 30 0 30 16 0 30 0 6 198 30 6 126 254 30 0 30 16 0 30 0 6
198 30 6 126 254 30 0 30 24 0 30 0 6 198 30 6 126 254 30 0 30 16 0 30 0 254 30 6
126 252 30 0 6 0 30 0 254 30 6 126 254 30 0
The space separates each element of the array. If I use a char[] array instead of byte[], I get complete gibberish:
▲ ? ? ▲ ♠ ~ ? ▲ ♠ ▲ ? ? ▲ ♠ ~ ? ▲ ♠ ▲ ? ? ▲ ♠ ~ ? ▲ ♠
Using the .ReadExisting() method gives me:
▲ ?~?♠~?▲ ▲? ▲ ?~♠~?▲ ?↑ ▲ ??~♠~?▲ F? ▲ ??~♠~?▲ D? ▲ ??~♠~?▲ f?
.ReadLine() times out, so doesn't work. ReadByte() and ReadChar() just give me numbers similar to the Read() into array function.
I'm in way over my head as I've never done something like this, not really sure where else to turn.
It sounds like you're close, but you need to figure out the correct Encoding to use.
To get a string from an array of bytes, you need to know the Code Page being used. If it's not covered in the manual, and you can't find it via a google/bing/other search, then you will need to use trial and error.
To see how to use GetChars() to get a string from a byte array, see Decoder.GetChars Method
In the code sample, look at this line:
Decoder uniDecoder = Encoding.Unicode.GetDecoder();
That line is specifically stating that you are to use the Unicode code page to get the correct code page.
From there, you can use an override of the Encoding class to specify different Code Pages. This is documented here: Encoding Class
If the Encoding being used isn't one of the standards, you can use the Encoding(Int32) override in the Constructor of the Encoding class. A list of valid Code Page IDs can be found at Code Pages Supported by Windows
There are two district strategies for solving your communications problem.
Locate and refer to appropriate documentation and design\modify a program to implement the specification.
The following may be appropriate, but are not guaranteed to describe the particular model DVM that you have. Nonetheless, they MAY serve as a starting point.
note that the authors of these documents comment that the Respective models may be 'visually identical', but also comments that '"Open-source packages that reportedly worked on LINUX with earlier RS-232 models do not work with the 2200039"
http://forums.parallax.com/attachment.php?attachmentid=88160&d=1325568007
http://sigrok.org/wiki/RadioShack_22-812
http://code.google.com/p/rs22812/
Try to reverse engineer the protocol. if you can read the data in a loop and collect the results, a good approach to reverse engineering a protocol, is to apply various representative signals to the DVM. You can use a short-circuit resistance measurements, various stable voltage measurements, etc.
The technique I suggest is most valuable is to use an automated variable signal generator. In this way, by analyzing the patterns of the data, you should be more readily be able to identify which points represent the raw data and which points represent stable descriptive data, like the unit of measurements, mode of operation, etc.
Some digital multimeters use 7 bit data transfer. You should set serial communication port to 7 data bits instead of standard 8 data bits.
I modified and merged a couple of older open source C programs together on linux in order to read the data values from the radio shack meter whose part number is 2200039. This is over usb. I really only added a C or an F on one range. My program is here, and it has the links where I got the other two programs in it.
I know this example is not in C#, but it does provide the format info you need. Think of it is as the API documentation written in C, you just have to translate it into C# yourself.
The protocol runs at 4800 baud, and 8N1 appears to work.
I need to convert a number between 1 and 6000000 to a letter combination like ABCDE.
Less letters is better. but i'm guessing i will need 4 or 5.
Can someone point me in the right direction as how to write an algorithm to convert numbers to letters and back? only A-Z. (caps).
You need to convert to base-26 numbering: 0 is A, 1 is B, 25 is Z, 26 is BA, etc.
The Hexavigesimal Wikipedia article has code for conversion to base 26.
There are 26 letters in the alphabet.
TYou have 26^4 < 6 000 000 and 26^5 > 6 000 000
Then you will need 5 letters, for most of your elements
Now you just need to express your number in base 26.
Their is only one way to write an X in 0 ... 6 000 000 as follow:
X = a4*26^4 + a3*26^3+ a2*26^2+ a1*26^1+a0
ai in {0,...25} then you just map ai with a letter from A to Z
The most naive thing to do would be to let A,B,...,Z represent the numbers 0,1,...,25 and the just convert your number to base 26 to get the alphabetic conversion.
For example, there is a C# implementation in this answer to this post.
Well if you want to convert from the decimal representation, then there are 10 digits [0-9] and if you want to have one character per decimal digit in the result, then you will need ten alpha characters. But if you convert from the binary representaion, just replace every 0 with an 'A' and every 1 with a 'B'...
everything depends on how you want to do it... The base you decide to use will determine how many letters you will need.
as an example, to do it from a binary representation,
take the number mod 2. If the result is 0 add an 'A' if its a 1, add a 'B'
Divide the number by 2 (or rightshift it one position.)
repeat until number is zero.
start with value of 57
1. 57 Mod 2 = 1 A
2. 57 / 2 = 28
3. 28 Mod 2 = 0 BA
4. 28 / 2 = 14
5. 14 mod 2 = 0 BBA
6. 14 / 2 = 7
7. 7 mod 2 = 1 ABBA --- A musical group !
8. 7 / 2 = 3
9. 3 mod 2 = 1 AABBA
10. 3/ 2 = 1
11. 1 mod 2 = 1 AAABBA
12. 1 / 2 = 0 --- -done
You should equate A = 0, B = 1 and so on upto Z = 25.
This would become a number system with the base (or radix) 26.
With this in mind, two digits can represent numbers ranging from 0 - 675 (ZZ = 675).
3 Digits would represent 26^3. i.e 0 - 17575.
With 5 digits you can represent from 0 - 11881375 (ZZZZZ).
You can take any standard algorithm that converts between decimal to its own radix to do that.
Conversion between Number bases can be referenced for help.
I am working on a little Hardware interface project based on the Velleman k8055 board.
The example code comes in VB.Net and I'm rewriting this into C#, mostly to have a chance to step through the code and make sense of it all.
One thing has me baffled though:
At one stage they read all digital inputs and then set a checkbox based on the answer to the read digital inputs (which come back in an Integer) and then they AND this with a number:
i = ReadAllDigital
cbi(1).Checked = (i And 1)
cbi(2).Checked = (i And 2) \ 2
cbi(3).Checked = (i And 4) \ 4
cbi(4).Checked = (i And 8) \ 8
cbi(5).Checked = (i And 16) \ 16
I have not done Digital systems in a while and I understand what they are trying to do but what effect would it have to AND two numbers? Doesn't everything above 0 equate to true?
How would you translate this to C#?
This is doing a bitwise AND, not a logical AND.
Each of those basically determines whether a single bit in i is set, for instance:
5 AND 4 = 4
5 AND 2 = 0
5 AND 1 = 1
(Because 5 = binary 101, and 4, 2 and 1 are the decimal values of binary 100, 010 and 001 respectively.)
I think you 'll have to translate it to this:
i & 1 == 1
i & 2 == 2
i & 4 == 4
etc...
This is using the bitwise AND operator.
When you use the bitwise AND operator, this operator will compare the binary representation of the two given values, and return a binary value where only those bits are set, that are also set in the two operands.
For instance, when you do this:
2 & 2
It will do this:
0010 & 0010
And this will result in:
0010
0010
&----
0010
Then if you compare this result with 2 (0010), it will ofcourse return true.
Just to add:
It's called bitmasking
http://en.wikipedia.org/wiki/Mask_(computing)
A boolean only require 1 bit. In the implementation most programming language, a boolean takes more than a single bit. In PC this won't be a big waste, but embedded system usually have very limited memory space, so the waste is really significant. To save space, the booleans are packed together, this way a boolean variable only takes up 1 bit.
You can think of it as doing something like an array indexing operation, with a byte (= 8 bits) becoming like an array of 8 boolean variables, so maybe that's your answer: use an array of booleans.
Think of this in binary e.g.
10101010
AND
00000010
yields 00000010
i.e. not zero. Now if the first value was
10101000
you'd get
00000000
i.e. zero.
Note the further division to reduce everything to 1 or 0.
(i and 16) / 16 extracts the value (1 or 0) of the 5th bit.
1xxxx and 16 = 16 / 16 = 1
0xxxx and 16 = 0 / 16 = 0
And operator performs "...bitwise conjunction on two numeric expressions", which maps to '|' in C#. The '` is an integer division, and equivalent in C# is /, provided that both operands are integer types.
The constant numbers are masks (think of them in binary). So what the code does is apply the bitwise AND operator on the byte and the mask and divide by the number, in order to get the bit.
For example:
xxxxxxxx & 00000100 = 00000x000
if x == 1
00000x00 / 00000100 = 000000001
else if x == 0
00000x00 / 00000100 = 000000000
In C# use the BitArray class to directly index individual bits.
To set an individual bit i is straightforward:
b |= 1 << i;
To reset an individual bit i is a little more awkward:
b &= ~(1 << i);
Be aware that both the bitwise operators and the shift operators tend to promote everything to int which may unexpectedly require casting.
As said this is a bitwise AND, not a logical AND. I do see that this has been said quite a few times before me, but IMO the explanations are not so easy to understand.
I like to think of it like this:
Write up the binary numbers under each other (here I'm doing 5 and 1):
101
001
Now we need to turn this into a binary number, where all the 1's from the 1st number, that is also in the second one gets transfered, that is - in this case:
001
In this case we see it gives the same number as the 2nd number, in which this operation (in VB) returns true. Let's look at the other examples (using 5 as i):
(5 and 2)
101
010
----
000
(false)
(5 and 4)
101
100
---
100
(true)
(5 and 8)
0101
1000
----
0000
(false)
(5 and 16)
00101
10000
-----
00000
(false)
EDIT: and obviously I miss the entire point of the question - here's the translation to C#:
cbi[1].Checked = i & 1 == 1;
cbi[2].Checked = i & 2 == 2;
cbi[3].Checked = i & 4 == 4;
cbi[4].Checked = i & 8 == 8;
cbi[5].Checked = i & 16 == 16;
I prefer to use hexadecimal notation when bit twiddling (e.g. 0x10 instead of 16). It makes more sense as you increase your bit depths as 0x20000 is better than 131072.