473,765 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert byte to sbyte - no easy way?

I need to convert a byte to an sbyte - and it has to preserve the
original binary meaning of the 8 bits. For example - a 1111 1111,
which is interpreted as byte to be 255, should show up in the sbyte as
-1. The BitConverter class only takes 2 byte arrays or larger, and
the Convert.ToSByte simply throws an overflow error. Any ideas?

Thanks,
Ray Ackley
Nov 15 '05 #1
2 24885
byte b = 0xFF;
sbyte sb = unchecked((sbyt e)b);

--
--Grant
This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 15 '05 #2
"Grant Richins [MS]" <gr*****@online .microsoft.com> wrote in message news:<uq******* *******@tk2msft ngp13.phx.gbl>. ..
byte b = 0xFF;
sbyte sb = unchecked((sbyt e)b);

Thanks! That worked great.

Ray Ackley
Nov 15 '05 #3

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

Similar topics

4
2662
by: David Levine | last post by:
Using v1.1. of the framework: I was writing a wrapper around the java.util.zip.ZipOutputStream class so that my app could access it using standard FileStream semantics and I ran into a conversion problem. The signature for the write method is void ZipOutputStream.Write(sbyte array,int off,int len) so I figured a simple conversion from byte to sbyte would do the trick. Now, my understanding of an sbyte is that it is a signed byte that...
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
5
2664
by: _Andy_ | last post by:
I'm looking for the alogithm to take a piece of 8-bit audio data, and scale it to a 16-bit short. I got as far as: private static short ByteToShort(byte b) { int word = 0; word |= ( ( b & 0x01 ) << 1 ); word |= ( ( b & 0x02 ) << 2 ); word |= ( ( b & 0x04 ) << 3 ); word |= ( ( b & 0x08 ) << 4 );
1
12533
by: Darrel | last post by:
I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array element in a for loop. Is there a faster method for converting an array of sbytes to an array of bytes? Thanks, Darrel
2
29512
by: jiangyh | last post by:
hi there : I have a question about how to convert Type to DbType? thanks a lot. jiangyh
1
1769
by: G.M. Harland | last post by:
Please keep in mind that I am a PowerBuilder developer working on his first ..NET project. I have a VB.NET console app that is responsible for taking MS Word docs and getting them into an Oracle 9i database using a BLOB column. I have read in the file using a file stream and then put it in a byte array. The problem is that I need to compress the byte array before inserting into the database. I have tried to use ZLIB, but I cannot get...
3
6404
by: reju | last post by:
I have to convert an sByte array to byte array. This sbyte array contains some signed values due to this .net converting functions will throw an exception.any work around for this? is there any way to pass sbyte array to stream classes eg: memory stream?
0
1034
by: hillman | last post by:
Hello, Maestros. Following on from the posts on creating structs from a byte. The methods described there work well. But I seem to have struck a snag that some might find interesting. The C/C++ structs that give problems all have the same issue: they all have at least one field something like this: char character There does not appear to be any way to do this in c#. The closest I could get was fixed sbyte character. This does indeed...
0
10781
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
10156
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
10007
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...
0
9832
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...
1
7375
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
6649
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
5275
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...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.