Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6772047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:30:12+00:00 2026-05-26T15:30:12+00:00

The SQL Server convert () function can convert varbinary data to a string with

  • 0

The SQL Server convert() function can convert varbinary data to a string with this encoding:

Each binary character is converted into two hexadecimal characters. If
the length of the converted expression is greater than the data_type
length it will be right truncated.

If the data_type is a fix sized character type and the length of the
converted result is less than its length of the data_type; spaces are
added to the right of the converted expression to maintain an even
number of hexadecimal digits.

The characters 0x will be added to the left of the converted result
for style 1.

For example, output might look like ‘0x389D7156C27AA70F15DD3105484A8461A2268284’. How can I easily do the same thing in C#? i.e. convert a byte[] to a string using the same sort of encoding?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:30:13+00:00Added an answer on May 26, 2026 at 3:30 pm

    You can use BitConverter.ToString() and drop the hyphens it uses as a separator:

    "0x" + BitConverter.ToString(bytes).Replace("-", "")
    

    Or you could use LINQ and string.Concat(). The .Net 4 version:

    "0x" + string.Concat(bytes.Select(b => b.ToString("X2")))
    

    In .Net 3.5, you have to add ToArray():

    "0x" + string.Concat(bytes.Select(b => b.ToString("X2")).ToArray())
    

    This doesn’t follow the specification you have with regards to truncating and adding spaces, but I’m not sure you need that. And it should be easy to modify the code to do that.

    Both of these versions go for readability first and performance second. If you need this to be very fast, you could use StringBuilder and add the formatted bytes manually to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert some Geometry data into Geography data in MS SQL Server
How can I convert a database from MySQL to MS SQL Server 2005?
how to convert a binary(128) from little endian to big endian in SQL Server?
select convert(varbinary(8), 1) in MS SQL Server produces output : 0x00000001 On assigning the
I have an Interbase DB. How can I convert it to SQL Server?
I'm looking for an efficient SQL Server function (in my case 2005) to convert
In SQL server you can use the DATENAME function to get the day of
I have a SQL Server function which converts a nvarchar day duration setting into
I have been writing a user-defined function in SQL Server: It looks like this
I'd like to be able to convert SQL Server query plans from XML to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.