473,799 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Encoding.ASCII. GetBytes

Checking if proper/best way to do this...
I have to pass a C# string to a C++ DLL. The function in the DLL
(gethostbyname) requires a null terminated string. It seems like when you do
byte [] var = Encoding.ASCII. GetBytes(string ); that it only loads the byte
array with the chars and does not add a null at the end. I would like to
know if the below is proper way to deal with this:

byte [] var = Encoding.ASCII. GetBytes(string + '\0');

By using this when I pass var to C++ func char* I do get the correct string
and can call gethostbyname(v ar); and it works OK.

Thank you!
Mar 1 '06 #1
1 12381
>byte [] var = Encoding.ASCII. GetBytes(string + '\0');

By using this when I pass var to C++ func char* I do get the correct string
and can call gethostbyname(v ar); and it works OK.


That will work, but an easier way would be to change your
gethostbyname declaration to take a string parameter directly.
Mattias

--
Mattias Sjgren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 1 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1196
by: Tim Murray | last post by:
Hi, I am writing in Managed C++ and trying to send an ASCII string to a telnet application that supports color. Does anyone know how to encode a color for the string that I am sending? This is what I have: Byte byteMessage = Encoding::ASCII->GetBytes(message- >ToCharArray()); m_sock->Send( byteMessage, byteMessage->Length, SocketFlags::None );
3
9706
by: Chris Auer | last post by:
I am trying to take in ASCII documents and convert them into ANSI for a customer in Germany. But every file I process turns umlauts and other german characters into something other then what it was. Here is some real simple console code. It seems like umlauts are not in ASCII, but they are. StreamWriter swExport = new StreamWriter("C:\\MyDocuments\\In Progress\\IIR\\OUT.txt",false,System.Text.Encoding.ASCII); swExport.WriteLine("");
1
4884
by: james | last post by:
I am creating an connection string at runtime using decryption (crypto libraries). i have a very unpridictable error while assinging the generated connection string to the connection property. my code is something like this ... Public Function DbconnStr(ByVal UID As String, ByVal Pwd As String, _ ByVal ServerName As String,
0
323
by: Jason L James | last post by:
Hi All, I am trying to send information from my vb.Net app to a piece of hardware with an embedded TCP/IP controller using sockets. I can send information so the sockets are working but the data is garbage!!! It works if the ascii of the character I am sending
1
9787
by: Emilio | last post by:
Question about Dim data As () = System.Text.Encoding.ASCII.GetBytes(message) Would it be the same to write: Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(message) ?
3
6059
by: devgrt | last post by:
C#: I have a buffer that is populated with char data from a DLL: byte ret = new byte; I want to convert to a C# string: string msg = Encoding.ASCII.GetString(ret, 0, ret.Length); The problem is I never know in advance what size the string will be. When I call Encoding.ASCII.GetString I need to specify the size. If I set it to ret.Length as above then in the debugger I can see msg is the full 1024 bytes (0 padded at the end...
4
14324
by: cok | last post by:
Hi, All I have a question about Encoding.ASCII.GetString. I want convert a byte array to a string, I use Encoding.ASCII.GetString(byteArray) (byteArray is something like ) but the retult is a long string contain string("ABC") and many '0',
9
4150
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string. What am I doing wrong? Dim plainText As String plainText = "t═e" Dim plainTextBytes() As Byte Dim enc As Encoding = Encoding.ASCII plainTextBytes = enc.GetBytes(plainText)
1
5610
by: Rui | last post by:
Hi, how can i do what Encoding.ASCII.GetBytes (in .net, c#) does with the strings. I am trying to query some dns server to check its response using udp sockets. Some of the source below: # encoding: utf8 import socket import sys import struct IP_PORT = 53
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10491
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...
1
10247
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
10031
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
9079
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 projectplanning, coding, testing, and deploymentwithout 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...
1
7571
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
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 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.