473,387 Members | 1,516 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,387 software developers and data experts.

Binary 2 Hex

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

Example:
string mybinary = "0000111111111000";
string myhex = Convert.ToString(Convert.ToInt32(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 34780
ron <goodr@no_spam_4me_mddm.panasonic.com> wrote:
Thought of that, but what if i have the following values.

"1111000011110000" or "0000111100000000" 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.com>
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.panasonic.com> wrote:
Thought of that, but what if i have the following values.

"1111000011110000" or "0000111100000000" 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.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
1111000011110000 is 0xF0F0 and
0000111100000000 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.panasonic.com> wrote in message
news:11****************************@phx.gbl...
Thought of that, but what if i have the following values.

"1111000011110000" or "0000111100000000" 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.panasonic.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 = "0000111111111000";
string myhex = Convert.ToString(Convert.ToInt32 (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
1111000011110000 is 0xF0F0 and
0000111100000000 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.panasonic.com> wrote in message
news:11****************************@phx.gbl...
Thought of that, but what if i have the following values.

"1111000011110000" or "0000111100000000" 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.panasonic.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 = "0000111111111000";
string myhex = Convert.ToString(Convert.ToInt32 (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****@phosworks.se> wrote:
1111000011110000 is 0xF0F0 and
0000111100000000 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.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
Daniel Gustafsson <da****@phosworks.se> wrote:
1111000011110000 is 0xF0F0 and
0000111100000000 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.com>
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
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...
20
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...
3
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...
103
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...
2
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...
9
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...
3
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...
4
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...
10
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)...
16
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.