473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Convert UInt16 to binary

Hi group,

Is there an easy way to convert a UInt16 value in to a string which presents
it in a binary format. I need a conversion with a fixed length.

so 6 must be presented as '0000000000000110'

Any help or code examples would be higly appreciated.

Thanks a lot in advance,

Bart

Nov 4 '08 #1
5 5400
bart wrote:
Is there an easy way to convert a UInt16 value in to a string which
presents it in a binary format. I need a conversion with a fixed length.

so 6 must be presented as '0000000000000110'

Any help or code examples would be higly appreciated.
Try:

Convert.ToString(v, 2).PadLeft(16, '0')

Arne
Nov 4 '08 #2
I agree with Arne, if you are trying to get a string representation? Are you
trying to get a byte[2] instead?

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
<bartwrote in message
news:3E**********************************@microsof t.com...
Hi group,

Is there an easy way to convert a UInt16 value in to a string which
presents it in a binary format. I need a conversion with a fixed length.

so 6 must be presented as '0000000000000110'

Any help or code examples would be higly appreciated.

Thanks a lot in advance,

Bart
Nov 4 '08 #3
Arne Vajhøj wrote:
bart wrote:
>Is there an easy way to convert a UInt16 value in to a string which
presents it in a binary format. I need a conversion with a fixed length.

so 6 must be presented as '0000000000000110'

Any help or code examples would be higly appreciated.

Try:

Convert.ToString(v, 2).PadLeft(16, '0')
Boooo-ring. These may be more acceptable for the LINQ era:

new string(Enumerable.Range(0, 16).Reverse().Select(i ="01"[v >i &
1]).ToArray())

Or

new string(Enumerable.Repeat(v, 16).Select((x, y) ="01"[x >y &
1]).Reverse().ToArray())

Or

Enumerable.Repeat(v, 16).Aggregate("", (x, y) ="01"[y >x.Length & 1] + x)

(I'm kidding, don't use these.)

--
J.
Nov 4 '08 #4
Aha...PadLef......so simple
Thanks a lot :)

"Arne Vajhøj" <ar**@vajhoej.dkschreef in bericht
news:49***********************@news.sunsite.dk...
bart wrote:
>Is there an easy way to convert a UInt16 value in to a string which
presents it in a binary format. I need a conversion with a fixed length.

so 6 must be presented as '0000000000000110'

Any help or code examples would be higly appreciated.

Try:

Convert.ToString(v, 2).PadLeft(16, '0')

Arne
Nov 4 '08 #5
bart wrote:
"Arne Vajhøj" <ar**@vajhoej.dkschreef in bericht
news:49***********************@news.sunsite.dk...
>bart wrote:
>>Is there an easy way to convert a UInt16 value in to a string which
presents it in a binary format. I need a conversion with a fixed length.

so 6 must be presented as '0000000000000110'

Any help or code examples would be higly appreciated.

Try:

Convert.ToString(v, 2).PadLeft(16, '0')
Aha...PadLef......so simple
Simple is good in programming !

If you need to obfuscate the code, then Jeroen had
some ideas.

Arne
Nov 4 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Norman | last post by:
I have a collection that can have a reference to various types of arrays. String was easy: ... string sPropertyArray = (string)oReturn; foreach( string sItem in sPropertyArray ) { ...
2
by: jiangyh | last post by:
hi there : I have a question about how to convert Type to DbType? thanks a lot. jiangyh
1
by: rawCoder | last post by:
Hi, Ok this shud b simple but i cant seem to find it. I want to convert a System.UInt16 data to 2 Byte array and System.UInt32 data to 4 Byte array ( BigEndian) something like Public Sub...
2
by: Howard Weiss | last post by:
I am reading a file (containing short integers). To read the file, I use the following FileStream *myFile = new FileStream(FileName, FileMode::Open, FileAccess::Read); __int64 myFileSize =...
6
by: Glenn Wilson | last post by:
I have converted most of the code that I have but am having trouble, mainly with the marked lines. (>>) public static UInt16 checksum( UInt16 buffer, int size ) { Int32 cksum = 0; int counter;...
5
by: Sakharam Phapale | last post by:
Hi All, I am using Microsoft Speech DLL "sapi.dll". I want to use following method of "SpeechLib.SpStream" object (following is a signature which I copied from Quick Info feature of .NET code...
5
by: Atara | last post by:
I am trying to convert the following code to VB .Net, I still have some gaps (the lines that are marked with (*)) and also I need an ending condition for the while loop. any help would be...
8
by: Prakash | last post by:
Hi Friends, Using Remoting concept, i have send WCHAR values from VC++.net applicaiton to C# applicaiton. In my C# application it is treated as unsigned shot. I need to convert it back to string...
3
by: bussiere maillist | last post by:
i've got a very long string and i wanted to convert it in binary like string = """Monty Python, or The Pythons, is the collective name of the creators of Monty Python's Flying Circus, a British...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.