-2
uint8_t certificateSerialNumber[] = {0x02, 0x04, 0x24,0xA8,0x16,0x34}; 

The decimal 614995508 (actual serial number)is converted to hexadecimal 24A81634.

The above unit8 array is the representation on TLV(tag length value) triplet of serial number where T is 0X02, L is 0X04 and V is the hexadecimal string.

So i'm able to split the hexadecimal string by two characters at a time

How to convert hexdecmial to TLV triplet format as shown above in unit8_t array in objective-c? reference: https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-integer

  • 3
    What is "string/Integer"? – sbooth Dec 28 '21 at 16:26
  • This reference might be helpful: https://stackoverflow.com/questions/16635548/how-do-i-convert-an-array-of-ints-to-a-nsstring-in-objective-c/16635699 – apodidae Dec 28 '21 at 20:16
  • Change the type from 'uint8_t' to just plain 'int' and use the solution in the above reference; it should work. Please post problem code if it doesn't work. – apodidae Dec 28 '21 at 21:00
  • Are you sure your data is not encrypted? – Leo Dabus Dec 29 '21 at 00:31
  • @LeoDabus Thanq soo much for reply my array is ASN.1 representation – noor palasamudram Dec 29 '21 at 04:53
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 11 '22 at 02:30
  • @sbooth Thanq for viewing my question. Please find the edited question – noor palasamudram Jan 20 '22 at 15:01
  • @LeoDabus Thanq for viewing my question. Please find the edited question – noor palasamudram Jan 20 '22 at 15:02
  • @apodidae Thanq for viewing my question. Please find the edited question – noor palasamudram Jan 20 '22 at 15:02
  • Still unclear. Give example input and desired output. You are starting with the array `{0x02, 0x04, 0x24,0xA8,0x16,0x34}`? And you want to get what? – matt Jan 20 '22 at 15:11
  • @matt in the array from index 2 to 5 is a hexadecimal value 24A81634 of 614995508. – noor palasamudram Jan 20 '22 at 15:49
  • I know that. Please answer the question I asked you. What actual result do you want to get? – matt Jan 20 '22 at 15:52
  • @matt lets take another example Decimal value :100000040509 Hex of above decimal: 174877863D So we can divide hex decimal value to five parts [17 48 77 86 3D] when i have given Decimal value it should convert to Hexdecimal value. that hexadecmial should convert into below uint8_t array format. uint8_t certificateSerialNumber[] = {0x02, 0x05, 0x17,0x48,0x77,0x86,0x3D}; – noor palasamudram Jan 20 '22 at 16:20
  • OK, so you are _not_ starting with the array? You are starting with a Decimal? And you want to _get_ the array? Is that right? – matt Jan 20 '22 at 16:32
  • @matt yes when decimal is given as input it should convert to the unti8 array – noor palasamudram Jan 20 '22 at 16:56

1 Answers1

0

So I'm a little surprised at the Decimal, but whatever:

let what: Decimal = 614995508
var num = (what as NSDecimalNumber).uint64Value
var arr = [UInt8]()
while num > 0 {
    let rem = num % (16*16)
    arr.append(UInt8(rem))
    num = num / (16*16)
}
arr.append(UInt8(arr.count))
arr.append(2)
arr = arr.reversed()
arr.forEach {
    print(String($0, radix: 16), terminator: " ")
}

Result: 2 4 24 a8 16 34

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I have another query the hex conversion provided by you will support hex signed 2's compliment also? – noor palasamudram Jan 21 '22 at 14:17
  • Please do not say "thank you" in a comment. — Please do not introduce extraneous questions. I've answered the question you asked. If you have a different question, press the Ask Question button and ask it. – matt Jan 21 '22 at 14:27