I have a String like this:
String data = "0x0f";
and I would like to leave it as a single byte which represents this hex. Does anyone know what function I can use?
I tried using the following function:
public byte pegarValorHexText(string text)
{
int NumberChars = text.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(text.Substring(i, 2), 16);
return bytes[0];
}
The string has a hexadecimal value, it has a maximum of 4 characters, the sequence "0x" indicating that it is a hexadecimal value and a sequence of two hex chars
Just use Convert.ToByte then, it should handle the hex prefix:
byte b = Convert.ToByte("0x0f", 16); // 15
Related
I'm trying to convert an unHex value to a string but it's not working.
I have the following value 0x01BB92E7F716F55B144768FCB2EA40187AE6CF6B2E52A64F7331D0539507441F7D770112510D679F0B310116B0D709E049A19467672FFA532A7C30DFB72
Result I hope would be this
but executing the function below displays this result
»’ Ç ÷ õ [Ghü²ê # zæÏk.R¦Os1ÐS • D} w Q gŸ 1 ° × àI¡ ”gg / úS * | 0ß ·) = ¤
Any idea how I can extract the information as expected
public static string Hex2String (string input)
{
var builder = new StringBuilder ();
for (int i = 0; i < socketLength; i + = 2)
{
// throws an exception if not properly formatted
string hexdec = input.Substring (i, 2);
int number = Int32.Parse (hexdec, NumberStyles.HexNumber);
char charToAdd = (char) number;
builder.Append (charToAdd);
}
return builder.ToString ();
}
Your result is base64-encoded. Base64 is a way of taking a byte array and turning it into human-readable characters.
Your code tries to take these raw bytes and cast them to chars, but not all byte values are valid printable characters: some are control characters, some can't be printed, etc.
Instead, let's turn the hex string into a byte array, and then turn that byte array into a base64 string.
string input = "01BB92E7F716F55B144768FCB2EA40187AE6CF6B2E52A64F7331D0539507441F7D770112510D679F0B310116B0D709E049A19467672FFA532A7C30DFB72";
byte[] bytes = new byte[input.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = byte.Parse(input.Substring(i * 2, 2), NumberStyles.HexNumber);
}
string result = Convert.ToBase64String(bytes);
This results in:
AbuS5/cW9VsUR2j8supAGHrmz2suUqZPczHQU5UHRB99dwESUQ1nnwsxARaw1wngSaGUZ2cv+lMqfDDftw==
See it running here.
I have a string of hex that I want to convert to UTF-16L, as specified at https://en.wikipedia.org/wiki/GUID_Partition_Table under "Partition entries (LBA 2–33)". The string with hex has a fixed length of 72 bytes. I'm not sure what to do to convert it. I was thinking converting it to byte first then use Encoding.BigEndianUnicode Property.
Also when I tried to use Encoding.UTF8.GetChars then I got a lot of spaces in my result.
static void Main(string[] args)
{
string hexString = "4200610073006900630020006400610074006100200070006100720074006900740069006F006E000000000000000000000000000000000000000000000000000000000000000000";
int length = hexString.Length;
byte[] bytes = new byte[length / 2];
for (int i = 0; i < length; i += 2){
bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
}
char[] chars = Encoding.UTF8.GetChars(bytes);
string s = new string(chars);
Console.WriteLine(s);
}
Prints this:
B a s i c d a t a p a r t i t i o n
(B\0a\0s\0i\0c\0 \0d\0a\0t\0a\0 \0p\0a\0r\0t\0i\0t\0i\0o\0n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0)
Bit of a weird one here but I'm trying to write a control program for a monitor that uses a serial RS232 port to receive commands in hexadecimal, 0xFF, notation i.e. 0x00.
Problem I have is that I can't seem to find a way of taking a user input, which is a decimal value like 55, and converting it into its byte, hexadecimal, form with the above mentioned format. Using the Convert method that Visual studio has built in gives me the correct value i need but without the required 0x at the beginning.
I'm new to using bytes and byte arrays in C# so forgive me if I've missed out on a simple formatting method that will solve it.
Below is a string to byte array method that I found on here and its helpful but gives me the wrong format for the bytes.
private static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i+2, 2), 16);
return bytes;
}
You might just want to convert a hex-string to bytes. See How do you convert a byte array to a hexadecimal string, and vice versa?
simple conversion of one single "hex-string-byte" and send the byte:
MonPort.Write(Convert.ToByte("A6"), 0,1);
Convert content of your textbox to a binary array and send it:
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
public void SendData()
{
byte[] sendBytes = StringToByteArray(YourSendBytesTextbox.Text);
MonPort.Write(sendBytes, 0, sendBytes.Length);
}
See following :
string input = "abcdefghijk";
byte[] data = string.Join(",",input.AsEnumerable()
.Select(x => "0x" + ((byte)x).ToString("X2")))
.Select(x => (byte)x).ToArray();
string str = Encoding.UTF8.GetString(data);
I am trying to convert a hex string to a string. But when I do it the result is gibberish. I also tried converting hex to byte array to string but that didn't work as well. I was told the first 2 bytes are just "headers" but I wanted to see what they look like as well
string = "01012121b769010006583b001f8b0800000000000400ed9d6942eb381280afe203ccf0b45af6fc0b59204d3090e541fafe0799aab2e5555ee34000b9bbddc6b1ac5295964fa5c59c3326d56275d833c636f01f871bf0af1126644cc315de5362b1d86ce909cea2e36b10312e02c178040fb2c5711704ff6c9e8280332db8c440e7440a16c00197c1e1703ebc044182c739e1329236e87d1840101624810e18fc909cd781eb783d27f65187344cb028978604d96d0f4120a4d09c73cd384823452a4d42d2042e69b88d4d073c608ab3566992c45ebaa4512ad70d27415ef667d08d908c6bad41844237556992b34b1a1ebc63347a9a3417594a8b5bb25421cd6c96924e4b291024e2a9a578dc622987345f68a950de92a50a69e6b354e4b254a8404ed653a61cd27ca1a5cc4d9529337f99524e4b459c81727aca94439aa99612a9a5380fab96ba7f8108590c9612284d97a5b8a9584ac621586a09d184b2c752f8a8431ad0406129128414c423c558c87a2c554893eb66899692d144dd4099520e4bb1082140f4942987345f682911453764a9429ad92ca598cb52b1d61c94d25da61cd27ca1a594baa5325548339fa5a6b39f439a2fb4948e6ec952bac9c517d77e17b0df7c943e83a5c2f8962c5548339fa52e60bfa634532d25419a278843d6d86f7b38026d09a02d90a85b37a0bdba34103e60a16995e6ad531a1e8938d70d09b25c3d401263c184e8d38d439a2d5a0a9232561a60bfc539504cabaa6e5ef62b904673d40dc75cbc363a9526cdc5ab5c37cc182b0d7b7e4ba3480289d1841d965a2c022975e492461b5394291224d30db9352017278989a2b234814b1a0e5140e5cd4016b1602a6ae7e273621fade7e2ed7d6038a4dca11ba63417c85ce764b9372569cea512ce54ae1b4c30c097d54d6b095f9034f4a8439ad814ec374837e7c025cd60dd2c5a7403b978b181fc1ff1aa6e52690c3c6fb04c8134e6dd958bc1521afb45f55c2cd6787f42094fa5891dd2c01d8d49ff6469a48eb54b1a210df535419abcbe69e462c2c3549ad50e1e8854260def6a19eca32e6942eeb494160a6a934c1ad92a4d5895465869da4bb89546344ab89572a06e9a96e28d96a14f371db59f9572a06e1cd2842dd2b4eaa65b9a487333b44c352d55e4625b3cfb72f11b94f0ec519734a171e9c659a6faa589874b13376abf279086e95a995a1ddf89fd40698cfc37cf7185b61e0a69c228af8bd1e83113c121583f33dede6a66b9181f754813f2b0285324486aa908ee208936cbd4d9254d5aa6e2086401dd2863faca541c354bf83230a083aa6ecee77f2917836e946cb4e149d95225dd14f1f14edf5607df80342c8af29e2f2341b2760a983852ba5117b74853623f6ca7646b3bd551a64e0ba074168fd1cdf942dd20df6416714923b418aa9b8e5cccad9afb74837c933d5acfc5bb47ecf9d6cad466bf404ac7fa86675cdc56fb392c4524aae568124da5e14c147d061284740335b46671e6854ce36a72b13bdf8034cc8cce376909d7328e5df58d910cac65faea1b5daa6f84d6d2d6373d259c1e754883ada3a3be111cd94f67f9a6421467973454174b636c7dd3996fb247dd638dda706802146b196b849c25adbc86ba7ed4096406cc188baab7bdd9e352b65d35569e772a757aaa7f5b14edaa2141a8838c9a4b7d719dfdbfa6343c5853d9993812c14bd2902094bd0026388f7958e9ff35fc062569d0196062033df504a269ef8d667e037cd46d29e3b254085563c47a2cc5c52d59ca21cd175aaa24cda5961299df40e6edaaa1e24fce03f2fd0b9959eabe45372cef1b9774b3457d4fa91f539f4a5e23191224d58d821a29d6a97fbbbd4c35a5d9a26e4c38a5ed487d2a9143373cc61aaaa725a361ae346834bf6ea251ba49ce2e69a6ea465a9f8aaeea26f56240f2b8e2e4a76cf7a944a1ed7199d41980c4a1a195ef2853d66fc05dd2a01fa254dfacac6e8c84464a4ad44dbbbfa9248df537492a53bc93c7d0df54f79a2af2a9285eb4f2a96e9eb748401cea1b4822e9a6d2ae967c2a00fa6159375065826ee0491df7f954f05197342c2ca42141ac6e903978c5bf5df737d5a401864393a26e04ef96861e9d593750c2e7d68d14c2a11b011958484dba31b2e2fd7a69934608a507ea861ead48039c7e0f524a5e2b53bbc73d96702c53b2bbd58420ba5efb5199621d7eca96129e492344a9d54441b2768a615542e46c8c192e8dc6da4feb29ad26f99b44ad9d226958ccb1bf2dd2daafd5df14091b3475cf40be59a33491e8f53749973440bf91431a9cd6407e15d6e9190c9b962269daebe28e7111f2a9b070a8344ddd14d2e409eb9126f7a9442e6952f50eb354533785a5aaba69b75447ab49de2fa65cba41d7b61aad1b315c37a2d94e519f9957a549fd0692e1c8084b6bbf362f86e10dbe110b9ca1d33e9ed6ed53315a15f98604c94ab8827be91875bb87c7210da768c6f7d455e653d17aa86e1a7de39234b99a7b74937b31b8531a5d6a1906e8e67ca16eac8747858dba98bc18ba966fc895c10cb498b1ed4f39bd18284db3bec9bc18a32da5ad87a72853244856a644d64e759570a73438f2399e8b73dd48976e429abc9c8e98bbc7ef89fd1c96fa32dd38a599a69bbc9d12436bbf6bb6533cf33731e92ce1204de82ce1257f93d1ac5c174b0d55d71a1a674c448fbf49872e6922a05f5709371ac7ccc9528697c7612bf54d210d34323c8a05f419b6184d2bfba544418f3a7371c96352e46290112252dd251c0dd9928b2796a9722e8e46e5629c13c3dc3ddf09b958505d1c6b1e0eadfd9a962a72b1ad48fb4a3858ca3eea90268a4a3e8a1edd345b868a34a4e63edda4d2d0a38e121ec591936f309a98c98a57c0c1c5b2948b01dcb4a5adfe5161ddc27e7aa8340ef693eefaa65d9a72bea97b9415d6fd52e1541c9935d45a3c6cf68fd913682a9646dd7e3016cb87cdf6908541370bcfe67d57dcf834db9a72627ad819e0c7207f135670d063b47f3fbf9ea1170502e522d3cbf11ffb66fbff8fe3e9707fd8b25c07fc7559d642e68de26b8a80d974e1614356de003a336615c55c489cfd11947f647718bc9168018976cf2e2737587ad889e6a544833452e2ecb3be448be989c608ae92680989764fe32c5b3a9bd1594a344a13736853fa129dcf3bc84799da13cd4d2aa5b5749c0e505f98681e855024b6995ccae6c0735626b3e949e720373ca8a478093c8d4224a7a5c82ef158ab356642be141b712e0a175d417d5032a10d618f5ae986b8d22359ac30528c844582e9778cba612e45e6728e10156ab28345a7c25c6aa8b9947db3fdffc769481e15c2612e1bf2d465ae537f1ed558309dc31bd5da88463a4a89c6ba113a32f689f644ebe989c608ae92e8301b46698ea294139d0da894128d936f6265faabe0b09ee8a03dd142695b30534bab392ced2e98c2164ccadba9af32931054722a154c81bed4b460d2a5a3605a45cd5530595a30056af4e18d15ad2c48fe50a9de65dac29f0eabf4f5d415295d8a87fbd7f44f81632dc59f218f0d1335692aedf7196be4c5191f8e408f27cc362284ab8f05bdef2e621f28179377d0c6d07368bef3922287abd723497c27d919231502ee6df664913bc5b6ffdae7de3e286bc25bce9056a8c2ef70603382acfa70ccd2ce33ab31905f9412d038ea7741bfe7bc3485ec61f36a3302d90f949e3d48b683ff4a773225a4116b08fbfc5c17875141a0bf368bfd3ad8af0fc760f1faba3ba7096884807f64660fb65f1cd760b9f52ad826c1faed141c5f8e8b5d00d5cefd611f70b069f05fc8c206ca8188c8705abb85b089871b41f21732d13fa7dd9fe4ef822e860413d382490ac66d303e3098a260c606330383e969c1425730239cc6b1e6146cb7004baed042c797e0f5b45f3e2e0e70bd5d3ead8fff0b48431ccb69e87a4fde80432b4f21b6c943b0785827cb335556984fdde1b80db75a6f16a7dd3178dd2d28f472b1df6fd77ba830407877ce12910d4c7911040e168df09843f7dbe571fb0244ae62f78b722dacff7b7c3a4298b7d376bf5ee16ccc4e6d03c2af565b7c37e4e3e3e2637df84f7020dded1ff0fa651f6cd6eb43f0bc3867a5840bb0df66597d23d6c650703ffe24e7657a816d315cbc2ef619bde5e454fef9e30f365f70812f481b93bc22870a1c2a44b8b34e56c1fe65cdb16988a387cdc7b614399e2532243f62cd4f67496745674de790ce86ce119d633c83e478a6b03c0d0bcd5f2306013579088f9d288613c570a2184e14c38962c0734b3f2c12d7e887f17c7cb2bc58a68000bb6ee654ed870d8280663f6c10f9a4743e0b04b4f4c39c6b724afd30bb3ce754ed8771f485f725bad90f1b9ce8abe11ef5c3dc4b544b96ce56ab9e66e9870dc23d673f6c4edcebee878dc4bd1221cd897bbe1fe6fb61be1ff643fa61711e135d161d2f68bf2bfd3023bf793f0c13d038ea777d3fccf7c37c3fecbbf6c3b26e55a51f56eb5f8de987c5acda0f83ae87b31f062d3cf68338f58c38f58c38f58cc03e788ee81ce3192a613c53af4a50af4a50584161058515145698b46f357fcf4c188d45389b203153cfec29c051be4acf2c9d565e1a2ccad6c9ffadf4ccb4312aefa4bc0def99fd7dfc2b3f4afc23da3a291801b3e9c2c386acbca18e05e51f5b7b66e9227cdb8528d6e0b3120b65cbf1ff567a665cc605f5b6265a343a29ed8916da943b2918c10c891ed249b1ab39d34eca53e925a0226d0a16ca2ab995592169f055b8092feba43c9558e875ff82cd1946a457ce4e8accd63e38963e943a29d92a88bf953ee53073c906af0fcaa388ae3399cbd5338382c90b562d561194f368b6a0e06fa56786e526efa4b426bad133ebcaa3ca94797da682d9c3ebc59c849cd76b7954f5e6d10b78bd2b8f0ad5caeb52ba795ddbb572d96589d77585d74d18ea7e5ee729afab49bc7ec4bc00ac1fb553ba50779a9df798383388d249ecc651bf0b5ab5b3402ea37427d0b012a5272f0900c01f88f025f99e903e9ab6e5346c7642fa85d82cd9f362ff35d8fcf4b3b0998b166c7e020ac646e18d63658a7fa7d08c35b5ad25f387e02245e5f2af50756a2af1e5110be91ab140128af85fa8f8623c3b9f12f48fa1dfa3ec2927c3e20c20cfb09e613dc37a864d3e9161f54d326c7f1ebd2186e505c3f22ac38695a940c6c86fc9b02876e3a8dff50ceb19d633ec28868d6764d8ecd926caf29951967e0749bb5056cc8db2d9869c1594a54d1258a9c5ccb6e64caa1365c2a22d1b335126797cd74773d0b69a6e9f2893e5983c161bb2f2867a8b59feb10d65b37d3fad48c5d696257eb7bb5c261594555c451326ca04ed8956a12aa32c463043a247a32ca8a4780984c0a5312926d0251e2bbea1046ed84677a32c2e5883646811a669a935cc0b3b708a2dd53f1ad5c625b73b3b345136db52a482b2368f5a29ece622490565519bfdd3279a283b288f22d5cd642e07ca663b97141365560e94cdf63049aa13650615cc26cab6275acbb8327d629e82391a656b7914a4eacba31d287b491e15722ccaf2ac8ace2f4bec4a886cffc40663c0f40976c9f4897e801d38552215b67e34ee822ee7992a11f64d950080ddaf377f4a3c72f87593267e39cb62c1f8512c6bdc2c0b4f7cfc593e6ef10221f6390589bc82cc7f7f3358faf16f0c567f0cea4fac900692ac6400814c723cb7932ca7df71aba37692c55987f3926cb67d7a37c9661ba91fda4976f1ad4836dba5fd52926d4df42892353741b2a0920ac99a2b92ecf6be83123cc97a924d6e9d64473b650b64ca2ecb241b5749d60c20597e45927d432942f8159936bd1ac2b4c6c9b4d5bb50f23dd37aa6fd1ca6dddeff2ca695cccdb450a85266850b64da37ac35e1823cb097002e1f0cb8821f0070e5e5808b439cde55eb5db5bfcb559bede25b015cda3bb7a426bb9f6f0d707514e680dbda1fb904702182abe451956da46c452af60a6615c0a52d950f55c0d57198f4267a14e0aa2ae0420457cba31d805beb84697519e06a8e5bfb4ce984fd6cc0cdb1b640ddb900d73b6d3de07aa76d1a6036c0ed73da120063fb91312dfc4d5310f061b87072f048c0ad787071eda90370152700e60a61188032a1656a8756d82513796faef7e6fe2e6fae875d0fbbb705bb13e625880276451576b1c12cc1ae545f3c2fe16dc4bc04a95c885bbbeb7db81e71bd0f370d301a71959cc1873b9977c53487ee5cbc6b62ac5ed38ac33b77a7b28477ee7e2fe7eefd22707f85aca426fb41b24375ab3dc1d5f0adf626f02e4670953caaeef19b6cae8f9d950b66f6ddb3eaec0525f880291b17f02e4670b53cdac1bba09239795729d9cebb10577b1eedd8c6ec72e7ae524cf66f6376bac798e55d242f595cd64abe1c1bb7896e5e4c00ab1d8dbbb3b9792317cf716e33846087d373b05dad93e376b3059805c839dd23c502ea2c0e8775f200907358efff6e97eb20050f0fd61eac7f9fef58b42c5eebf31d434df9f167f792e005413421f5d55cc7ad73230c4174d4313722a2fd9e67c567ef2ef6eee25b77177b7cf6f87c5bf83c616ec46077b1e603f0997f0a3e8f711ca3d8ac7634eecee638f6d0eca1d97ba3678066d5b26bd9286ff454829ee88cbe80a0798c6d984a2ba0991cd09b202c3e40ce0ab42ac3247e311e90aaea80563266fdbed806414323c83fb4b997795b56c0645c854999ed3d5f34d469c8ca1b9a0d75f1e300983c3b7cb19bd24b309b1730196735f4462ca9ca5f8b553f4c62325a7db19b52432dee0f49962552133b5ce79b4086c5a2aa82324a6a6231aa5d8b1afb8318fd5c251c5cd5662e1d56b9ea9ae6b25c75762caaaa9a0ba4ea335737574d36d79461f8b0e0aab0ca555a55b92a1a3e0c3f334d8ddf1e0085ad1f8dbba0cb79684a45c3c02779593e3e1c06ed87f58a9f5e38fc5d1dd68be377a4a39fc22b8bcd8fe21516b7adeadf10a6a40d605e9bc1dd8247ea3f1282847504718c745377592a72e2d118b70ce96c3a7084136b\n24f439820e1c115847c9397164b50b221574e38880a6033fa4bd6ec791e15bac06c7c93862431ebbdab7e3c538022a397e168e405ca37004cd15169f6472e208971abf73a1d7ed38d2bb396c8123ede6eac39139cdd5812335735d15473acde57164148e802e67da6ed3e34811ec67e1c86a77d3140159b8a088bc1282bb0d8a384ea48888af811c623c2b46678e67d08a72528414f4bb107496e9b32dae0de35d1bbfdab5e159c2b3c40f6109efdaf02c61c379d786cbb5312794b4ba36668092d9c75bbc83e35b3938fc788b1f6ff92150e21d1c1e4a6cb82e07c76f8012b7a764cef196564fc905e32d2ac2a52a664e1c79021c815c5ec191d5f1bd3a4f53698d79aeb1fe50d9bfc7ad3f6c6bdfb2aac9e20846c06cbaa6b76f8e461d121df230178dd2db700c45f063c46b13a865acc269eb0f073218463043a2473318a86456c790d6ed8dfad338069313194c0e65b0c6046acf60e5c333d87c0c0659df3b866e7a46f0b745b7a79f856e9cb5a01b14a18f3fabe3112fd2f570174f9a699bb7ab68a75fd5356f7720b8c5da839b07b7df066e1347f4eae036fc5bc47e44af7cf8113d0f6ecc839b4db707b7db06b7094382c3c0ede26140837610515af8679a9bb40c8cd6bc826fe7f3bfb57dbf04e09b92b58d0b8cd0f6ef11c3801f87c7776d92e5dad6c7ed1b1718917e87dcc6624356de506f1acb3fb6eefbb50ca288e5e842e96d6e5c004010297da8e29b0965ffd86713dfda13ad5515df4cd6b85d96e8f1fb7e2d4b2fc145e14d7c5bea0d965e5c142ebaf1adde8ed55ae0854d3b364eeb10f5ca51c6d4fc0ebf1b9a8b57372eb079d44a01cd0aee4e57dbb800b53901df86e551c4b799cce5fa6a19164c95332ba5b7b97181829fb5aeed733ba86036372e18944769b786790ae6f8af968dcea31dcc7a491e9db2ef579435a1d9658959c10265668d78dccface212663dbca557610fbdee290483e7466d618009a81f8dbba0df79068187ee36c0fe2e76a7c5e6b87a3dfe01a20558f8bd88fadb761b582c6f024865d832336d09407a0695c145799300cd0a00cd2bbef2c3e5bd6a9d0f13ad46d1505aa5fd6795ce769e6dd91e40d05342777e8f015b8a59bfc7705a041167f1a59c3ac2cd78039c8a8986b6e6524e1de166bc794e05957c1aa79eca9b170de054caa35a5fcaa92336d8ba014ec5444316bd9453476cb075f39c3a3e8f4ee7d4ce3cea39f5624e05fd7a4ef59cfa399c7a5afc2c0f6c1bf09e1619c39e16fdc05b7e786ee0a58f2c28d303bcf494309f0bbcde31eb1db3b7ee98f5c0eb81d7036fb9c07d7fe0f58e590fbc9fea98fd0dc03bcac37b4de01de6e1fd12e0f51e5eefe1bd750faf9f89e06722f89908e502f7fd81d77b783df07a0f6f1ae06b3cbcd79cd230ccc33b604a8394dec3eb3dbcbfcdc3bb7b0cf0eb4315e0ddec17d525ef4669064d6afd9b61265fe47515e0c508ae924715245a2995c31ea5b701bc98c1aa1f4ac36f86852ceca7fc0b801723b85a1eed005e50c9a7012fc4d59e4785b922f0c63cec075e7539f08a3bd9f1cd307687fc40f2ddc5007ee973268760390e823151f5a371d77b7d3d047baf6f1ae06bbcbe50eb7dfcc1a6062eaeeaf575ec1485b40425862038a64f84313af32e2016b30331b5bb3aa80071061b051b86b84f76c5bbc6d37677c29694c38178a676d701c44458a0cd54244ee9cd8038870dc8b34058b206c4c308cb02b14d3c1f0ec43311d668201e0f1ba5f6794ed8f040ec8138b975200eaf07c49cd61bd7a46900f1459fce25d0e51a9e23d0950680b81b8d0bf83dd373e20ef5dd87c1dc9245f968dc054dcfb3bbc2efc2606e83718fc1c33178f7f83b30b84cb65f8ac104b82a9e1583b99afdd373633e17739ce57331c747fe4f649ef3dd8a5a89d0b55b1185acbcc1b15b51fee34417e9a6f4924a6b4b97786cc48a5eb496b37e2ee6f958d9ade8c29dd98fb3ecccde6eae90f76e2e359bb9ba86c7abe60a79dc67aee99b4b759a6bc2e652bcd85c8a5737978aa8c097e028fe3e9b4b91b0f5a37177b6cfc538db52d6c49fd735342fc7f31ff8136af0ef883e3f05467ed95760d2762dafa4aa7b75d67e44cae063be0273a4afc01ce92b3047fa0acc91f84138b77fe25264bfbbf842505dfe655f7ee9e08b515f7e111f7a225fa4212b6f683658c58fd3f8827643b42fe9e18b79bffcf23ceb975f3c5f78beb861be98edcb2fda8ce20b6a14b6ebc375006322eb7830f15f82298149f62518cb1e5963342798440423319e15a333bf004c4ca4e71eff1bb395f87196adc4db5bbaac92b260d2d84a7c5a4b77e156e2c759b6121feced696c25fe49de9ea7ddbcde9ee15b89f77a7b243c2f64314ad7b20244333069398f5eb0e479501e75ae00992b8f2a2c983cce4573af00313ac64c769c65c9f360046dcc54fd2404ade5d18b115445d1b43c3a6105c870049590d7afbb02a41b410ff974b9d2e4b731588a09a81f8dbbb3eda97e5358ea67bedd1eccfeb42dd6851cb8c57a8d5ce9779c19f7b4ab8cf211e6d61f76636ec796eb47da72fdd8b5e243d2efa9afae6d821be77e829b9fe0e627b8f9096e69c1f413dc7ede043739e40341734e7053d11dbfce0437e9fa7c50fdae9fe0e627b8f9096e69801f3ec1ad157d691d880c3bd776cc8ebe7e771fbfbbcfadefee03cfc79a8783d0f7bd1d7d47f876df1fdf4538097d6dc8ca1beae62affd8b59d6524f920f47d6f47df11beddf6449bdaf4823afa4e4bf494ed2c8b9740f550f876c33076e6d1e9e8dbbd9de5f8c5cea2f0ed8a9a6f370ecbe82bb8fe798b9d2951f5a371d7eff8e35dbe9f06c17ec71f07d7d2c3d8d438b7b80ce913c979158c102c86fb7f07edf8230d7f07088ef07c9b2b3b925956762493675eda9049d7cccbe4e29997a092e4b3665ed6a6f2cd3af37237cbcccbdd23ff47842e734535af60dd5c3664e50d7573957f9c3cf3b27809542f85b98c45a39b9d79290bafa0ac7a05e3a832ec2da26f34f39284ad1f8dbb7e65c7af9d40f92b577624ee099486c822afc1902c64c3bdd6b1b223a1951d09b9cf125ad991744ea08cf9ae952f180d458839f9c2afecf8562b3bd05c210b3bf982e61202123eb5f345efc878c1174f93f9c2867ceae28ba78bf90254f2f4597cb1ea9c56e7f9a2246cfd68dcfd992b3b3c600c5da171c35ce05e58b1da35b9e06922178c5e58014f3dddeac28add2c0b2bda3bb2591d61b9a0b1b0625a47f6c28515bb591656b4273acd5f79a21b0b2be6ecbd772fac28f7dea949ed6e5d4b0d925f58e11756f88515331160fccd17565002ea47e3ae5f58e147d93e87417fedc28a9a476bf4c28a26e6b60dac41d3bb0b98e6994b6bc0c28aff03c71fb271b76901000a\n\n"
This code is for getting to bytes and then converting it to string.
compString = compString.Substring(4);//taking the first 2 bytes out. 1 hex has 4 bits
byte[] byteArray = hexStringToByteArray(compString);
string result = System.Text.Encoding.ASCII.GetString(byteArray);
private byte[] hexStringToByteArray(string hexString)
{
hexString = hexString.Replace("\n", "");
int len = (hexString.Length - 2);
byte[] byteArray = new byte[len / 2];
for (int i = 0; i < len; i += 2)
{
byte b = byte.Parse(hexString.Substring(i, 2), NumberStyles.HexNumber);
byteArray[i / 2] = b;
}
return byteArray;
}
Any suggestions?
EDIT- Output was weird because it was compressed. I needed to decompress it. Thanks everyone
I have a hex string that I convert over to array of bytes. I am not sure if it's because of the length of the string?
Example of a string that work:
"010121212704000066ae00001f8b0800000000000400bd93316bc3301085496c476ee5c174f2ef350412532f1d8c3d9504b268b3f70e4a962418b2ba810e3564e8d2df504ed6e9ae04374bc9f6f16cddbdbb274919b6c5c7d18ba7c96324c37d093cb19c67bed5b5021ef4765139bd2d2ad28b7aedbb3af51975ad56fdc0f1ab48a69194e12e85369ef97c78060e6c09b2a215f12e9dbb36ef8667f6ecdc59d9be907e2a493f64601d7be5cc3ae92733aa30dc199eb1b1073ea730aacf56606b667c0515f37661de8003dbebc256f9e5f4b6e00cabc43a1b456789b502c6fa9b86e60276ff37e8b9338c91705e1e892136dcff5b8f67b5faec073f712912113d7d7bb742f4afeed33d03e577f7ef70c548b8c148b8e29fc20def1eeeef37c983c6f739167af2f003d1a6efdf270400000a\n\n"
a string that throws and error
Index was outside the bounds of the array:
"010100215c190000182904001f8b0800000000000400ed987d4fdb4810c6bfca88ff21bb769c106bbbd27aed10377ec3760351844e888bae48052a884a3ffe3de32012c0b9f2d288ab5a24b2b333bbe39df5cf8f37514343df2fbe5cdef8df6ffefeb0f379b1f8ea773ab7b7b77bb7eeded5f53f1d4708d9394e93eaecf3fce274e77ef0f98f07ef9e5fde2c4e2fcfe63bf46d7e7d737e75f96147c8bb140fa62fae4fbfcdbf7cbdba5eec9d5d5d74ec29e697f3c5f5f91c1377b41a9a32a58e5636d351608b51114e4c856ba5aeeac0a78238e470904e30b6acb41c084485fc0b0dffab0e7b5531b42d21f6aab0ac5b42ec557652b684d8ab2a3b6a09b157a5b62d217b9599b4ad90bdaa2ed296107b556d8edb42f0aa386bbb167b5512b62d9ebd6a94b485d8abd2d665b05745dd41b725d6b8559ab4ad91bd6a7a386dbb5ce3e6026cb5a1060ea8b28e5aa2ec55699cb55d135e85a945dbad63b7ea34b4a495e6d1155b45192d3b6ca01f0cef826c703cc7529603d852456db59026acc1104c055f1eeeda0908403418a320079719334375a507b8321ad8d85cb6f9a6e23f0e74322a516b003bd76194c1ce513360e5222d17d2cce6c9467bb00c28c2eca196bd410f140cc1bdd595d89dca4cd841c5c45b1e50eb3ae7386357e9dc823364a9523dc52ad326b35c66e6e4c73a30e8e1760db57024ae89c4e1a7527b037e1a3e311a71d2ec011a1586a85ff41c21f7110de130cb52425e9f5996825685d88a6e9753f03ad0e9bb4c3c3aa1e56cf8547163e1133b677979dca8ca604fb1542489273ac600dc341b6b5e66ccbe501fb31317b7563bd86fcbfba56928e9a3242b2994544a8a25194947922249b5a4a9a440d248d258522a299134915449ca241d4aca251df0ae20515ae808161a658b923f2a5e0537a0d6d14b481ddefe860d6e1070ef022e2a8c258a478d685598d79ab860b4785a2a9da6293f20c8161d16badfc5403670e724a7e346e52889d371abf261864ec3319b2a8c62dc2a6444abea32d02e611a5a74724d4d0730d9bcd243fa4896422a2926434714514d530a6844634a29a1095594d121e574800a3181eb44cd1db63611d791af61ce69674e7a8eb867aee778af616e78b462ce13de8ab9c17698e3a764c59c79825d2b6a59435bded0f62ad44c1a95b1351999b84ce22caad6d1eb3e0bbd2449d6d1eb1f6d40cf9887e8f507cf422f5ca1573439b8bde3d0ac91f890c00c0ce660f029818f59f1fa6ef79e15f96c561a3e5af4c971c44a9f1c97c1d9022bbd7b565e48c9fb2042ac1f2b44dcfd35440e37222285d7df84086d50a74788fc008f779327774d9ee49be5c9f1d65f89db418e05b51db9ff7e19feca0ab52fdfa8502bfcdade922fd1a8de9a46b9bd376a\n94dc5f3f430dbcde1f8d7aac51e9be789646399ebb11925f5ca344774da35e756c5fd32831906b4728d7d9ce6bf1773b42796f15a89f738472bafc85e95e9e9cb7ca53cf5d7f9f759dadb0d27fad3cbd1b250f15ca7582672994707b1b0fda3f47a1deed6b9ec077b1954689b76a945c9efeefb8f3b6a3510f7f5a78aa51bfc34f0bde06717aac472ffb69e1cfa1e9bd25e97f7e68eaf0afa47614d77c739a56d97134d53359f8b33019f807819fe4997fe2cfca03df0f7ce1c3f66751c02e4ffa5208183313d6be3c6942fed4e731b31934c5373e78f10dbaa227a47ff7d70c4336e3fb21d075e8c85d1981a06444c5185d8cf3675234534e4eb0405e1aee88d1ff020794ae045c1900000a\n\n"
This the method that converts:
private byte[] hexStringToByteArray(string hexString)
{
int len = (hexString.Length - 2);
byte[] byteArray = new byte[len / 2];
for (int i = 0; i < len; i += 2)
{
byte b = byte.Parse(hexString.Substring(i, 2), NumberStyles.HexNumber);
byteArray[i / 2] = b;
}
return byteArray;
}
I found another way but this throws a different exception of: System.FormatException: Could not find any recognizable digits.`
private byte[] hexStringToByteArray(string hexString)
{
int NumberChars = hexString.Length - 2;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 2; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
return bytes;
}
The conversion method is working fine.
The problem is with the provided string. It has 3 linebreaks 2 at the end and 1 in the middle.
You can either strip the line breaks from the hexString with
hexString = hexString.Replace("\n","");
or just use this string. I stripped the linebreaks for you.
"010100215c190000182904001f8b0800000000000400ed987d4fdb4810c6bfca88ff21bb769c106bbbd27aed10377ec3760351844e888bae48052a884a3ffe3de32012c0b9f2d288ab5a24b2b333bbe39df5cf8f37514343df2fbe5cdef8df6ffefeb0f379b1f8ea773ab7b7b77bb7eeded5f53f1d4708d9394e93eaecf3fce274e77ef0f98f07ef9e5fde2c4e2fcfe63bf46d7e7d737e75f96147c8bb140fa62fae4fbfcdbf7cbdba5eec9d5d5d74ec29e697f3c5f5f91c1377b41a9a32a58e5636d351608b51114e4c856ba5aeeac0a78238e470904e30b6acb41c084485fc0b0dffab0e7b5531b42d21f6aab0ac5b42ec557652b684d8ab2a3b6a09b157a5b62d217b9599b4ad90bdaa2ed296107b556d8edb42f0aa386bbb167b5512b62d9ebd6a94b485d8abd2d665b05745dd41b725d6b8559ab4ad91bd6a7a386dbb5ce3e6026cb5a1060ea8b28e5aa2ec55699cb55d135e85a945dbad63b7ea34b4a495e6d1155b45192d3b6ca01f0cef826c703cc7529603d852456db59026acc1104c055f1eeeda0908403418a320079719334375a507b8321ad8d85cb6f9a6e23f0e74322a516b003bd76194c1ce513360e5222d17d2cce6c9467bb00c28c2eca196bd410f140cc1bdd595d89dca4cd841c5c45b1e50eb3ae7386357e9dc823364a9523dc52ad326b35c66e6e4c73a30e8e1760db57024ae89c4e1a7527b037e1a3e311a71d2ec011a1586a85ff41c21f7110de130cb52425e9f5996825685d88a6e9753f03ad0e9bb4c3c3aa1e56cf8547163e1133b677979dca8ca604fb1542489273ac600dc341b6b5e66ccbe501fb31317b7563bd86fcbfba56928e9a3242b2994544a8a25194947922249b5a4a9a440d248d258522a299134915449ca241d4aca251df0ae20515ae808161a658b923f2a5e0537a0d6d14b481ddefe860d6e1070ef022e2a8c258a478d685598d79ab860b4785a2a9da6293f20c8161d16badfc5403670e724a7e346e52889d371abf261864ec3319b2a8c62dc2a6444abea32d02e611a5a74724d4d0730d9bcd243fa4896422a2926434714514d530a6844634a29a1095594d121e574800a3181eb44cd1db63611d791af61ce69674e7a8eb867aee778af616e78b462ce13de8ab9c17698e3a764c59c79825d2b6a59435bded0f62ad44c1a95b1351999b84ce22caad6d1eb3e0bbd2449d6d1eb1f6d40cf9887e8f507cf422f5ca1573439b8bde3d0ac91f890c00c0ce660f029818f59f1fa6ef79e15f96c561a3e5af4c971c44a9f1c97c1d9022bbd7b565e48c9fb2042ac1f2b44dcfd35440e37222285d7df84086d50a74788fc008f779327774d9ee49be5c9f1d65f89db418e05b51db9ff7e19feca0ab52fdfa8502bfcdade922fd1a8de9a46b9bd376a94dc5f3f430dbcde1f8d7aac51e9be789646399ebb11925f5ca344774da35e756c5fd32831906b4728d7d9ce6bf1773b42796f15a89f738472bafc85e95e9e9cb7ca53cf5d7f9f759dadb0d27fad3cbd1b250f15ca7582672994707b1b0fda3f47a1deed6b9ec077b1954689b76a945c9efeefb8f3b6a3510f7f5a78aa51bfc34f0bde06717aac472ffb69e1cfa1e9bd25e97f7e68eaf0afa47614d77c739a56d97134d53359f8b33019f807819fe4997fe2cfca03df0f7ce1c3f66751c02e4ffa5208183313d6be3c6942fed4e731b31934c5373e78f10dbaa227a47ff7d70c4336e3fb21d075e8c85d1981a06444c5185d8cf3675234534e4eb0405e1aee88d1ff020794ae045c1900000a"