473,807 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with hexa

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. =)
Nov 15 '05 #1
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. =)

Nov 15 '05 #2
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
Nov 15 '05 #3
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. =/
Nov 15 '05 #4
"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...
Nov 15 '05 #5
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
Nov 15 '05 #6
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
Nov 15 '05 #7

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

Similar topics

4
5268
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
1
11743
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).
3
6897
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
2
3547
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
2
17048
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.
2
1597
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.
7
7073
by: petedawn | last post by:
hi guys, based on users button press i am passing the following to my javascript function, test('&eacute;'). and within my javascript i have this function test(x) which processes this input. now i am comparing this input using this, if ((x == "&eacute;")) { 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.
2
3701
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.
5
2368
by: lokeshrajoria | last post by:
hello, how to convert binary to hexa decimal..... thanks & regards lokiiiii
1
2261
by: hpbrothers | last post by:
How to declare hexa & octadecimals data types in c++
0
9600
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
10628
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
10373
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
10374
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
9195
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
5547
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
4331
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
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
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.