1

In this question I asked how can I convert varbinary to valid BASE64 string value in MSSQL.

Now I'd like to know how can it be done without MSSQL (in .NET f.e.).

Let's think of a method which accepts the following string (or byte array)

0x1F8B0800000000000400CD57DB6EDB38107D5F60FF81E0D31648C5247DD906528BD4898BA075525469DF6969EC10CB8BCA4B607F5B1FFA49FB0B3BBACB521CB7E9A228020422357366E6F0CC50FEF7EBB7F8F54649720FD609A3137A121D53023A33B9D0EB8406BF7AFE377DFDEACF3FE2CB5C6DC8E7D6EE4569879EDA25F4CEFBE28C3197DD81E22E5222B3C699958F32A318CF0D3B3D3E7EC94E4E182004452C42E28F417BA1A05AE072667406850F5C2E4C0ED235FBF826AD50C93557E00A9E414253AE0A0917DCF325771035CB254278D8784ACEA5E098550A724509D7DA78EE31E7B34F0E526F8D5EA7056E7079BB2D00ED565C3A686A39EBCDBFB7ACE3D3B22CD63BB6505970DEA81F043C79D1F0C4C6EE4F629B763C229397C8B8DF9655576C26746E0C25E3386733694B9B433C47159C001721CC11D9353EEA84827A2AFF8EC82C481F2C241A82B75C1E910F612945F60EB6B7E61FD0890E520ED3C584F1DDCE066E7DB0A600EBB71F61D514719553C276FDD8D8B1731BF8D435BE0D029FAF31365F4AE8C4C01E752FFFB700A8286C144A167CF31EF4DADF25141F29998B0DE4ED4E83FA490BEC2B74F2368C82C4AC3F9DE999959473A1C136094C243F3944D47A738EAE09B75B518D9B82EFA5E028E973A8DB27AA14F2509E5D467DA3B2BA53DB8E667B5A3A5EF0A240CA062DDEEC90B4EEEFD9F3F4C7C5AE6A0C96B90734DF65DB45F2C6F2358CDE6268CC742EACF3AD902999E56A6236E67F0FB76DB401C5633DF78CB7C6E5F3CE29EFEBBFF26C46703D97732C4F81F655A5D06554F7FCC4AB1AB35C72FB409FCC8C0C4AEFEBB5C7BCEB3619FAD73B5384988DD21ED3C4263C8D3A7ECCF963921D9B74D13BE98E241A3772397C354DF4539B5082D4DC8BBCD2CED67950516910A55FE44C0AACB73758702D56E07C3D1429DE08A7A31BEDF7B95D9873B93C7CC5FCF2A11EB4F81200D9C46C5602EC4F0D787DCF6D76C7ED5F8A6F9E0D917E76884F67CDC111BD6F42D72A4B68BEC4C56D9D5E3DD19F38BBA7828FD9F08B2DBE0027D63D44F9FDA6212B95D483B636577A655A82B19C6146ADC988FF05789E232BE7160F90671E5F67E05C75D17EE632A0C9A55A427EA56F822F823F770ED4526E87F5C6ECF1F8D505B59B737C53942BF77F9480690A2C016EF49B2064DEE53D9FEA6F1F442990B780FB5587E28706C2ADB71DD2B5D1DF09D4D0770105E8B2296E016F120473373AE5F7F094DCF0FBE23DAC79B66DE7D67E90C307B14B7B7C21F8DA72E51A8CDEBFFC15C2CA9F21AFFE038574340FB80C0000

and returns valid BASE64

