473,801 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary 2 Hex

ron
Hi,
I am having some issues converting a binary string value
to a hex string value.

Example:
string mybinary = "00001111111110 00";
string myhex = Convert.ToStrin g(Convert.ToInt 32(mybinary,
2), 16);

returns: "FF8"
should return: "0FF8"

I always need it to return a 16 bit value how do i keep
it from dropping the "0" value's in the hex string?

Thanks,
Ron
Nov 15 '05 #1
6 34810
ron <goodr@no_spam_ 4me_mddm.panaso nic.com> wrote:
Thought of that, but what if i have the following values.

"11110000111100 00" or "00001111000000 00" and so on etc..
I need it to be a 16 bit value my string values are 16
bit and i need to look at all 4 positions of the hex
string.


So what would be wrong with left-padding with 0s? The only reason it's
coming up with FF8 instead of 0FF8 is because it's *not* left-padding
it automatically, and 0FF8 is the same as FF8. Left-pad the result to
the size you need (ie 4) and you should be fine.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
ron <goodr@no_spam_ 4me_mddm.panaso nic.com> wrote:
Thought of that, but what if i have the following values.

"11110000111100 00" or "00001111000000 00" and so on etc..
I need it to be a 16 bit value my string values are 16
bit and i need to look at all 4 positions of the hex
string.


So what would be wrong with left-padding with 0s? The only reason it's
coming up with FF8 instead of 0FF8 is because it's *not* left-padding
it automatically, and 0FF8 is the same as FF8. Left-pad the result to
the size you need (ie 4) and you should be fine.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
111100001111000 0 is 0xF0F0 and
000011110000000 0 is 0xF00
both cases would work just fine if you left-pad it with zeroes., I don't
mean you always pad it with one zero, just left-pad with (4-strlen(str))n
zeroes and it should work fine.

"ron" <goodr@no_spam_ 4me_mddm.panaso nic.com> wrote in message
news:11******** *************** *****@phx.gbl.. .
Thought of that, but what if i have the following values.

"11110000111100 00" or "00001111000000 00" and so on etc..
I need it to be a 16 bit value my string values are 16
bit and i need to look at all 4 positions of the hex
string.

Ron

-----Original Message-----
Uhm, just left-pad it with zeroes?

"ron" <goodr@no_spam_ 4me_mddm.panaso nic.com> wrote in

message
news:0b******* *************** ******@phx.gbl. ..
Hi,
I am having some issues converting a binary string value to a hex string value.

Example:
string mybinary = "00001111111110 00";
string myhex = Convert.ToStrin g(Convert.ToInt 32 (mybinary, 2), 16);

returns: "FF8"
should return: "0FF8"

I always need it to return a 16 bit value how do i keep
it from dropping the "0" value's in the hex string?

Thanks,
Ron

.

Nov 15 '05 #4
111100001111000 0 is 0xF0F0 and
000011110000000 0 is 0xF00
both cases would work just fine if you left-pad it with zeroes., I don't
mean you always pad it with one zero, just left-pad with (4-strlen(str))n
zeroes and it should work fine.

"ron" <goodr@no_spam_ 4me_mddm.panaso nic.com> wrote in message
news:11******** *************** *****@phx.gbl.. .
Thought of that, but what if i have the following values.

"11110000111100 00" or "00001111000000 00" and so on etc..
I need it to be a 16 bit value my string values are 16
bit and i need to look at all 4 positions of the hex
string.

Ron

-----Original Message-----
Uhm, just left-pad it with zeroes?

"ron" <goodr@no_spam_ 4me_mddm.panaso nic.com> wrote in

message
news:0b******* *************** ******@phx.gbl. ..
Hi,
I am having some issues converting a binary string value to a hex string value.

Example:
string mybinary = "00001111111110 00";
string myhex = Convert.ToStrin g(Convert.ToInt 32 (mybinary, 2), 16);

returns: "FF8"
should return: "0FF8"

I always need it to return a 16 bit value how do i keep
it from dropping the "0" value's in the hex string?

Thanks,
Ron

.

Nov 15 '05 #5
Daniel Gustafsson <da****@phoswor ks.se> wrote:
111100001111000 0 is 0xF0F0 and
000011110000000 0 is 0xF00
both cases would work just fine if you left-pad it with zeroes., I don't
mean you always pad it with one zero, just left-pad with (4-strlen(str))n
zeroes and it should work fine.


Or do it slightly more simply:

string myHex = String.Format ("{0:X4}", Convert.ToInt32 (myBinary, 2));

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
Daniel Gustafsson <da****@phoswor ks.se> wrote:
111100001111000 0 is 0xF0F0 and
000011110000000 0 is 0xF00
both cases would work just fine if you left-pad it with zeroes., I don't
mean you always pad it with one zero, just left-pad with (4-strlen(str))n
zeroes and it should work fine.


Or do it slightly more simply:

string myHex = String.Format ("{0:X4}", Convert.ToInt32 (myBinary, 2));

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #7

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

Similar topics

13
15271
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large, maybe 32 kbytes is all and the need for speed is not so great, but the need for accuracy in the...
20
7568
by: Christian Stigen Larsen | last post by:
A signed int reserves one bit to signify whether a number is positive or negative. In light of this, a colleague asked me whether there existed an int in C++ that was -0, a zero with the negative bit set. I was intrigued by this, so I tried the following code: #include <stdio.h> int main(int, char**) { int a(-0); printf("a=%d\n", a);
3
3498
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is possible to change the behavior of operator << so it will stream numeric values as their actual values when an ofstream is in binary mode. I did not, however, find any information on how this can be accomplished. What is involved in getting this...
103
48771
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
2
2535
by: Lisa Pearlson | last post by:
Hi, My php application (on Apache/Linux) needs to do the following: The PHP script receives a request from a client (binary), asking for certain records of data. My PHP script loops through all records and sends each of them ONE BY ONE. After each record that my server script sends, it waits for the client to confirm proper reception with an ACK (binary digit). When there are no more records, my server script sends the client a binary
9
6523
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the delta binary is pretty huge in size. I have 1 byte file and 2 bytes file, the delta should be 1 byte but somehow it turns out to be 249 bytes using binary formatter. I guess serialization has some other things added to the delta file.
3
2128
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write 23 doubles to it with fwrite, one call for each double. Then I close the file using fclose. After three times around the loop in the debugger, I stop the program (using "Stop debugging"). That is writing 552 bytes. The resulting file's properties...
4
3696
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the following code that compiles OK. It runs and shows the requested output. However, after execution, it pops one of those windows to send error reports online to the porgram creator. I have managed to find where the error is but can't see what's wrong....
10
22760
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app) outFile.write("teststring", 10); outFile.close(); If I leave out the ios::ate and ios::app modes my string is written to the start of the file as I'd expect but I want to write the data to
16
4502
by: Erwin Moller | last post by:
Why is a binary file executable? Is any binary file executable? Is only binary file executable? Are all executable files binary? What is the connection between the attribute of binary and that of executable?
0
9698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10300
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10279
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9106
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7595
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4267
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2963
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.