473,326 Members | 2,133 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,326 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 34776
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.