How to convert hex to binary? - objective-c

For Objective-C:
Hi everyone, I'm trying to convert a hex input into binary. For example, someone enters in :
A55
I want that to convert to
101001010101
I've tried looking through past posts and none seem to be working for me. Any help would be greatly appreciated.

Use a lookup table: there are only 16 possible characters in a HEX representation, each corresponding to a four-character binary code group. Go through the HEX character-by-character, obtain a lookup, and put it in the resultant NSString.
Here is a copy of the lookup table for you.
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
There are multiple options as to how to do lookups. The simplest way would be making a 128-element array, and placing NSStrings at the elements corresponding to codes of the characters (i.e. at positions '0', '1', ..., 'E', 'F', with single quotes; these are very important).

I believe there is a built-in function for this. If not, you should at least be able to go hex->dec then dec->bin
You can write the conversion from scratch if you know the number of characters, bin to hex is common enough algorithmically.
A mathematical look at the algorithms
SO Answers in C/C++ Another
Base 10 to base n in Objective C
C Hex->Bin

Build a lookup table (an array where you can supply a value between 0 and 15 to get the binary for that hex digit):
char *hex_to_bin[] = {
"0000", "0001", "0010", "0011",
/* ... */
"1100", "1101", "1110", "1111"
};
There should be 16 elements in that table. The conversion process for multiple digits is to handle one digit at a time, appending the results onto the end of your result storage.
Use getchar() to read a char:
int c = getchar();
if (c < 0) { puts("Error: Invalid input or premature closure."); }
Use strchr() to determine which array index to retrieve:
char *digits = "00112233445566778899AaBbCcDdEeFf";
size_t digit = (strchr(digits, c) - digits) / 2;
Look up the corresponding binary values for digit:
printf("%s", hex_to_bin[digit]); // You'll want to use strcat here.

Related

How to shrink 10 digit numeric into 2 character

I have input comprising five character upper-case English letters e.g ABCDE and I need to convert this into two character unique ASCII output.
e.g. ABCDE and ZZZZZ should both give two different outputs
I have converted from ABCDE into hex which gives me 4142434445, but from this can I get to a two character output value I require?
Example:
INPUT1 = ABCDE
Converted to hex = 4142434445
INPUT2 = 4142434445
OUTPUT = ?? Any 2 ASCII Characters
Other examples of INPUT1 =
BIRAL
BRMAL
KLAAX
So you're starting with a 5-digit base-26 number, and you want to squeeze that into some 2-digit scheme with base n?
All possible 1-5 digit base-26 numbers gives you a number space of 26^5 = 11,881,376.
So you want the minimum n where n^2 >= 11,881,376.
Which gives you 3446.
Now it's up to you to go and find a suitable glyph block somewhere in UTF where you can reliably block-out 3446 separate characters to act as your new base/alphabet. And construct a mapping from your 5-char base-26 ABCDE type number onto your 2-char base-3446 wierd-glyph number. Good luck with that.
There's not enough variety in ASCII to do this, since it's only 128 printable characters. Limiting yourself to 2-chars of ASCII means you can only address a number space of 16384.

Pair of Zeros separated by 1's NFA?

