Solved

How to convert 16 bit byte to 2 string characters

Posted on 2008-10-29
6
1,149 Views
Last Modified: 2012-05-05
I have a C# app that reads binary data from an app through a C++ import.

The data i am trying to read is held in short integers so i need to split the int16 into its byte pairs and read the values into its two strings.

I do it in VB.net with the following function
 Public Shared Function ByteToString(ByVal bytStr As Byte) As String
        Dim strCon As String        'hold the string
        strCon = Convert.ToChar(bytStr)
        'return the string
        Return strCon
    End Function

But i cannot find the same functionality in C#.

Supposedly this should work but all i store then is the numeric value of the byte
bytByteArr[1].ToString();

Thanks for any help!!
0
Comment
Question by:Halon
  • 3
  • 3
6 Comments
 

Author Comment

by:Halon
ID: 22836177
Little more info

I read in from a byte array 2 bytes at a time
'read the next 2 bytes in                  
                bytByteArr = brContents.ReadBytes(2);

for example first two bytes are 73 and 114 - which are the letters I and r
Then i get 101 and 110 - which are e and n respectively

Hope this helps my clear as mud question
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22836332
If you have a 16-bit integer:

int input = 1234;
int lower = input & 0x00FF;
int upper = (input & 0xFF00) >> 4;
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22836371
Sorry, that should be a shift of 8-bits not 4!

If you have a 16-bit integer:

int input = 1234;

int lower = input & 0x00FF;

int upper = (input & 0xFF00) >> 8;

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 22836427
If you have an array of bytes and you want to convert them to a string you can use the following:
// You get this from the file.

byte[] bytes = new byte[] { 73, 114 };

// Then convert byte array to string, you can use a different encoding if you need to.

string result = System.Text.Encoding.Default.GetString(bytes);

Open in new window

0
 

Author Comment

by:Halon
ID: 22841416
numberkruncher, that works great. i think i am taking the long way at getting what i need done but your idea gets my string out of the binary data.

A question though. I know the field i am grabbing can be up to 20 ints long, thats 40 short ints that i grab. At any point there can be null terminators within the data ('\0' i belive is the code from C++ i am grabbing). How can i strip this from my string or toss it as i create it?

Thanks!!
0
 

Author Comment

by:Halon
ID: 22841747
Its OK, i found it.

//set the char we want to trim, in this case the nulls
        char[] trimNulls = { '\0' };

        //return the result
        return strTextResult.Trim(trimNulls);

Thanks!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now