H4sIAAAAAAAEAM1X227bOBB9X2D/geDTFkjFJH3ZBlKL1ImLoHVSVGnfaWnsEMuLyktgf1sf+kn7Czu6y1Ict+miKAIEIjVzZubwzFD+9+u3+PVGSXIP1gmjE3oSHVMCOjO50OuEBr96/jd9/erPP+LLXG3I59buRWmHntol9M774owxl92B4i5SIrPGmZWPMqMYzw07PT5+yU5OGCAERSxC4o9Be6GgWuByZnQGhQ9cLkwO0jX7+CatUMk1V+AKnkFCU64KCRfc8yV3EDXLJUJ42HhKzqXgmFUKckUJ19p47jHns08OUm+NXqcFbnB5uy0A7VZcOmhqOevNv7es49OyLNY7tlBZcN6oHwQ8edHwxMbuT2Kbdjwik5fIuN+WVVdsJnRuDCXjOGczaUubQzxHFZwAFyHMEdk1PuqEgnoq/47ILEgfLCQagrdcHpEPYSlF9g62t+Yf0IkOUg7TxYTx3c4Gbn2wpgDrtx9h1RRxlVPCdv3Y2LFzG/jUNb4NAp+vMTZfSujEwB51L/+3AKgobBRKFnzzHvTa3yUUHymZiw3k7U6D+kkL7Ct08jaMgsSsP53pmZWUc6HBNglMJD85RNR6c46uCbdbUY2bgu+l4Cjpc6jbJ6oU8lCeXUZ9o7K6U9uOZntaOl7wokDKBi3e7JC07u/Z8/THxa5qDJa5BzTfZdtF8sbyNYzeYmjMdC6s862QKZnlamI25n8Pt220AcVjPfeMt8bl884p7+u/8mxGcD2XcyxPgfZVpdBlVPf8xKsas1xy+0CfzIwMSu/rtce86zYZ+tc7U4SYjdIe08QmPI06fsz5Y5Idm3TRO+mOJBo3cjl8NU30U5tQgtTci7zSztZ5UFFpEKVf5EwKrLc3WHAtVuB8PRQp3ginoxvt97ldmHO5PHzF/PKhHrT4EgDZxGxWAuxPDXh9z212x+1fim+eDZF+dohPZ83BEb1vQtcqS2i+xMVtnV490Z84u6eCj9nwiy2+ACfWPUT5/aYhK5XUg7Y2V3plWoKxnGFGrcmI/wV4niMr5xYPkGceX2fgXHXRfuYyoMmlWkJ+pW+CL4I/dw7UUm6H9cbs8fjVBbWbc3xTlCv3f5SAaQosAW70myBk3uU9n+pvH0QpkLeA+1WH4ocGwq23HdK10d8J1NB3AQXosiluAW8SBHM3OuX38JTc8PviPax5tm3n1n6QwwexS3t8IfjacuUajN6//BXCyp8hr/4DhXQ0D7gMAAA=

4
  • Well which are you interested in - converting a hex string, or converting a byte array? Those are two different questions with different answers. (I suspect both have been asked before.) Convert.ToBase64String is what you're after, basically. It's not clear why you've tagged this sql-server and varbinary if you need to do it without SQL. Commented Aug 15, 2017 at 13:04
  • Duplicates: stackoverflow.com/questions/11809675 for byte array to base64; stackoverflow.com/questions/41963481 for hex to byte array Commented Aug 15, 2017 at 13:13
  • I was not sure what MSSQL returns me. LinqPad5 returns me already converted string. That's why I included MSSQL tag. It was for completeness of the question. Thanks for the links! Commented Aug 15, 2017 at 13:18
  • "I was not sure what MSSQL returns me." It sounds like you should be able to have checked that before asking the question though, at which point the SQL would have been irrelevant. "LinaPad5 returns me already converted string." What is LinaPad5 and what does it have to do with the question? If you mean LINQPad, you should be able to do research not in LINQPad, unless your final code really needs to be running in LINQPad. Commented Aug 15, 2017 at 13:19

2 Answers 2

3

Look at the Convert.ToBase64String() method.

Sign up to request clarification or add additional context in comments.

1 Comment

That is correct. ToBase64String works. I was trying with casting to byte[] and then encoding with different (UTF8, UTF16, ASCII), but completely forgot about this one. This answer will be accepted.
0

You can use C# method Convert.ToBase64String() for this. Pass the byte array to this method as parameter and it will return it's base64 string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.