I'm working on a language L = { every pair of zeros is separated by 1's that's of length 4i, i>=0 }
e.g. 110011110 should be accepted because the first two zeros are separated by nothing. Then the next pair is separated by 4 ones.
Here's my attempt for the NFA, anything missing?
We can use Myhill-Nerode directly to derive a minimal DFA for this language. The reasoning is straightforward.
e, the empty string, can be followed by any string in L to get to a string in L.
0 can be followed by 1* or by (1^4i)L to get to a string in L.
1 can be followed by L to get to a string in L. This means it is indistinguishable from the empty string. That also means we don't need to worry about longer strings that start with 1, since they will be covered by shorter strings that don't start with 1.
00 can be followed by the same stuff as 0 can, so it is indistinguishable. This also means we don't need to worry about longer strings that start with 00, since they are handled by shorter strings that don't.
01 can be followed by 1* or 111(1^4i)L to get to a string in L.
10, 11 can be ignored as they start with 1 (see 3)
000, 001 can be ignored as they start with 00 (see 4)
010 cannot be followed by anything to get a string in L. We can also ignore anything that starts with this since it can't lead to a string in L.
011 can be followed by 1* or 11(1^4i)L to get a string in L.
100, 101, 110, 111 can be ignored as they start with 1 (see 3)
0000, 0001, 0010, 0011 can be ignored as they start with 00 (see 4)
0100, 0101 can be ignored since they start with 010 (see 8)
0110 cannot be followed by anything to get to a string in L so is indistinguishable from 010.
0111 can be followed by 1* or 1(1^4i)L to get a string in L.
1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111 can all be ignored since they start with 1 (see 3)
The only string of length four distinguishable from shorter strings was 0111; 01110 is indistinguishable from 010 in that nothing leads it to a string in L, and 01111 is indistinguishable from 0 in that it can be followed by 1* or (1^4i)L to get to something in L.
This may seem like a lot of work, but it was a pretty simple exercise. We can go back through and list our complete set of shortest-length distinguishable strings:
e, from point 1 above
0, from point 2 above
01, from line 5 above
010, from point 8 above
011, from point 9 above
0111, from point 14 above
To write down a DFA, we need one state for each of these shortest-length distinguishable strings. The transitions from the state corresponding to string x will lead to states corresponding to strings formed by concatenating input symbols to x. So:
___________________________
| ^
0 V 1 1 1 | 1
--->(e)--->(0)--->(01)--->(011)--->(0111)
\_/ \_/ | 0 | 0 | 0
1 0 | V V
|<-----------------
V
(010)
\___/
0,1
e is the initial state.
the state for e loops to itself on 1 since e.1 = 1 and 1 is indistinguishable from e. Indistinguishable strings lead to the same state in a minimal DFA.
the state for e goes to the state for 0 on 0 since e.0 = 0 and 0 is distinguishable from all strings of the same or shorter length.
state 0 leads to state 01 leads to state 011 leads to state 0111 on 1s, since 0111 = 011.1 = 01.1.1 = 0.1.1.1 and these are all distinguishable from strings of the same or shorter length.
0111 leads to 0 on 1 since 0111.1 = 01111 which is indistinguishable from 0.
states 0, 01, 011 and 0111 lead to state 010 on 0 since 0.0 = 00, 01.0 = 010, 011.0 = 0110 and 0111.0 = 01110 are indistinguishable from 010.
010 leads to itself on all inputs since nothing can be added to it to get a string in L, so the same is true for any concatenation with this at the front.
Now that we have the structure, we simply have to look at each state and say whether its canonical string is in L. If so, the state is accepting; otherwise, it is not.
e is in L, so (e) is accepting.
0 is in L, so (0) is accepting.
01 is in L, so (01) is accepting.
010 is in L, so (010) is not accepting.
011 is in L, so (011) is accepting.
0111 is in L, so (0111) is accepting.
This completes the derivation of the minimal DFA for L.

ISO 8583 Field 22

I'm trying to build an parser to deserialze into object. Socket will send byte into parser. For the length of field 22 POS Entry Mode will N3 and byte will be always 2 digit. How to get the value for this field ?
You read the ASCII value of this field, and convert it into an integer.
if it says N3 that means they are three digits numeric field, so if the value say 51, you cast it to 051 and send the ASCII equivalent
Field 22 is pos entry mode. It's 3 digit numeric value. If format is BCD then 2 bytes contains 4 digits[ 0 (padded) + 3 digit POS entry mode). If format is ascci then it is 3 byte.

How does one substitute the CVC3, ATC and unpredictable number in EMV contactless track data?

