Hi,
I have the following binary data:
StringData = 800006000000;
which is equivalent to the following:
byte[] bytes = new byte[6];
bytes[0] = 0x80;
bytes[1] = 0x00;
bytes[2] = 0x06;
bytes[3] = 0x00;
bytes[4] = 0x00;
bytes[5] = 0x00;
How can I parse the string StringData to the equivalent byte array above?
I tried the following:
byte[] bytes = Encoding.ASCII. GetBytes(String Data);
but it gave me back garbage data.
This bytes[0] = 0x80; is equivalent to bytes[0] = 128;
Any help would be appreciated.
Thanks,
Yama 7 10811
Yama <Ya**@discussio ns.microsoft.co m> wrote: I have the following binary data:
StringData = 800006000000;
which is equivalent to the following:
byte[] bytes = new byte[6]; bytes[0] = 0x80; bytes[1] = 0x00; bytes[2] = 0x06; bytes[3] = 0x00; bytes[4] = 0x00; bytes[5] = 0x00;
How can I parse the string StringData to the equivalent byte array above?
I tried the following: byte[] bytes = Encoding.ASCII. GetBytes(String Data);
but it gave me back garbage data.
No, it gave you back exactly what you asked for - but that's not what
you wanted.
I suggest you call byte.Parse repeatedly with 2-character substrings,
specifying NumberStyles.Al lowHexSpecifier .
Alternatively, you could use something like this:
static int ParseHexDigit(c har c)
{
if (c >= '0' && c <= '9')
{
return c-'0';
}
if (c >= 'a' && c <= 'f')
{
return c-'a'+10;
}
if (c >= 'A' && c <= 'F')
{
return c-'A'+10;
}
throw new ArgumentExcepti on ("Invalid hex character");
}
public static string ParseHex(string hex)
{
char[] result = new char[hex.Length/2];
int hexIndex=0;
for (int i=0; i < result.Length; i++)
{
result[i] = (char)(ParseHex Digit(hex[hexIndex++])*16+
ParseHexDigit(h ex[hexIndex++]));
}
return new string (result);
}
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hi Yama,
You can convert from a string to a byte array using the
Convert.FromBas e64String method.
Cheers,
Steve Goodyear
Vancouver, Canada
Your problem description still isn't 100% clear, but here's what I'm
assuming.
You have a string of digits (and possibly letters A-F or a-f) that
represent hexadecimal nibble values:
string StringData = "8000060000 00";
You want to translate this into a byte array, where each character from
the string becomes half of a byte (a nibble).
I don't think that there are any built-in classes in .NET to do this
for you. You'll have to do it yourself:
byte[] bytes = new byte[StringData.Leng th / 2];
for (int i = 0; i < StringData.Leng th - 1; i += 2)
{
int j = i / 2;
byte[j] = (ToNibble(Strin gData[i]) << 4) + ToNibble(String Data[i +
1];
}
private byte ToNibble(char c)
{
if ('0' <= c && c <= '9')
{
return c - '0';
}
else if ('a' <= c && c <= 'f')
{
return c - 'f';
}
else if ('A' <= c && c <= 'F')
{
return c - 'F';
}
else
{
throw new ArgumentExcepti on(String.Forma t("Character '{0}'
cannot be translated to a hexadecimal value because it is not one of
0,1,2,3,4,5,6,7 ,8,9,a,b,c,d,e, f,A,B,C,D,E,F", c));
}
}
That does not work. I get an exception telling me Invalid length for a
Base-64 string.
"Steve Goodyear" wrote: Hi Yama,
You can convert from a string to a byte array using the Convert.FromBas e64String method.
Cheers, Steve Goodyear Vancouver, Canada
Thanks to all my problem has been resolved with your help!
"Bruce Wood" wrote: Your problem description still isn't 100% clear, but here's what I'm assuming.
You have a string of digits (and possibly letters A-F or a-f) that represent hexadecimal nibble values:
string StringData = "8000060000 00";
You want to translate this into a byte array, where each character from the string becomes half of a byte (a nibble).
I don't think that there are any built-in classes in .NET to do this for you. You'll have to do it yourself:
byte[] bytes = new byte[StringData.Leng th / 2]; for (int i = 0; i < StringData.Leng th - 1; i += 2) { int j = i / 2; byte[j] = (ToNibble(Strin gData[i]) << 4) + ToNibble(String Data[i + 1]; }
private byte ToNibble(char c) { if ('0' <= c && c <= '9') { return c - '0'; } else if ('a' <= c && c <= 'f') { return c - 'f'; } else if ('A' <= c && c <= 'F') { return c - 'F'; } else { throw new ArgumentExcepti on(String.Forma t("Character '{0}' cannot be translated to a hexadecimal value because it is not one of 0,1,2,3,4,5,6,7 ,8,9,a,b,c,d,e, f,A,B,C,D,E,F", c)); } }
Bugs in my code sample:
The line
return c - 'f';
should read
return c - 'a';
The line
return c - 'F';
should read
return c - 'A';
Bugs in my posted code. The line
return c - 'f';
should read
return c - 'a' + 10;
The line
return c - 'F'
should read
return c - 'A' + 10; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Nathan |
last post by:
Is there a way to convert a string to a CipherMessage? I am calling a
function that decrypts a CipherMessage and returns the value. The only
problem is when I want to use an encrypted value stored in a querystring, I
can't figure out how to convert it back to a CipherMessage.
|
by: Swarup |
last post by:
I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling
it into a byte arry. Now i have to convert it into a string to store it in
the database.
I use
System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
now i am using enc.GetString(value) and the value retured is one byte less
if the size of the byte...
|
by: Ricardo Quintanilla |
last post by:
i have a code that sends data to a socket listening over as400 platform,
the socket responds to me as a "byte array".
then i need to convert the "byte array" into a string.
the problem is that the data converted from the byte array into a string
, is not what i expect.
example:
- the data returned from the socket is (byte array)...
|
by: dale zhang |
last post by:
Hi,
I am trying to save and read an image from MS Access DB based on the
following article:
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp
Right now, I saved images without any errors. After reading the ole object
from db, I saved it to C: as file1.bmp and displayed on the web. But it can
not be displayed. After I manually sent...
|
by: Charles Law |
last post by:
I thought this was going to be straight forward, given the wealth of
conversion functions in .NET, but it is proving more convoluted than
imagined.
Given the following
<code>
Dim ba(1) As Byte
Dim b As Byte
| |
by: moondaddy |
last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a
string, and then from a string back to a byte array.
for example
Private mByte() as New Byte(4){11,22,33,44}
Now how do I convert it to:
dim myStr as string = "11,22,33,44"
|
by: Bob Altman |
last post by:
Hi all,
I'm looking for the fastest way to convert an array of bytes to String. I
also need to convert a String back to its original Byte() representation.
Convert.ToBase64String and Convert.FromBase64String seem like the closest
thing I can find to what I'm looking for baked into the base class library.
Can anyone suggest a better way...
|
by: Peter |
last post by:
Trying to convert string to byte array.
the following code returns byte array of {107, 62, 194, 139, 64}
how can I convert this string to a byte array of {107, 62, 139, 65}
System.Text.UTF8Encoding str = new System.Text.UTF8Encoding();
string s = new string((char)107, 1);
s += new string((char)62, 1);
|
by: est |
last post by:
From python manual
str( )
Return a string containing a nicely printable representation of an
object. For strings, this returns the string itself. The difference
with repr(object) is that str(object) does not always attempt to
return a string that is acceptable to eval(); its goal is to return a
printable string. If no argument is given,...
|
by: Debadatta Mishra |
last post by:
Introduction
In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not...
|
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...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |