2

I am trying to convert string to hex then to Base64, it is working but but base64value getting is not matching - Vs 2008(.net 3.5) & vs 2019 (.net 4.6)

this my code --HEX string

Dim QrCodeHex as string ="010c426f6273205265636f726473020f3331303132323339333530303030330314323032322d30342d32355431353a33303a30305a0407313030302e303005063135302e3030"
dim QrCodeBase64En as string = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(QrCodeHex))

I am getting this result- is wrong

MDEwYzQyNmY2MjczMjA1MjY1NjM2ZjcyNjQ3MzAyMGYzMzMxMzAzMTMyMzIzMzM5MzMzNTMwMzAzMDMwMzMwMzE0MzIzMDMyMzIyZDMwMzQyZDMyMzU1NDMxMzUzYTMzMzAzYTMwMzA1YTA0MDczMTMwMzAzMDJlMzAzMDA1MDYzMTM1MzAyZTMwMzA=

Correct Result is

AQxCb2JzIFJlY29yZHMCDzMxMDEyMjM5MzUwMDAwMwMUMjAyMi0wNC0yNVQxNTozMDowMFoEBzEwMDAuMDAFBjE1MC4wMA==

how do i get it.

jps
  • 20,041
  • 15
  • 75
  • 79
SAIJAN KP
  • 151
  • 1
  • 5
  • 1
    The first base64 string contains the hex-string. The second (the correct one) contains the underlying string. `Bobs Records3101223935000032022-04-25T15:30:00Z1000.00150.00` but you wrote that you are *trying to convert string to hex then to Base64*, but the step to convert to hex seems not to be necessary. Just convert the orignal string to base64 instead. – jps Nov 23 '21 at 14:59
  • we need the following details to base64. – SAIJAN KP Nov 24 '21 at 07:20
  • we need the following details to base64. Tag + Length + value. Tag is 1 , Length is 12 & data is "Bobs Records" . bas64 string to hex conversion should be 010c426f6273205265636f726473. eg 112Bobs Records to base64 and decoding base 64 to hex 010c426f6273205265636f726473. please help – SAIJAN KP Nov 24 '21 at 07:26
  • thanks - its working now – SAIJAN KP Nov 24 '21 at 12:35

1 Answers1

4

System.Text.Encoding.UTF8.GetBytes converts a (regular) string to a byte array. However, in your case, you don't have a regular string ("Bobs Records...") but a hexadecimal representation of the byte array ("010c426f62..."). So you need to convert that hex representation to a byte array first:

Dim QrCodeHex As String = "010c426f6273205265636f726473020f3331303132323339333530303030330314323032322d30342d32355431353a33303a30305a0407313030302e303005063135302e3030"
    
' Hex to bytes
Dim bytes As Byte() = BigInteger.
    Parse(QrCodeHex, NumberStyles.AllowHexSpecifier).
    ToByteArray().Reverse().ToArray()
    
Dim QrCodeBase64En As String = Convert.ToBase64String(bytes)
    
Console.WriteLine(QrCodeBase64En)

(fiddle)

Note: I just used BigInteger for conversion, since it was the most compact way to do so without relying on .NET 5+ features. (The Reverse is required because, by default, it outputs the number as little-endian.) See this question for alternatives: How can I convert a hex string to a byte array?

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • thanks -- it working now – SAIJAN KP Nov 24 '21 at 12:36
  • Dear @heinzi I'm also having the same problem. i'm working on JAVA-11 platform. Could you please help me. import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Base64; public class QrCodeSample { public static void main(String[] args) { Charset cset = StandardCharsets.UTF_8; byte[] bArr = "010c426f6273205265636f726473020F3331303132323339333530303030330314323032322d30342d32355431353a33303a30305a0407313030302e303005063135302e3030".getBytes(cset); String bVal = Base64.getEncoder().encodeToString(bArr); System.out.println(bVal); } } – mahmood sk Dec 29 '21 at 09:33
  • 1
    @mahmoodsk: Sure, here is how to do the conversion from string to byte array in Java: https://stackoverflow.com/q/140131/87698 – Heinzi Dec 29 '21 at 10:11