I'm trying to assemble proper track data given a CVC3 and a bunch of positional parameters. But the EMV C-2 Kernel book is about as obtuse as you could imagine (would it kill somebody to include an example!?!). Can anyone help work this example:
9f62 - pcvc3(t1) - Position of CVC3 in track1: 0x38 (4-6?)
9f63 - punatc(t1) - Unpredictable Number Track1 Pos: 0x3C6 (2-3 7-10?)
9f64 - natc(t1) - Digits in track1 ATC: 4
9f65 - pcvc3(t2) - Position of CVC3 in track2: 0x38 (4-6)
9f66 - punatc(t2) - Unpredictable Number Track2 Pos: 0x3C6 (2-3 7-10?)
9f67 - Digits in track2 ATC: 4
After successful checksum generation:
9f61 - track2 CVC3 - 2EF4
9f60 - track1 CVC3 - 609B
9f36 - ATC - 1E47
assuming the discretionary data field starts out as all 0s, how does it end up? The spec says this:
Convert the binary encoded CVC3 (Track2) to the BCD encoding of the
corresponding number expressed in base 10. Copy the q least significant digits of the
BCD encoded CVC3 (Track2) in the eligible positions of the 'Discretionary Data' in
Track 2 Data. The eligible positions are indicated by the q non-zero bits in
PCVC3(Track2).
I read that as:
CVC3 = 0x609B = 24731 (so copy 731? What does BCD have to do with this? Or are they just saying "copy the 731 as bcd encoded to the byte array"?)
yes you are correct it is rather obtuse. you are correct that you would convert your p values (pCVC3, and PUNATC) to binary. (0011 1000, 0011 1100 0110 for your track1 p values) you then right align the proper values with the discretionary data. example
Bxxxxxxxxxxxxxxxx^ /^14111014010000000000
....000000000000000000CCC000
....00000000000000AAAA000UU0
so you say that you CVC3 for track 1 is 609B which is 24,731, since you PCVC3 is asking for only 3 characters youd set 731 in. your ATC is 1E47 which is 7,751. your PUNATC is asking for 4 digits so you'd use 7751. FYI... if the ATC is lower than the requested characters you'd pad with 0's. Your unpredictable number is even more tricky... so you make a 4 byte random number. convert it to a uint (base 10) and then mark the 8 most significant bytes as 0. example. lets say your random 4 bytes is 29A6 06AE. in base 10 that is 698,746,542. mark out the first 8 characters with 0. and you are left with 000,000,002. you'd place 02 in for you unpredictable number placment.. so.. all that said your track would look like this
Bxxxxxxxxxxxxxxxx^ /^14111014017751731020
the last character is equal to the length of the unpredicatable number (numeric) digits. which was 02.. so the last digit is 2 making your final track data
%Bxxxxxxxxxxxxxxxx^ /^1411014017751731022;
track2 is very similar. good luck with it. I understand your frustrations with this. :)

Is there a way to represent a number in binary where bits have approximately uniform significance?

I'm wondering if it is possible to represent a number as a sequence of bits, each having approximately the same significance, such that if we flip one of the bits, the overall value does not change by much.
For example, we can use sequences of 4-bits, where each group represents a value from 0 to 15 and the overall value is the sum of all these values.
0110 0101 1101 1010 1011 → 6 + 5 + 13 + 10 + 11 = 45
and now flipping any bit can only incur in a maximum difference of 8 in the final value.
Some drawbacks obviously exist with this approach:
values have multiple representations, with some values having more representations than other ones (for example, there are 39280 distinct representations for the number 38, and only 1 for the number 0);
the amount of values that can be represented is greatly reduced (this representation allows for integers from 0 to 75, while 20 bits could normally represent 220 ~ 1 million different integers).
Are there any resources I can find concerning this problem? I can't seem to find anything online, but maybe I'm not searching with the right keywords. What other alternatives exist to my approach? Do they improve on its disadvantages?