1

I am trying to convert 2017 to a byte array.

byte[] bytes = new byte[]{0x02, 0x00, 0x01, 0x07}

What are the available c# functions that can help me do something like:

BitLibrary.GetBytes is just used as an example, I am not sure what is available to me/What format the above conversion is in.

bytes[] bytes = BitLibrary.GetBytes(2017) 

I first attempted to use BitConverter.GetBytes(2017) but that returned

{byte[4]}
    [0]: 225
    [1]: 7
    [2]: 0
    [3]: 0

When I looked for Decimal to Hexadecimal Converters online, 2017 return 7E1.

UPDATE - I am not sure of what this format is called, but from my requirements I am given the following -

2017

byte[] bytes = new byte[]{0x02, 0x00, 0x01, 0x07}

2021

byte[] bytes = new byte[]{0x02, 0x00, 0x02, 0x01}
Kush
  • 438
  • 1
  • 7
  • 20
  • unfortunetly, its only showing a hexadecimal conversion, it does not output {0x02, 0x00, 0x01, 0x07} for the integer 2017 – Kush Jul 12 '20 at 09:44
  • Does this answer your question? [Is there an easy way to turn an int into an array of ints of each digit?](https://stackoverflow.com/questions/829174/is-there-an-easy-way-to-turn-an-int-into-an-array-of-ints-of-each-digit) – GSerg Jul 12 '20 at 09:45
  • Can you show input and excpected output? – Stefan Jul 12 '20 at 09:47
  • I have updated the question with examples. Its called Hex Encoded byte array, with 0x as the prefix – Kush Jul 12 '20 at 09:48
  • 3
    FYI: there is no such thing as a "Hex Encoded Byte Array". A byte array is an array of 8-bit numbers. So, as for your output: I hope you do realize the output is a debatable representation of the number ;-) – Stefan Jul 12 '20 at 09:51
  • hmm.. It would appear that I may very well have to prepend 0x0 to each character in the integer. I wonder what is this conversion called though. it is used everywhere in my requirements and is only refered to as 'Hex Encoded Byte Array' – Kush Jul 12 '20 at 09:54
  • May I assume that you need to transform in a byte array a year value? – Steve Jul 12 '20 at 10:15

1 Answers1

2

note: this answer reflects the original title and information given by OP in the question:

Convert Integer to Hex Encoded Byte Array format with 0x as prefix

By itself this answer does not reflect a big endian representation of the number.


Okay,

So

input: 2017 result a byte array with the individual digits; something like:

public static byte[] Convert(uint input)
{
    return input.ToString().Select(c => (byte) (c - '0')).ToArray();
}

as for the usuage:

public static void Main()
{
    foreach(var b in Convert(2017))
    {
       Console.WriteLine("0x" + b.ToString("X2"));  
    }
}
    
public static byte[] Convert(int input)
{
    return input.ToString().Select(c => (byte) (c - '0')).ToArray();
}

Output:

0x02
0x00
0x01
0x07
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • Thank you for your asnwer, I found out that this byte array in in Big-Endian format. Sorry I am not too familiar with this term, I am currently testing your function with my integer values to see if it holds. – Kush Jul 12 '20 at 10:04
  • 1
    You're welcome, I hope my answer helped you. Good luck. – Stefan Jul 12 '20 at 10:04
  • This works perfectly. I found out that the requirements were in fact not worded correctly at all. This solution satisfies my use cases. Much appreciated. – Kush Jul 16 '20 at 10:37