473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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.Writ e(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.ASC IIEncoding encoding = new System.Text.ASC IIEncoding();
return encoding.GetByt es(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 4386
Plater
7,872 Recognized Expert Expert
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
mjaaland
3 New Member
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.FromBas e64String (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 Recognized Expert Expert
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
437
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 string that I sent into it. After working on Debugging the code I found two problems. One was in the statement fromEncrypt = New Byte(encrypted.Length) {} where both FromEncrypt and encrypted are both byte arrays and encrypted's length is 24....
3
9512
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 the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
3
1674
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
10364
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, etc. I need: short = byte+byte, short = byte+byte, etc.
6
33876
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 to back to the original byte implementation. For example, this doesn't work: // Bytes for .02{cent} in my standard codepage byte ba = new Byte { 0x2e, 0x30, 0x32, 0x9b };
4
13410
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 be read and the string representation of the byte array has to be converted back to the original byte array and converted back to the original string - confused yet? in psuedo
2
3593
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 that is displayed in a text box. Once displayed, the text will be copied, transmitted and then pasted (all manually by humans) into a second utility where the string must then be reverse-engineered into the *original* byte array. The byte array will...
5
16147
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); for (i=0,i2=7; i<5; i++,i2--)
0
1907
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 reproduce the problem. Thanks, Jon Jacobs Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
0
4129
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 a specified base to an /// equivalent 8-bit unsigned integer array (byte). /// </summary> /// <param name="text">A System.String containing an array of numbers separated by '\r' or '\n' or '\t' or ' ' (space) or ','...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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, 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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5380
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
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
3
2877
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.