Ok, let me explain this first. I did a search about 'hexadecimal'
first and found a lot of threads about it, but none of them tell me
what i want to know.
I am new to C#, and want to work with binary data (actually some
custom file formats). I used to do this a lot in PHP, but i can't
understand how this works in C#.
in PHP i could do this:
$packet = 0x2a . 0x00 . 0x00 . 0x3f . "some string";
it would result in a var with 4 bytes that i specified exacly, and a
string. A simple binary data packet.
in C#, if i try
var = 0x00 + "some string";
when i save this to a file, 0x00 turns to 0x30
all i want is to create a var like i did in the PHP example, but in
the C# way..... anyone can help me?
i don't know if it's the right way to do it in this language, please
if you have a better idea, tell me. =) 6 2195
Hi Natan,
Try this:
UnicodeEncoding unicode = new UnicodeEncoding ();
string s = unicode.GetStri ng(new byte[]{your bytes here})
Note that there are others encoders, such as ASCIIEncoding, etc.
--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
"Natan Vivo" <nv***@mandic.c om.br> wrote in message
news:82******** *************** **@posting.goog le.com... Ok, let me explain this first. I did a search about 'hexadecimal' first and found a lot of threads about it, but none of them tell me what i want to know.
I am new to C#, and want to work with binary data (actually some custom file formats). I used to do this a lot in PHP, but i can't understand how this works in C#.
in PHP i could do this:
$packet = 0x2a . 0x00 . 0x00 . 0x3f . "some string";
it would result in a var with 4 bytes that i specified exacly, and a string. A simple binary data packet.
in C#, if i try
var = 0x00 + "some string";
when i save this to a file, 0x00 turns to 0x30
all i want is to create a var like i did in the PHP example, but in the C# way..... anyone can help me?
i don't know if it's the right way to do it in this language, please if you have a better idea, tell me. =)
Natan Vivo <nv***@mandic.c om.br> wrote: Ok, let me explain this first. I did a search about 'hexadecimal' first and found a lot of threads about it, but none of them tell me what i want to know.
I am new to C#, and want to work with binary data (actually some custom file formats). I used to do this a lot in PHP, but i can't understand how this works in C#.
in PHP i could do this:
$packet = 0x2a . 0x00 . 0x00 . 0x3f . "some string";
it would result in a var with 4 bytes that i specified exacly, and a string. A simple binary data packet.
That's not really simple at all - you need to work out what text encoding
you mean, to start with.
Don't try to turn your binary data into a string - instead, deal with
the file at a binary level and convert your string into a byte array
with the appropriate Encoding.
See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in message news:<MP******* *************** *@msnews.micros oft.com>... That's not really simple at all - you need to work out what text encoding you mean, to start with.
Don't try to turn your binary data into a string - instead, deal with the file at a binary level and convert your string into a byte array with the appropriate Encoding.
I am not working with a file. And i don't want to work with
encodings.... See, eg: I want to create a var and put 0x00 x0x2f 0x00
0x20 and "something" in.
Ok, "something" is ascii... but 0x00 and etc, there is no encoding in
this. it is a byte and i should not have to use anything to convert,
encode, etc... it is what it is.
I would like to understand how it works in C#... i can't understand
how to work correctly with a byte array. how to add, remove, and
convert everything into a single text.
See http://www.pobox.com/~skeet/csharp/unicode.html for more information.
I have no use for unicode of filestreams in my program. =/
"Miha Markic" <miha at rthand com> wrote in message news:<#W******* *******@tk2msft ngp13.phx.gbl>. .. Hi Natan,
Try this: UnicodeEncoding unicode = new UnicodeEncoding (); string s = unicode.GetStri ng(new byte[]{your bytes here})
Note that there are others encoders, such as ASCIIEncoding, etc.
I can't understand why i should encode something... i am telling
exactly what bytes i want to store.
Man, this C# is getting me headaches... i'll probably go right back to
C...
Natan Vivo <nv***@mandic.c om.br> wrote: Jon Skeet [C# MVP] <sk***@pobox.co m> wrote: That's not really simple at all - you need to work out what text encoding you mean, to start with.
Don't try to turn your binary data into a string - instead, deal with the file at a binary level and convert your string into a byte array with the appropriate Encoding. I am not working with a file.
Then work with a memory stream, or a byte array.
And i don't want to work with encodings.... See, eg: I want to create a var and put 0x00 x0x2f 0x00 0x20 and "something" in.
Ok, "something" is ascii...
Ah, so you want Encoding.ASCII.
but 0x00 and etc, there is no encoding in this. it is a byte and i should not have to use anything to convert, encode, etc... it is what it is.
Exactly - you deal with everything in binary, including your string,
which you convert to binary with an Encoding.
I would like to understand how it works in C#... i can't understand how to work correctly with a byte array. how to add, remove, and convert everything into a single text.
The easiest way is probably to use a binary stream. You need to be
careful though - you say you want to "convert everything into a single
text" but the binary data *isn't* text, it's binary! See http://www.pobox.com/~skeet/csharp/unicode.html for more information.
I have no use for unicode of filestreams in my program. =/
But you *do* have use for an encoding, namely Encoding.ASCII.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Natan Vivo <nv***@mandic.c om.br> wrote: Note that there are others encoders, such as ASCIIEncoding, etc. I can't understand why i should encode something... i am telling exactly what bytes i want to store.
You need to encode your string, because you want it represented as
bytes. You need to understand that a string is a sequence of
characters, *not* bytes.
Man, this C# is getting me headaches... i'll probably go right back to C...
It's actually just the same in C, but a lot of C programmers don't
realise it - they rarely deal with encodings properly. (Things might
well be better now than they were, but basically treating text as if it
were already binary is a bad move.)
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dany |
last post by:
Hi
When I open an Hexa file I have some weird char like when I open it with
Notepad. I want to open it in a readeable form (ex: "FF25AB") like with
Ultra edit.
Thanks
regards,
Dany
|
by: Adam |
last post by:
Hello,
I'm trying to decifer the data in the table that stores the data in the
binary format. All numbers are placed in varbinary fields. All I know is the
MS SQL 2000 database useing collation SQL_Latin1_General_CP1_CI_AS
(default).
For example the content of the field is:
(0xB4F5000000000000) in unicode and defined as varbinary(8).
|
by: Golan |
last post by:
Hello,
I have a hexa file which I need to convert to decimal.
I use memcpy into variables (char for one octet, short for 2 octets
and int for 4 octets) and then print the value into the file by using
fprintf.
The problem is that I don't know how to convert a field of 6 octets?
Should I use a long variable?
Thanks
|
by: [Gho] |
last post by:
How to convert a Hexa value to negative and non negative
value :
if i get 0x1d = result should be 29
but if i get fd = result should be -3
How to do that
|
by: Nadav |
last post by:
Hi,
How can I use C# to print a number in hexadecimal e.g. printf(" %x "); or
print 15 ( decimal ) as 'F' (hexa)
Can a number be printed as binary as-well?
Thanks in advance,
Nadav.
| |
by: Alex |
last post by:
Seems that those functions can't read from or write to any address beyond 2GB
(7FFFFFFF in hexa). when trying to access addresses beyond 2GB nothing
happens - no action and no exception either.
i am on 32 bit system, but i am using an Unsigned Integer poiter (UIntPtr)
so i Should be able to access 2GB-4GB adresses, except i don't.
the functions just dont work. what do i need to do to make it work?
thanks.
|
by: petedawn |
last post by:
hi guys,
based on users button press i am passing the following to my javascript
function, test('é'). and within my javascript i have this
function test(x) which processes this input.
now i am comparing this input using this, if ((x == "é")) {
alert ('eacute clicked') }, but i am unable to capture this for some
reason. everything else works. except special characters. btw i am
using english operating system.
|
by: sarchanakumar |
last post by:
Hi friends, I want to write a program to add two hexa-decimal numbers. I know there is no way of direction addtion. My logic is, first convert two hexa-numbers into decimal after that we add and finally we have to convert the resultant decimal into hexadecimal. Is it right? Give your suggestion.
|
by: lokeshrajoria |
last post by:
hello,
how to convert binary to hexa decimal.....
thanks & regards
lokiiiii
|
by: hpbrothers |
last post by:
How to declare hexa & octadecimals data types in c++
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |