In Microsoft Access I would like to convert decimal into hexadecimal, formatting it in a way that it is filled up with leading zeros when less than four digits.
I tried with
Format(Hex([decimalfieldname]);"0000")
Format(Hex(137);"0000") => Output: 0089
Format(Hex(127874);"0000") => Output: 1F382
but:
Format(Hex(169);"0000") => Output: A9
I would like to obtain 00A9
Any ideas how I can do that?
Don't use Format for strings, use Right instead, pad 4 zeroes by default and take the last 4 characters.
Right("0000" & Hex(169), 4)
Note that you have to implement a check if you want more than 4 digits, and want to return the full number in that case, using IIF
Iif(Len(Hex([decimalfieldname])) > 4, Hex([decimalfieldname]), Right("0000" & Hex([decimalfieldname]), 4))
Related
I need to validate two possible patterns for the input using HTML5 pattern.
123456789a (first 9 digits should be exactly numbers and then an alphabetical character) nothing more nothing less
OR
123456789012 (exactly 12 digits nothing more nothing less)
I tried ^([0-9]{12,12})|([0-9]{9,9}[A-Za-z]{1,1}), ^([0-9]{12})|([0-9]{9,9}[A-Za-z])$, and many more but the problem is if user enters an alphabet character when the total length is between 9 and 12, then it takes as a valid input. But it should be not.
Valid input is either 12 digits, or 9 digits with one char.
What have I done wrong?
You could check for 9 digits at the start of the string: (see ^, beginning of input assertion, \d, digit character class and the x{n} quantifier)
^\d{9}
followed by either an alphabetical character or 3 more digits, and the end of the string: (see the non capturing group (?: ... ), [ ... ], the character set, x|y and $, end of input assertion)
(?:[a-zA-Z]|\d{3})$
So the expression would be:
^\d{9}(?:[a-zA-Z]|\d{3})$
I'm trying to sort out a regex pattern for any positive number, NN:NN. Its for the user to enter in the duration of exercise HH:MM where MM is <60.
For eg: 22:22 , 99:59, 01:00, 66:00 but not 00:00
So far I have tried this pattern="([01]?[0-9]|2[0-3]):[0-9][0-9]" but its only for 24 hour time.
pattern="([01]?[0-9]|2[0-3]):[0-9][0-9]"
I expected 55:55 but it does not allow me to go above number 2 on first digit ('2'_:__)
Using the character class [01]?[0-9] also matches 00 as well as [0-9][0-9]
One option is to use a negative lookahead to check if what is on the right is not 00:00 and make use of word boundaries \b
\b(?!00:00)[0-9]{2}:[0-5][0-9]\b
\b Word boundary
(?!00:00) Assert what is on the right is not 00:00
[0-9]{2}:[0-5][0-9] match 2 digits : number 00 till 59
\b Word boundary
Regex demo
const regex = /\b(?!00:00)[0-9]{2}:[0-5][0-9]\b/;
[
"22:22",
"99:59",
"99:60",
"01:00",
"66:00",
"00:00"
].forEach(s => console.log(s + ": " + regex.test(s)))
Use
/^(?=.*[1-9]+)\d{2}\:\d{2}$/;
Explanation
(?=.*[1-9]+) Lookahead for at least 1 non zero digit
\d{2}\ To match any 2 digits in hour part
\:[0-5][0-9] To match minutes with max 59 after :
Here's a JavaScript implementation of the RegEx
let re = /^(?=.*[1-9]+)\d{2}\:[0-5][0-9]$/;
console.log("00:00 => ",re.test("00:00"));
console.log("01:00 => ",re.test("01:00"));
console.log("00:10 => ",re.test("00:10"));
console.log("11:00 => ",re.test("11:00"));
console.log("33:44 => ",re.test("33:44"));
console.log("66:60 => ",re.test("66:60"));
Since there are 2 parts to this, it may be better to validate this in 2 parts instead of 1. If you try to do too much in a regex, it will get unwieldy and impossible to understand. It'll also become too difficult to test reasonably.
The first part of this is a positive assertion. You want to make sure you have 2 numbers followed by a colon and 2 more numbers:
/^\d{2}:\d{2}$/
To limit the second group of digits to valid minutes (e.g. something less than 59) the following regex will work:
/^\d{2}:[0-5][0-9]$/
The second is a negative assertion. You want to make sure it's not 00:00. Depending on the framework you're using, there's generally an easy way to add this second check. If you're not using a framework, you would listen for changes to the input and validate it on a change.
I have a BINARY number which i want to convert it into the DECIMAL and OCTAL.
(0100 1111 1011 0010)2
I know how to convert it into the decimal. But the question making me confuse. Because middle of every 4 digits there is a space "0101 1111"
can u help me how to understand this question.
Thanks
First of all, make sure that number you are converting into Decimal and Octal is actually 'Binary' and not 'Binary Coded Decimal (BCD)'. Usually when the number is grouped into 4 binary digits, it represents a BCD instead of just binary.
So, once you make sure its actually binary and not BCD, the conversion to both decimal and octal are simple steps.
For binary to octal, you group the binary number into sets of 3 digits, starting form the Least Significant Bit(LSB or right-most) to the Most Significant Bit(MSB or left-most). Add leading zeros if a group of 3 digits can not be formed at the MSB.
Now convert each group of digits from binary to octal:
(000) -> 0
(001) -> 1
.
.
(111) -> 7
Finally put the numbers together, and there you have your binary converted to octal.
Eg:-
binary - 00101101
split into groups of 2: -> 000 101 101 -> 0 5 5 - > 55
Difference between'Binary Coded Decimal' and 'Binary':
For the decimal number 1248
the binary would simply be 10011100000
However, the BCD would be -> 0001 0010 0100 1000
The spaces are not part of the number, it's just to make it easier for humans to read. Conversion from binary to octal is simple. Group the binary digits into sets of 3 (from right to left, add extra 0s to the leftmost group, then convert each group individually. Your example:
0100 1111 1011 0010 -> 100 111 110 110 010 -> 47662
The space is just for readability. Especially nice if you try to convert this to hex, because 4 binary digits make up one hex-digit.
Firstly, those spaces are for human readability; just delete them. Secondly, If this is not for a computer program, simply open up the windows calculator, go to view, and select programmer. Then chose the bin radio button and type in your number. the qword radio button should be selected. If it's for a program, I will need to know what language to help you.
To convert octal to decimal very quickly there are two methods. You can actually do the actual calculation in bitshift. In programming, you should do bitshift.
Example octal number = 147
Method one: From left to right.
Step 1: First digit is one. Take that times 8 plus 4. Got 12.
Step 2: Take 12 times 8 + 7. Got 103, and 103 is the answer.
Ultimately you can use method one to convert any base into base 10.
Method one is reading from left to right of the string. Make a result holder for calculation. When you read the first leftmost digit, you add that to a result value. Each time you read a new digit, you take the result value and multiply that by the base of the number(for octal, that would be 8), then you add the value of the new digit to the result.
Method 2, bitshift:
Octal Number: 147.
Step 1: 1 = 1(bin) = Shift << 3 = 1000(result value)
Step 2: 4 = 100(bin) + 1000(result value) = 1100(result value)
Step 3: 1100(result value) Shift << 3 = 1100000
Step 4: 7 = 111(bin) + 1100000(result value) = 1100111
Step 5: 1100111 binary is 103 decimal.
In a programming loop, you can do something like the below, and it is lightning fast. The code is so simple that it can be converted into any programming language. Note that there isn't any error checking.
for ( int i = 0; i < length; i++ ){
c = (str.charAt(i) ^ 48);
if ( c > 7 ) return 0; // <- if char ^ 48 > 7 then that is not a valid octal number.
out = (out << 3) + c;
}
% format %2s 100
100
% format %.2s 100
10
%
%
% format %0.2s 100
10
%
I am not able to understand the difference between %2s and %.2s .
Can anyone explain me ?
TCL format command manual page specifies that format string can consist of six different parts. In this case second, third and fourth portions are of interest.
If there is a character from set [-+ 0#], they specify justification of the field, if there should be padding, sign shown of numbers, etc. 0 in the third example specifies that number should be padded with zeros instead of spaces. However, in this example there is nothing to pad.
If there is some other number without dot (2 in the first example), the number is interpreted as minimum field length and number is padded with spaces if necessary.
If there is a dot, the number after if interpreted as precision indicator and way it behaves differs depending on the other format parameters. For strings it means the maximum number of characters.
With
format %4.2s foo
you then get
fo
That is, at most two characters are printed, but the field width is at minimum 4 characters.
If you are actually trying to print a number instead of string, then the sixth (the only mandatory) field is important. "s" means "print as is". For numbers you want to use for example "d" which means decimal (integer) or "f" for floating point. Check the manual for the whole list.
With
format %4.2d 100 # Print with at least two numbers and with field width of 4 characters
you get
100
With
format %08.2f 123.45678 # Field width 8, pad with zeros, print two decimals
you get
00123.46
In the last example notice that all numbers and the dot are counted for the field length and that the number has been rounded.
Just wondering on how I would go about converting binary to hexadecimal??
Would I first have to convert the binary to decimal and then to hexadecimal??
For example, 101101001.101110101010011
How would I go about converting a complex binary such as the above to hexadecimal?
Thanks in advance
Each 4 bits of a binary number represents a hexadecimal digit. So the best way to convert from binary to hexadecimal is to pad the binary number with leading zeroes so that the number of bits is divisible by four.
Then you process four bits at a time and convert them to a single hexadecimal digit:
0000 -> 0
0001 -> 1
0010 -> 2
....
1110 -> E
1111 -> F
No, you don't convert to decimal and then to hexadecimal, you convert to a numeric value, and then to hexadecimal.
(Decimal is also a textual representation of a number, just like binary and hexadecimal. Although decimal representation is used by default, a number doesn't have a textual representation in itself.)
As a hexadecimal digit corresponds to four binary digits you don't have to convert the entire string to a number, you can do it four binary digits at a time.
First fill up the binary number so that it has full groups of four digits:
000101101001.1011101010100110
Then you can convert each group to a number, and then to hexadecimal:
0001 0110 1001.1011 1010 1010 0110
169.BAA6
Alternatively, you can split the number into the two parts before and after the period and convert those from binary. The part before the period can be converted stright off, but the part after has to be padded to be correct.
Example in C#:
string binary = "101101001.101110101010011";
string[] parts = binary.Split('.');
while (parts[1].Length % 4 != 0) {
parts[1] += '0';
}
string result =
Convert.ToInt32(parts[0], 2).ToString("X") +
"." +
Convert.ToInt32(parts[1], 2).ToString("X");
You could simply have a small hash table, or other mapping converting each quadruplet of binary digits (as a string, assuming that's your input) into the corresponding hex digit (0 to 9, A to F) for the output string. You'll have to bunch the input bits up by 4, left-padding before the '.' and right-padding after it, with 0 in both cases, as needed.
So...:
locate the '.'
left of the '.', bunch by 4, left-padding the last bunch, going leftwards: in your example, 1001 leftmost, then 0110, finally 0001 (left-padding), that's it;
ditto to the right -- in your example 1011, then 1010, then 1010, finally 0110 (right-padding)
each bunch of 4 binary digits, via a hash or other form of hashing, turns into the hex digit to put in that place in the output string.
Want some pseudo-code for it, e.g., Python?
The simplest approach, especially if you already can convert from binary digits to internal numeric representation and from internal numeric representation to hexadecimal digits, is to go binary->internal->hex. I say internal and not decimal, because even though it may print as decimal, it is actually being stored internally in binary format. That said, it is possible to go straight from one to the other. This does not apply to your specific example, but in many cases when converting from binary to hex, you can go four digits at a time, and simply lookup the corresponding hex values in a table. There are all sorts of ways to convert.
BIN to HEX
Binary and hex are natively compatible. Just group 4 binary digits(bits) and substitute the corresponding HEX-digit.
More reference here:
http://en.wikipedia.org/wiki/Hexadecimal#Binary_conversion