Home > database >  Convert eight bytes into a string
Convert eight bytes into a string

Time:09-01

i have eight byte variables:

Byte7 = 0 dez, hex: 00
Byte6 = 2 dez, hex: 02
Byte5 = 32 dez, hex: 20
Byte4 = 33 dez, hex: 21
Byte3 = 17 dez, hex: 11
Byte2 = 37 dez, hex: 25
Byte1 = 3 dez, hex: 03
Byte0 = 88 dez, hex: 58

Question: How can i put all these together into only one string that then has the hex-value of each byte? The result string should be like this: 0002202111250358

Thanks

CodePudding user response:

This is one way to do it:

Console.WriteLine($"{Byte7:X2}{Byte6:X2}{Byte5:X2}{Byte4:X2}{Byte3:X2}{Byte2:X2}{Byte1:X2}{Byte0:X2}");

It uses the string formatter X2 to format the byte as a 2-digit hexadecimal.

CodePudding user response:

If you want to generalise this to any number of bytes (within reason) you could write a helper method to do this:

public static string BytesToString(params byte[] bytes)
{
    return string.Concat(bytes.Select(b => b.ToString("X2")));
}

Which you can then call with any number of bytes:

public static void Main()
{
    byte Byte7 = 0;
    byte Byte6 = 2;
    byte Byte5 = 32;
    byte Byte4 = 33;
    byte Byte3 = 17;
    byte Byte2 = 37;
    byte Byte1 = 3;
    byte Byte0 = 88;

    var result = BytesToString(Byte7, Byte6, Byte5, Byte4, Byte3, Byte2, Byte1, Byte0);
    Console.WriteLine(result); // 0002202111250358
}

CodePudding user response:

Also, if you like to achieve your goal in a bit esoteric way, you can utilize bitwise or(|) operation to combine individual bytes into an unsigned 64-bit integer. This is fairly easy with low-level programming languages such as C/C or some intermediate programming languages like C# (but not with Java!).

And with, good old javascript, it can be measured somewhat between arghhh! to this is the worst nightmare :).

I am sharing a snippet just for educational purposes, but of course, you are welcome to adopt the solution that snippet provides if you like and/or crazy enough :D...

Thank you, and have fun.

// <!> `n >>> 0` triple right shift enforces to be unsigned 32bit integer

// <!> unsigned 32 bit versions of the bytes
const b7 =  0 >>> 0; // dez, hex: 00
const b6 =  2 >>> 0; // dez, hex: 02
const b5 = 32 >>> 0; // dez, hex: 20
const b4 = 33 >>> 0; // dez, hex: 21
const b3 = 17 >>> 0; // dez, hex: 11
const b2 = 37 >>> 0; // dez, hex: 25
const b1 =  3 >>> 0; // dez, hex: 03
const b0 = 88 >>> 0; // dez, hex: 58

// combined the high part of the bytes as u32 
const high = (
     b7 << 24
   | b6 << 16
   | b5 <<  8
   | b4 <<  0
) >>> 0;

// combined the low part of the bytes as u32 
const low = (
     b3 << 24
   | b2 << 16
   | b1 <<  8
   | b0 <<  0
) >>> 0;

// BigInt (unsigned) version of the low
const u64low = BigInt.asUintN(64, BigInt(low));

// BigInt (unsigned) version of the high
const u64high = BigInt.asUintN(64, BigInt(high));

// BigInt (unsigned) version of the combined high and low
// <!> Multiplyting u64 with 0x100000000n is the same as shifting the bits 32 times to the left
// <!> BigInt can not operate bitwise operators with good old JS
const combined = BigInt.asUintN(64, u64low   u64high * 0x100000000n);

// Hex string of the result
const resultStr = `0x${combined.toString(16).padStart(16, '0')}`;

// Let's show it in our esoteric div :)
document.querySelector('#esoteric').innerHTML = resultStr;
#esoteric {
  background: #eee;
  color: #333;
  border: 0.0625rem solid #333;
  border-radius: 0.25rem;
  padding: 0.625rem;
}
<div id="esoteric"></div>

  • Related