473,509 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert a hex number (in a string variable) to a byte.

Hi,

I have a variable, a string that contains a hex number (max is FF).
I want to convert it to a byte value. Could some one let me know how
is it to be done. I have seen conversion of direct numbers but it does
not work in this case.
Pubudu
Jun 27 '08 #1
4 1456
On 2008-06-04, Pubs <pu******@gmail.comwrote:
Hi,

I have a variable, a string that contains a hex number (max is FF).
I want to convert it to a byte value. Could some one let me know how
is it to be done. I have seen conversion of direct numbers but it does
not work in this case.
Pubudu
see:
Convert.ToByte(string value, int fromBase)

byte b = Convert.ToByte ("FF", 16);

--
Tom Shelton
Jun 27 '08 #2
Pubs <pu******@gmail.comwrote:
I have a variable, a string that contains a hex number (max is FF).
I want to convert it to a byte value. Could some one let me know how
is it to be done. I have seen conversion of direct numbers but it does
not work in this case.
Convert.ToByte(text, 16);

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #3
Yet another way:

byte b = byte.Parse("FF",
System.Globalization.NumberStyles.HexNumber);

On Jun 4, 1:42*pm, Pubs <pubud...@gmail.comwrote:
Hi,

* *I have a variable, a string that contains a hex number (max is FF).
I want to convert it to a byte value. Could some one let me know how
is it to be done. I have seen conversion of direct numbers but it does
not work in this case.

Pubudu
Jun 27 '08 #4
On Jun 4, 3:19*pm, qglyirnyf...@mailinator.com wrote:
Yet another way:

byte b = byte.Parse("FF",
System.Globalization.NumberStyles.HexNumber);

On Jun 4, 1:42*pm, Pubs <pubud...@gmail.comwrote:
Hi,
* *I have a variable, a string that contains a hex number (max is FF).
I want to convert it to a byte value. Could some one let me know how
is it to be done. I have seen conversion of direct numbers but it does
not work in this case.
Pubudu- Hide quoted text -

- Show quoted text -
Hi all,

Thank you every body for the help. It works great now.

Pubudu
Jun 27 '08 #5

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

Similar topics

3
1708
by: Russell Mangel | last post by:
// Need help converting String to Byte // How do I get this String in a Byte Array String strBytes = "FFD9FFD8"; // Like this... Byte Bytes = {0xFF, 0xD9, 0xFF, 0xD8}; // This is no good,...
1
2637
by: Peter Schmitz | last post by:
Hi, is there any way to convert a managed System::String var to a BYTE pointer (BYTE*)? Thanks a lot Peter
16
16388
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
7
10803
by: Yama | last post by:
Hi, I have the following binary data: StringData = 800006000000; which is equivalent to the following: byte bytes = new byte; bytes = 0x80;
3
14376
by: Gordon Knote | last post by:
Hi I've got the following problem: In one of my programs I receive a string that contains a hex number (e.g. "B4"). Now, I want to convert this "number" into a var of type BYTE. How can this be...
2
2090
by: Mike Moore | last post by:
does anyone have an example of how to get the connection string object converted to a string variable type in order for me to call a function?
5
8906
by: EOS | last post by:
Hi, Thanks for this great forum and my convertion from Byte into String works beautifully with Encoding.ASCII.GetString() Anyone how to do the reverse, I mean convert a String into Byte? In...
1
6485
by: Vitaly Zayko | last post by:
How to convert bit string to byte and vice versa? For example: string bitstr = "11111111"; after conversion should get 0xFF -- Vit Zayko
9
3867
by: korea saeed | last post by:
how can i convert number string to number?(without using parsint)
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
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...
1
7067
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...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3215
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...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
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...

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.