473,401 Members | 2,146 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,401 software developers and data experts.

C# "Hex" function equiv...

I need to convert single bytes to their Hex equivalent in C#. This means a 2 character representation in a string that is the hex number. This is done with the Hex function in VB to which there appears to be no equivalent in C#.

So if I had an array of bytes and I was going through a loop (lets say byte[] b = new byte[4])

... Fill the byte array with various values

and when doing something like

for(int i=0; i<b.Length; i++)
// convert it here

End up with...

"0a ff 00 1b"

depending on what the values were that I loaded into the byte array.

Anyone have some code to do this?

The VB code I'm converting is:

Public Function HexString(ByVal b() As Byte, ByVal l As Integer, ByVal offset As Integer) As String
Dim i As Integer
If l = 0 Then l = b.Length
Dim str As String = ""
For i = offset To l - 1 + offset
str = str & " " & Hex(b(i))
Next
Return str
End Function

Thanks,
D
Mar 1 '07 #1
1 7504
Brahahahhs..

byte[] b = new byte[4];

String feh = b[i].ToString("X2");

this seems to work great.
Mar 1 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Grant Edwards | last post by:
I'm getting tired of seeing meaningless warnings from my code, but I can't figure out how to get rid of them: For example: fcntl.ioctl(fd,0xc0047a80,s) causes FutureWarning: hex/oct...
7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
2
by: Generic Usenet Account | last post by:
What exactly is the difference between the hex manipulator and the following statement: cout.setf(ios_base::hex)? According to Stroustrup, Third Edition, Section 21.4.4, "once set, a base is...
4
by: Ping | last post by:
Hi, All, We can use BitConverter.ToString(byte) to a string, but how to get the byte from a string like "AD-A6-0D-1F"?
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
2
by: Zach.Baier | last post by:
I have an document created with the XML writer that is having issues when loaded to a mainframe due to the fact the it is inserting a "." or "00" between each carachter when viewed as hex. Any...
1
by: Sergei Shelukhin | last post by:
Hi. I have a string, 16 bytes long, representing a hex number. I need to convert it to a number and put it into pgsql database; it complicates the matters further cause pgsql doesn't support...
3
by: Slaunger | last post by:
I know there must be a simple method to do this. I have implemented this function for calculating a checksum based on a ones complement addition: def complement_ones_checksum(ints): """...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
0
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
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...

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.