473,473 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Convert hexa

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
Nov 15 '05 #1
2 3530
Gho,

I don't think that is right. Why can't fd equal 253? It all depends on
the type that you are trying to convert to. Based on that, if the initial
bit is set, then it is a negative number, otherwise, it is a positive one.
However, even converting to an signed byte, a value of fd should come out
to -115 (I believe). I think that a value of 8d will give you -3 though.

Regardless, to convert from a string representation to a value, you can
use the Parse method on the Int32 (or other numeric types, Int16, Byte, etc,
etc) to convert from a string to a value. Use the overload that takes a
NumberStyles enumeration value and pass NumberStyles.HexNumber for that
parameter.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"[Gho]" <yo*****@taux01.nsc.com> wrote in message
news:04****************************@phx.gbl...
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

Nov 15 '05 #2
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
I don't think that is right. Why can't fd equal 253? It all depends on
the type that you are trying to convert to. Based on that, if the initial
bit is set, then it is a negative number, otherwise, it is a positive one.
However, even converting to an signed byte, a value of fd should come out
to -115 (I believe). I think that a value of 8d will give you -3 though.
I don't think so - bear in mind that 0xff (all bits set) should be the
bit pattern for -1.
Regardless, to convert from a string representation to a value, you can
use the Parse method on the Int32 (or other numeric types, Int16, Byte, etc,
etc) to convert from a string to a value. Use the overload that takes a
NumberStyles enumeration value and pass NumberStyles.HexNumber for that
parameter.


Or in this case, just cast from the byte to sbyte - I think that would
work better, as I suspect that SByte.Parse will assume the range of the
type is the range of the string to accept (as it were) with no overflow
- and overflow is basically what the OP wants, I think.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

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

Similar topics

4
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
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...
3
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...
4
by: oddstray | last post by:
Hi, I have a number which is larger than the max unsigned long int. I don't have 64-bit integers available to me. I need to get the resulting 40-bit hex string. I can't find any algorithm...
2
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.
1
by: Chandra | last post by:
Hi, I have a problem related to File Reading. I have a file which contains data which is in Hexa format. I have to read that data and push it to a function which takes a parameter of type char....
2
by: mshahzadali | last post by:
is there any expert who could guide me making a C++ program. I want to convert a Decimal number into a Binary, Octal and a Hexadecimal Number using a C++ Built-in Function(if there exist any).
0
by: Metalpalo | last post by:
Hello everybody I created small batch file in MSDOS. In this batch file i need to print hexadecimal value of some env. variable. For example: I enter value via command :set /P PAR=Enter...
1
by: hpbrothers | last post by:
How to declare hexa & octadecimals data types in c++
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...
1
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
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...
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.