473,396 Members | 1,774 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,396 software developers and data experts.

Byte array to string and back - wrong numbers! (from C# to C++)

Hi, I hope someone can help me out with this one!


Step 1: I get a struct from unmanaged code looking like this.
public unsafe struct KEY
{
public UInt16 idkey_len;
public byte* idkey;

}

I get the struct after unmanaged code has filled it with values, I can not change the unmanaged code. The byte * idkey indicates the start of a key I want to store, and the indkey_len is the length.

I observe through debugger that the first value is 96.... (where the pointer poitns to), and the length is 21.



Step 2: I work in C#, so I want to store this using other data structures.

byte[] idKey = new byte[key.idkey_len];
for (i = 0; i < idKey.Length; i++)
idKey[i] = *(key.idkey + i);

Debugging show that the resulting byte array begins with { 96, 0, 0...}, this is good, 9600 was above! Lenth of the array is 21 too.



Step 3:
I convert it to a string and store it in a .txt

foreach (byte b in idKey)
textwriter.Write(b);

The result begins with 9600..... Still good, the original started with 9600 as mentioned. There are 52 numbers in total.



Step 4: NOW it messes up. I want to load this and convert it back to the original struct, so first I read the numbers as a string, and I make a byte array out of it.
I use this for conversion:

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);

The resulting byte array have totally different numbers, respectively { 57, 54, 48 48... } and the length, well, I just set it to 21 cause I didn't know what else I could do.

In order to process it right I have to be able to reconstruct the struct mentioned in step one. That is, a byte pointer to the start of the whole thing! I don't have a way to come around it, as I said I cannot change the unmanaged code.

Please, I would GREATLY appreciate any help.

all the best
Bendik Mjaaland
Jul 14 '08 #1
3 4355
Plater
7,872 Expert 4TB
0x00 is not a valid string byte and will be removed when you do string conversions. The same applies for many other byte values.
If you really want them stored as a string, store them as a HEX string:
"96 00 00 " etc

{ 57, 54, 48 48... } IS the byte values for "9600".
Jul 14 '08 #2
Thanks for answering!

But with hex string, will that really help me out? I want the result of a byte string with values {96, 0, 0, ....}, not {96, 00,....}

I am sure you know what you are talking about, if you could be more precise to someone a bit less experienced I would really appreciate it :).

PS! I also read about Base64 conversion of some kind, but when I tried Convert.FromBase64String (or something) I got a formaterror, supposedly the length of the string must be fixed to an even number of 8! Ideas?

Thank you
Jul 15 '08 #3
Plater
7,872 Expert 4TB
Ok so we need to clear things up.
You receive these byte values:
0x96 0x00 0x00
Correct?

And you wish to save them as a textual string?
Jul 15 '08 #4

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

Similar topics

3
by: Steve Mauldin | last post by:
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
3
by: PawelR | last post by:
Hello group, I've string :"A1 10 15 06 87 FF 10" etc. this are hex value of character. How convert this string to array of byte : System.Byte arrBytes = new byte; Thx PawelR
3
by: MuZZy | last post by:
Hi, I just wonder if someone can help me wit this - i have a byte array and need to convert it to short array, creating short numbers by combining bytes by pairs: My array: byte, byte, byte,...
6
by: clintp | last post by:
I have a byte array that contains 8-bit ascii characters. I'm not particular about the codepage used to display them, but I have to preserve the position in the string with something and be able...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
5
jeffbroodwar
by: jeffbroodwar | last post by:
Hi everyone, I have a program that converts variables long,string,double to byte array here's the code : for long : //CompanyId temp = longToByteArray(CompanyId); ...
0
by: JonJacobs | last post by:
When I add a series of byte arrays to an array list, then I read them back, all the arraylist byte array elements are identical to the last byte array entry. What is wrong? The following code will...
0
by: laszlobarta | last post by:
Hello, For saving 1 hour of your precious time use this function for parsing a text to a byte array: /// <summary> /// Converts the string representation of an array of numbers in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.